# Opening opencv cap for webcamera for extended period of time

**URL:** <https://forum.opencv.org/t/opening-opencv-cap-for-webcamera-for-extended-period-of-time/11814>\
**Category:** Python\
**Tags:** programming, django\
**Created:** [January 30, 2023, 7:34am UTC](https://forum.opencv.org/t/opening-opencv-cap-for-webcamera-for-extended-period-of-time/11814 "2023-01-30T07:34:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwhyt](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dwhyt/32/7076_2.png) [@dwhyt](https://forum.opencv.org/u/dwhyt)\
**Post date:** [January 30, 2023, 7:34am UTC](https://forum.opencv.org/t/opening-opencv-cap-for-webcamera-for-extended-period-of-time/11814/1 "2023-01-30T07:34:14Z")

</div>

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

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

```

1. `while` loop that include frame retrieval, counting logic and visualization of the frame with inference results using opencv `cv2.imshow()`
2. 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)

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

```

1. `while` loop that include frame retrieval, counting logic and visualization of the frame with inference results using opencv `cv2.imshow()`
2. 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).
