2013-03-06 14:41:02 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
'''
|
|
|
|
Multithreaded video processing sample.
|
|
|
|
Usage:
|
|
|
|
video_threaded.py {<video device number>|<video file name>}
|
|
|
|
|
|
|
|
Shows how python threading capabilities can be used
|
|
|
|
to organize parallel captured frame processing pipeline
|
|
|
|
for smoother playback.
|
|
|
|
|
|
|
|
Keyboard shortcuts:
|
|
|
|
|
|
|
|
ESC - exit
|
|
|
|
space - switch between multi and single threaded processing
|
|
|
|
'''
|
|
|
|
|
2015-09-14 00:00:22 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
from multiprocessing.pool import ThreadPool
|
|
|
|
from collections import deque
|
|
|
|
|
|
|
|
from common import clock, draw_str, StatValue
|
|
|
|
import video
|
|
|
|
|
|
|
|
|
|
|
|
class DummyTask:
|
|
|
|
def __init__(self, data):
|
|
|
|
self.data = data
|
|
|
|
def ready(self):
|
|
|
|
return True
|
|
|
|
def get(self):
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
|
2015-09-14 00:00:22 +08:00
|
|
|
print(__doc__)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2013-03-06 14:41:02 +08:00
|
|
|
try:
|
|
|
|
fn = sys.argv[1]
|
|
|
|
except:
|
|
|
|
fn = 0
|
2012-10-17 07:18:30 +08:00
|
|
|
cap = video.create_capture(fn)
|
|
|
|
|
|
|
|
|
|
|
|
def process_frame(frame, t0):
|
|
|
|
# some intensive computation...
|
2017-12-11 17:55:03 +08:00
|
|
|
frame = cv.medianBlur(frame, 19)
|
|
|
|
frame = cv.medianBlur(frame, 19)
|
2012-10-17 07:18:30 +08:00
|
|
|
return frame, t0
|
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
threadn = cv.getNumberOfCPUs()
|
2012-10-17 07:18:30 +08:00
|
|
|
pool = ThreadPool(processes = threadn)
|
|
|
|
pending = deque()
|
|
|
|
|
|
|
|
threaded_mode = True
|
|
|
|
|
|
|
|
latency = StatValue()
|
|
|
|
frame_interval = StatValue()
|
|
|
|
last_frame_time = clock()
|
|
|
|
while True:
|
|
|
|
while len(pending) > 0 and pending[0].ready():
|
|
|
|
res, t0 = pending.popleft().get()
|
|
|
|
latency.update(clock() - t0)
|
|
|
|
draw_str(res, (20, 20), "threaded : " + str(threaded_mode))
|
|
|
|
draw_str(res, (20, 40), "latency : %.1f ms" % (latency.value*1000))
|
|
|
|
draw_str(res, (20, 60), "frame interval : %.1f ms" % (frame_interval.value*1000))
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.imshow('threaded video', res)
|
2012-10-17 07:18:30 +08:00
|
|
|
if len(pending) < threadn:
|
|
|
|
ret, frame = cap.read()
|
|
|
|
t = clock()
|
|
|
|
frame_interval.update(t - last_frame_time)
|
|
|
|
last_frame_time = t
|
|
|
|
if threaded_mode:
|
|
|
|
task = pool.apply_async(process_frame, (frame.copy(), t))
|
|
|
|
else:
|
|
|
|
task = DummyTask(process_frame(frame, t))
|
|
|
|
pending.append(task)
|
2017-12-11 17:55:03 +08:00
|
|
|
ch = cv.waitKey(1)
|
2012-10-17 07:18:30 +08:00
|
|
|
if ch == ord(' '):
|
|
|
|
threaded_mode = not threaded_mode
|
|
|
|
if ch == 27:
|
|
|
|
break
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.destroyAllWindows()
|