Fil*_*ppo 6 python video performance opencv video-streaming
我在使用 intel core i7-4510u 的 Kali linux 上运行此脚本:
import cv2
from datetime import datetime
vid_cam = cv2.VideoCapture(0)
vid_cam.set(cv2.CAP_PROP_FPS, 25)
vid_cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
vid_cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 360)
lastDate = datetime.now().second
fcount = 0
while(vid_cam.isOpened()):
if(datetime.now().second>lastDate):
lastDate = datetime.now().second
print("Fps: " + str(fcount))
fcount = 0
else:
fcount += 1
ret, image_frame = vid_cam.read()
cv2.imshow('frame', image_frame)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
vid_cam.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
如果我运行它,它会打印Fps: 4。
如果我检查任务管理器,我的 cpu 大约为 2%。
问题可能出在哪里?
一个潜在的原因可能是读取帧时的 I/O 延迟。由于cv2.VideoCapture().read()
这是一个阻塞操作,因此主程序会停止,直到从相机设备读取帧并返回为止。提高性能的一种方法是生成另一个线程来并行处理抓取帧,而不是依赖单个线程按顺序抓取帧。我们可以通过创建一个新线程来提高性能,该线程仅轮询新帧,而主线程则负责处理当前帧。这是多线程框架的片段。
from threading import Thread
import cv2, time
class VideoStreamWidget(object):
def __init__(self, src=0):
self.capture = cv2.VideoCapture(src)
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
time.sleep(.01)
def show_frame(self):
# Display frames in main program
cv2.imshow('frame', self.frame)
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
cv2.destroyAllWindows()
exit(1)
if __name__ == '__main__':
video_stream_widget = VideoStreamWidget()
while True:
try:
video_stream_widget.show_frame()
except AttributeError:
pass
Run Code Online (Sandbox Code Playgroud)