gpt4 book ai didi

Python OpenCV 多处理加速

转载 作者:行者123 更新时间:2023-12-01 01:27:08 27 4
gpt4 key购买 nike

我正在尝试在网络摄像头的实时反馈上运行我的图像处理算法。我希望它在多处理模块的并行进程中运行,我该如何实现呢?这是我当前没有并行编码的代码:

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()

最佳答案

我修改了你的代码,基本上,你把它变成一个函数,然后并行调用它。在代码中任意位置调用 bob.start(),几毫秒内,并行代码就会运行

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)

关于Python OpenCV 多处理加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53240948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com