1

I am trying to run my image processing algorithm on a live feed from the webcam. I want this to run in a parallel process from the multiprocessing module, how can i implement this? This is my current code without parallel coding:

from cv2 import VideoCapture , imshow , waitKey ,imwrite
import numpy as np
from time import time

def greenify (x):
    return some_value

skip = 4

video = VideoCapture(0)
video.set(3,640/skip)
video.set(4,480/skip)

total = 0
top_N = 100

while True:
    image = video.read()[1]        
    if waitKey(1) == 27:
        break

    arr = array([list(map(greenify,j)) for j in image])

    result = unravel_index(argpartition(arr,arr.size-top_N,axis=None)[-top_N:], arr.shape)
    centre = skip*np.median(result[0]) , skip*np.median(result[1])

    imshow('Feed', image)

print('Time taken:',total)
video.release()

1 Answer 1

3
+50

I have modified your code, basically, you make it a function, then you call it in parallel. call bob.start() wherever you want in the code, and within a few miliseconds, the parallel code will run

import numpy as np
from cv2 import VideoCapture

from multiprocessing import Process, Manager
import multiprocessing as mp

def getcors():
    skip = 4
    top_N = 100
    video = VideoCapture(0)
    video.set(3,640/skip)
    video.set(4,480/skip)
    while True:
        frame = video.read()[1]
        arr = np.array([list(map(greenify,j)) for j in frame])
        result = np.unravel_index(np.argpartition(arr,arr.size-top_N,axis=None)[-top_N:], arr.shape)
        centre = skip * np.median(result[1]) , skip*np.median(result[0])

bob = Process(target = getcors)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.