updated video_threaded.py sample:
- switch btw threaded and non-threaded mode - performance counters - description
This commit is contained in:
parent
addcd16d8f
commit
e0c1fb5f76
@ -126,6 +126,17 @@ def Timer(msg):
|
||||
finally:
|
||||
print "%.2f ms" % ((clock()-start)*1000)
|
||||
|
||||
class StatValue:
|
||||
def __init__(self, smooth_coef = 0.5):
|
||||
self.value = None
|
||||
self.smooth_coef = smooth_coef
|
||||
def update(self, v):
|
||||
if self.value is None:
|
||||
self.value = v
|
||||
else:
|
||||
c = self.smooth_coef
|
||||
self.value = c * self.value + (1.0-c) * v
|
||||
|
||||
class RectSelector:
|
||||
def __init__(self, win, callback):
|
||||
self.win = win
|
||||
|
@ -1,29 +1,82 @@
|
||||
'''
|
||||
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
|
||||
'''
|
||||
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
|
||||
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__':
|
||||
def process_frame(frame):
|
||||
import sys
|
||||
|
||||
print __doc__
|
||||
|
||||
try: fn = sys.argv[1]
|
||||
except: fn = 0
|
||||
cap = video.create_capture(fn)
|
||||
|
||||
|
||||
def process_frame(frame, t0):
|
||||
# some intensive computation...
|
||||
frame = cv2.medianBlur(frame, 19)
|
||||
frame = cv2.medianBlur(frame, 19)
|
||||
frame = cv2.medianBlur(frame, 19)
|
||||
return frame
|
||||
return frame, t0
|
||||
|
||||
threadn = 8
|
||||
cap = cv2.VideoCapture(0)
|
||||
threadn = cv2.getNumberOfCPUs()
|
||||
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 = pending.popleft().get()
|
||||
cv2.imshow('result', res)
|
||||
if len(pending) < threadn+1:
|
||||
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))
|
||||
cv2.imshow('threaded video', res)
|
||||
if len(pending) < threadn:
|
||||
ret, frame = cap.read()
|
||||
task = pool.apply_async(process_frame, (frame.copy(),))
|
||||
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)
|
||||
if cv2.waitKey(1) == 27:
|
||||
ch = cv2.waitKey(1)
|
||||
if ch == ord(' '):
|
||||
threaded_mode = not threaded_mode
|
||||
if ch == 27:
|
||||
break
|
||||
|
Loading…
x
Reference in New Issue
Block a user