- OpenCV => 4.2 (4.5.5)
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio Code
- Python Version : 3.8
Hello to everyone,
I want to ask you something about setting height and width set of videocapture.
Is there any fastest way to set the video frame height and width. Because the process, for ex: cap.set(3,720) is too slow. This process takes approximately 6 seconds.
Let me write some code to explain well.
import cv2
import time
cap = cv2.VideoCapture(0)
#Set the resolution
time1 = time.time()
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
print("time1 is :" str(time.time() - time1))
time2 = time.time()
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
print("time2 is :" str(time.time() - time2))
while(True):
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow("preview",frame)
cv2.imwrite("outputImage.jpg", frame)
#Waits for a user input to quit the application
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
The output is
time1 is : 5.48
time2 is : 5.42
This is too slow to set resolution. I want to reduces this setting time if possible.Is there any faster way to set the resolution do you know?
CodePudding user response:
Pass your desired properties when you instantiate the VideoCapture
.
Docs: https://docs.opencv.org/4.x/d8/dfe/classcv_1_1VideoCapture.html#a31e7cf5ba9debaec15437a200b18241e
import cv2 as cv
cap = cv.VideoCapture(index=0, apiPreference=cv.CAP_ANY, params=[
cv.CAP_PROP_FRAME_WIDTH, 1920,
cv.CAP_PROP_FRAME_HEIGHT, 1080,
])
assert cap.isOpened()
while True:
(success, frame) = cap.read()
if not success: break
...
cap.release()
And may I suggest playing with the apiPreference
argument? Try CAP_DSHOW
as well as CAP_MSMF
, since you are on Windows.
CodePudding user response:
Thanks for your answer.
I did what you say but it didn't work. I mean, I tried to pass desired properties when instantiate the VideoCapture but It still takes approximately 20 seconds for initialize.
Also I changed the apiPreference argument, still same.