msmf test
0:00:02.860830
0:00:04.202524
0:00:05.706894
directshow test
0:00:01.346997
0:00:01.043394
0:00:01.441231
The test results above show that each additional parameter set (via cap.set()) significantly increases the initialization time for the MSMF backend. In contrast, the DirectShow backend’s initialization time does not lengthen with additional parameters.
Windows’ built-in camera application also uses the MSMF backend, yet it initializes very quickly. This leads me to believe the issue lies with OpenCV’s implementation, rather than an inherent problem with the MSMF backend itself.
The problem with using the DirectShow backend is its higher bandwidth requirements. With the MSMF backend, I can simultaneously open six 800x600 cameras, whereas using DirectShow leads to USB bandwidth issues.
Therefore, I would like to ask if there are any methods to speed up the initialization time of the MSMF backend."
stick all the parameters into the optional parameters argument of the constructor.
it’s just a list of keys and values, so it’s an even length list.
superficially that may appear so, but it’s not inherent in the APIs. learn about “pixel formats” in this context. MSMF (opencv’s backend to it) may somehow choose a pixel format that actually stands for compression. the DSHOW backend doesn’t do that on its own. you would have to set the CAP_PROP_FOURCC.
print("msmf test")
t1=datetime.datetime.now()
cap = cv2.VideoCapture(0)
ret,frame=cap.read()
t2=datetime.datetime.now()-t1
print(t2)
cap.release()
t1=datetime.datetime.now()
cap = cv2.VideoCapture(0)
flag=cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
ret,frame=cap.read()
t2=datetime.datetime.now()-t1
print(t2)
cap.release()
t1=datetime.datetime.now()
cap = cv2.VideoCapture(0)
flag1=cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
flag2=cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1920) # Note: this should probably be 1080 for 1920x1080
ret,frame=cap.read()
t2=datetime.datetime.now()-t1
print(t2)
cap.release()
t1=datetime.datetime.now()
params = [
cv2.CAP_PROP_FRAME_WIDTH,1920,
cv2.CAP_PROP_FRAME_HEIGHT, 1080, # Assuming this was the intended height
]
cap = cv2.VideoCapture(0,cv2.CAP_MSMF,params)
ret,frame=cap.read()
t2=datetime.datetime.now()-t1
print(t2)
cap.release()
msmf test
0:00:02.857944
0:00:04.183373
0:00:05.688011
0:00:05.671727
After testing, I found that when using the MSMF backend, passing all parameters at once via params in the constructor takes the same amount of time as setting each parameter separately. This method did not improve the long initialization time."
"When testing with the DirectShow backend, it was confirmed that its default encoding is YUY2. After changing the encoding method to MJPG, bandwidth consumption decreased, and I can now open 5 cameras at 800x600 resolution. However, one camera still fails to open, and its bandwidth usage is still higher than what the MSMF backend consumes.