Opening opencv cap for webcamera for extended period of time

dwhyt
Mon, 30 Jan 2023 07:34:14 +0000
Python

i have a django web application hosted locally on a desktop workstation that is supposed to retrieve a video feed/video frames from a webcamera connected via usb. What then happens is that the frame would used as input into an object detection model to count some stuff, but since the objects are small, i need to retrieve the frame at a higher resolution (720, 1280) instead of the default resolution. after the counting is done, i stop reading frames from the web camera - here is the sequence:

1. Press button in web application to start retrieving video from webcamera

2. create new opencv cap, since i need a higher resolution, i have to specify the resolution

cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

3. while loop that include frame retrieval, counting logic and visualization of the frame with inference results using opencv cv2.imshow()

4. stop retrieving video frames (cv2.destroyAllWindows() and break out of while loop) after counting is done, finally calling cap.release()

the problem with this sequence is that the video stream takes a few seconds (about 3 seconds) to load (this means that after clicking the button in step 1, i only see the video stream in step 3 after about 3 seconds which is quite slow, and there is an autofocus that takes about 1 second after the video stream pops up)

i am considering this new sequence which does not involve the release of the cap. i found that the video stream takes much faster to load and there is no autofocus each time the video stream is loaded:

1. when starting the django server, create the opencv cap and specify the required resolution (meaning this is only done once)

cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

2. while loop that include frame retrieval, counting logic and visualization of the frame with inference results using opencv cv2.imshow()

3. stop retrieving video frames (cv2.destroyAllWindows() and break out of while loop WITHOUT calling cap.release())

Would the second flow result in any problems or damage to the webcamera, especially if the cap is opened for a long time (like weeks or even months).

1 post - 1 participant

Read full topic