2012-05-24 17:40:55 +02:00
|
|
|
import numpy as np
|
|
|
|
import cv2
|
|
|
|
|
2012-05-25 16:38:39 +02:00
|
|
|
from multiprocessing.pool import ThreadPool
|
2012-05-24 17:40:55 +02:00
|
|
|
from collections import deque
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2012-05-25 16:38:39 +02:00
|
|
|
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)
|
2012-05-25 16:38:39 +02:00
|
|
|
pool = ThreadPool(processes = threadn)
|
|
|
|
pending = deque()
|
2012-05-24 17:40:55 +02:00
|
|
|
while True:
|
2012-05-25 16:38:39 +02:00
|
|
|
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
|