37 Multithreaded video processing sample.
39 video_threaded.py {<video device number>|<video file name>}
41 Shows how python threading capabilities can be used
42 to organize parallel captured frame processing pipeline
43 for smoother playback.
48 space - switch between multi and single threaded processing
52 from __future__
import print_function
57 from multiprocessing.pool
import ThreadPool
58 from collections
import deque
60 from common
import clock, draw_str, StatValue
75 self.
_start_start = datetime.datetime.now()
79 self.
_end_end = datetime.datetime.now()
87 return (self.
_end_end - self.
_start_start).total_seconds()
110 cap = video.create_capture(fn)
114 def process_frame(frame, t0):
116 frame = cv.medianBlur(frame, 19)
117 frame = cv.medianBlur(frame, 19)
120 threadn = cv.getNumberOfCPUs()
121 pool = ThreadPool(processes = threadn)
128 last_frame_time =
clock()
130 while len(pending) > 0
and pending[0].ready():
131 res, t0 = pending.popleft().get()
132 latency.update(
clock() - t0)
133 draw_str(res, (20, 20),
"threaded : " +
str(threaded_mode))
134 draw_str(res, (20, 40),
"latency : %.1f ms" % (latency.value*1000))
135 draw_str(res, (20, 60),
"frame interval : %.1f ms" % (frame_interval.value*1000))
136 draw_str(res, (20, 80),
"FPS : %.1f" % (1.0/frame_interval.value))
137 cv.imshow(
'threaded video', res)
138 if len(pending) < threadn:
139 _ret, frame = cap.read()
141 frame_interval.update(t - last_frame_time)
144 task = pool.apply_async(process_frame, (frame.copy(), t))
146 task =
DummyTask(process_frame(frame, t))
150 threaded_mode =
not threaded_mode
160 print(
"[INFO] elasped time: {:.2f}".format(fps.elapsed()))
161 print(
"[INFO] approx. FPS: {:.2f}".format(fps.fps()))
163 if __name__ ==
'__main__':
166 cv.destroyAllWindows()
def draw_str(dst, target, s)