Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

video capture opencv and multiprocessing

from __future__ import print_function

import numpy as np
import cv2 as cv

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

    print(__doc__)

    try:
        fn = sys.argv[1]
    except:
        fn = 0
    cap = video.create_capture(fn)


    def process_frame(frame, t0):
    # some intensive computation...
        frame = cv.medianBlur(frame, 19)
        frame = cv.medianBlur(frame, 19)
        return frame, t0

    threadn = cv.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, 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))
            cv.imshow('threaded video', res)
        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)
        ch = cv.waitKey(1)
        if ch == ord(' '):
            threaded_mode = not threaded_mode
        if ch == 27:
            break
    cv.destroyAllWindows()
Comment

PREVIOUS NEXT
Code Example
Python :: win64pyinstaller 
Python :: what does % do in python 
Python :: python list contain list 
Python :: django optional path parameter 
Python :: np.random.exponential 
Python :: How to install packages offline? Ask Question 
Python :: python constant 
Python :: python swap numbers 
Python :: jupyter change cell to text 
Python :: how to make a numpy array of certain values 
Python :: wav file to array python 
Python :: how to append string to another string in python 
Python :: shallow copy in python 
Python :: python regex to find year 
Python :: line plotly with shaded area 
Python :: how to convert python to exe 
Python :: duplicates in python list 
Python :: colon in array python 
Python :: django x-frame-options allowall 
Python :: python - count number of occurence in a column 
Python :: tkinter python 
Python :: python filter() 
Python :: how to strip white space of text in python? 
Python :: len(sys.argv) == 2 
Python :: pandas nat to null? 
Python :: getting input in python 
Python :: box plot python 
Python :: batch gradient descent 
Python :: matplotlib legend get handles 
Python :: get ticks pygame 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =