【问题标题】:Python OpenCV temporarily release VideoCapturePython OpenCV 临时发布 VideoCapture
【发布时间】:2022-07-12 14:23:29
【问题描述】:

我想在使用 OpenCV 按下一个键后用我的网络摄像头捕获和导出图像。问题是初始化时间太长。

我的网络摄像头显示当前是否正在使用绿色 LED。如果我打电话给cap = cv2.VideoCapture(0),网络摄像头初始化大约需要 7 秒。但是,直到我第一次拨打cap.read(),绿色指示灯才会亮起。之后,LED 一直亮着,直到我致电 cap.release()

是否有可能回到调用cap = cv2.VideoCapture(0)cap.read() 之间的状态而不调用cap.release() 并再次使用cap.open(0) 重新初始化VideoCapture,这又需要大约7 秒?同时我不想让其他应用程序使用网络摄像头。

一般来说,我想防止我的网络摄像头一直打开,只有在按下键后的一小段时间内才需要打开。是调用cap = cv2.VideoCapture(0)后一直打开网络摄像头还是只分配资源?

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

【问题讨论】:

    标签: python opencv webcam


    【解决方案1】:

    从经验上看,从您调用 cv2.VideoCapture(0) 到 release() 的那一刻起,似乎摄像头一直处于开启状态,但我在 2 台不同的计算机(笔记本电脑和 Windows 上的固定电脑)上尝试了提到的比我用相同代码得到的要高得多。您可能需要检查您的硬件和环境。可能是您的网络摄像头,opencv 版本,无论您是否构建它。 我在您的代码中附加了时间度量和输出。我的建议是打开一个新环境并获取另一个网络摄像头并运行以下命令:

        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
    

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 2022-01-25
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 2013-03-29
      • 2020-12-19
      相关资源
      最近更新 更多