从经验上看,从您调用 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