简体   繁体   中英

Python OpenCV temporarily release VideoCapture

I want capture and export an image with my webcam after pressing a key using OpenCV. The problem is that the initialization takes too long.

My webcam shows if it is currently in use with a green LED. If I call cap = cv2.VideoCapture(0) it takes around 7 seconds until the webcam is initialized. However, the green led does not turn on until I call cap.read() for the first time. Afterwards, the LED remains on until I call cap.release() .

Is it possible to get back into the state between calling cap = cv2.VideoCapture(0) and cap.read() without calling cap.release() and reinitializing the VideoCapture using cap.open(0) again, which will take around 7 seconds again? I do not want to allow the webcam to be used by other applications in the meantime.

In general, I want to prevent my webcam from being turned on all the time, when it is only necessary for a short moment after the key has been pressed. Is the webcam turned on all the time after calling cap = cv2.VideoCapture(0) or are the resources allocated only?

MWE:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
tmp_img = np.zeros(shape=[512, 512, 3], dtype=np.uint8)

while True:
    cv2.imshow('windo1', tmp_img)
    k = cv2.waitKey(1)

    if k == ord('q'):
        ret, img = cap.read()  # webcam LED turns on once 'q' is pressed
        cv2.imshow('window1', img)
        
        cap.release()  # webcam LED turns of now
        cap.open(0)  # webcam LED remains off until 'q' is pressed again, but this call takes a long time again

Empirically, it seems like the cam will be on from the moment you call cv2.VideoCapture(0) until the release(), but i tried on 2 different computer(laptop and stationary both on windows) and it the time you mentioned significantly high than what i got with the same code. you might want to check your hardware and environment. could be your webcam, opencv version, if you built it or not. i attached your code with time measures and the output. my suggestion is open a new environment and get another webcam and run the following:

    import wizzi_utils as wu  # pip install wizzi_utils
    import cv2
    import numpy as np
    fps = wu.FPS('cv2 capturing')  # pip install wizzi_utils
    cap = cv2.VideoCapture(0)
    stand_by_img = np.zeros(shape=(240, 320, 3), dtype='uint8')
    wu.cvt.add_text(stand_by_img, header='take_img():', pos=(0, 50), text_color='aqua', with_rect=False,
                    bg_color='b', bg_font_scale=1)
    wu.cvt.add_text(stand_by_img, header="press 't' to take image using cv2", pos=(0, 100),
                    text_color='aqua', with_rect=False, bg_color='b', bg_font_scale=2)
    wu.cvt.add_text(stand_by_img, header="press 'q' to end", pos=(0, 150), text_color='aqua',
                    with_rect=False, bg_color='b', bg_font_scale=2)

    while True:
        k = wu.cvt.display_open_cv_image(img=stand_by_img, ms=1, title='stand_by_img', loc='top_center')

        if k == ord('t'):
            fps.start(ack_progress=True)
            success, frame_cv = cap.read()  # webcam LED turns on once 'q' is pressed
            if success:
                t = 'capture'
                cv2.imshow(t, frame_cv)
                wu.cvt.move_cv_img_by_str(frame_cv, t, where='top_left')

            cap.release()  # webcam LED turns of now
            cap.open(0)  # webcam LED remains off until 'q' is pressed again, but this call takes a long time again
            fps.update(ack_progress=True)
        elif k == ord('q'):
            break
    fps.finalize()
    cv2.destroyAllWindows()
    Iter 1 Started:
    Iter 1 Done: iterTime=0.450s, 2.22 FPS
    Iter 2 Started:
    Iter 2 Done: iterTime=0.885s, totalTime=1.336s, avgTime=0.668s, 1.50 FPS
    Iter 3 Started:
    Iter 3 Done: iterTime=0.821s, totalTime=2.157s, avgTime=0.719s, 1.39 FPS
    Iter 4 Started:
    Iter 4 Done: iterTime=1.176s, totalTime=3.333s, avgTime=0.833s, 1.20 FPS
    Iter 5 Started:
    Iter 5 Done: iterTime=1.002s, totalTime=4.335s, avgTime=0.867s, 1.15 FPS
    Iter 6 Started:
    Iter 6 Done: iterTime=1.190s, totalTime=5.525s, avgTime=0.921s, 1.09 FPS
    Iter 7 Started:
    Iter 7 Done: iterTime=1.227s, totalTime=6.752s, avgTime=0.965s, 1.04 FPS
    Iter 8 Started:
    Iter 8 Done: iterTime=1.235s, totalTime=7.987s, avgTime=0.998s, 1.00 FPS
    Iter 9 Started:
    Iter 9 Done: iterTime=0.952s, totalTime=8.939s, avgTime=0.993s, 1.01 FPS
    Iter 10 Started:
    Iter 10 Done: iterTime=0.746s, totalTime=9.685s, avgTime=0.969s, 1.03 FPS
    Summary of cv2 capturing(10 iters)
        Total   Run Time = 9.685s
        Average Run Time = 0.969s (std = 0.240)
        1.03 FPS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM