python 并行计算 opencv_python - 与opencv和Python并行读取视频帧 - SO中文参考 - www.soinside.com...

与opencv和Python并行读取视频帧

问题描述 投票:-1回答:1

我必须使用Python中的opencv与多处理和队列并行读取视频中的帧,我的代码出错了。这是我的代码,我不知道我的问题在哪里。

#! /usr/bin/python

import numpy as np

import cv2

import multiprocessing as mp

import time

def read_frames(q1,q2):

while True :

frame = q1.get()

if frame=='Done':

break

R=frame[:,:,0]

G=frame[:,:,1]

B=frame[:,:,2]

y = (np.uint8)((0.299 * R)+ (0.587 *G) +( 0.114 *B))

q2.put(y)

if __name__ == '__main__':

q1 = mp.Queue()

q2 = mp.Queue()

processes =[mp.Process(target=read_frames, args= (q1,q2)) for i in rang$

for p in processes:

p.start()

# feed the processes

# read input file and send to the processes the frames:

cap = cv2.VideoCapture('gou.avi')

lines = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

cols = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

fps = int(cap.get(cv2.CAP_PROP_FPS))

fourcc_ver = int(cap.get(cv2.CAP_PROP_FOURCC))

out = cv2.VideoWriter('output.avi',fourcc_ver, fps, (cols,lines),False)

# y = np.empty(shape=(lines,cols),dtype=np.uint8)

while(cap.isOpened()):

ret, frame = cap.read()

# as long as new frames are there

if ret==True:

q1.put(frame)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

else:

break

q1.put('Done')

for p in processes:

p.join()

for p in processes:

result=[q2.get()]

# result.sort()

# results = [r[1] for r in results]

for i in result:

out.write(i)

# Release everything if job is finished

cap.release()

out.release()

cv2.destroyAllWindows()

python

opencv

1个回答

-1

投票

你可以做的是将所有帧添加到列表[]然后使用列表引用,如frame = list [],然后按列表上的位置寻址帧:frame [0]或frame [1:4]

热门问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值