opencv/samples/python2/video_threaded.py

30 lines
868 B
Python
Raw Normal View History

2012-05-24 17:40:55 +02:00
import numpy as np
import cv2
from multiprocessing.pool import ThreadPool
2012-05-24 17:40:55 +02:00
from collections import deque
if __name__ == '__main__':
def process_frame(frame):
# some intensive computation...
frame = cv2.medianBlur(frame, 19)
frame = cv2.medianBlur(frame, 19)
frame = cv2.medianBlur(frame, 19)
return frame
threadn = 8
2012-05-24 17:40:55 +02:00
cap = cv2.VideoCapture(0)
pool = ThreadPool(processes = threadn)
pending = deque()
2012-05-24 17:40:55 +02:00
while True:
while len(pending) > 0 and pending[0].ready():
res = pending.popleft().get()
cv2.imshow('result', res)
if len(pending) < threadn+1:
ret, frame = cap.read()
task = pool.apply_async(process_frame, (frame.copy(),))
pending.append(task)
2012-05-24 17:40:55 +02:00
if cv2.waitKey(1) == 27:
break