问题描述
我当前正在处理一个问题,在一项需求中,我需要将3D NumPy数组与其他3d NumPy数组的列表进行比较,并返回不匹配的值及其索引位置。下面是其多处理程序和其中一个流程(可行的版本)的信息。当前,在NumPy比较期间,这花费了太多时间。按照要求,我想更快地执行此操作。任何更好的技术来执行NumPy快速比较都会很有帮助。工作演示如下:
import numpy as np
import queue
import _pickle as cPickle
import zlib
import datetime
import queue
from multiprocessing import Process,Queue,Pool
import cv2
def get_time_milliseconds():
time = (datetime.datetime.now() - datetime.datetime.utcfromtimestamp(0)).total_seconds() * 1000.0
return time
# this function takes the resized batch as argument and return a differences while maintaining
# keyframes.
def create_diff_batch(inp_q,out_q):
while True:
try:
frames = inp_q.get(timeout=0.01)
a = get_time_milliseconds()
diff_batch = []
keyframe = None
i = 0
for frame in frames:
if i == 0:
keyframe = frame[0]
diff_batch.append(frame)
else:
#print('Memory of Frame********************8',asizeof(frame[0])/(1024*1024))
match_mask = (keyframe == frame[0])
idx_unmatched = np.argwhere(~match_mask).astype('uint8') # get index of unmatched value
idx_values = frame[0][tuple(zip(*idx_unmatched))] # get corresponding value of index
frame[0] = [idx_unmatched,idx_values]
diff_batch.append(frame)
i = i + 1
# print('Length********',len(frames),len(diff_batch))
print('High comparison time*******************************',(get_time_milliseconds() - a) / 1000)
out_q.put(zlib.compress(cPickle.dumps(diff_batch)))
except queue.Empty:
pass
batch_queue = Queue()
batch_differencer_queue = Queue()
# start differencer
# pool = Pool()
# pool.map(create_diff_batch,filter_output_queue)
# pool.close()
batcher_process = Process(name='Difference',target=create_diff_batch,args=(batch_queue,batch_differencer_queue,))
batcher_process.start()
# give video path
cap = cv2.VideoCapture('/home/dhaval/piyush/ViIDWIN/Datasets_VIDWIN/personcar.mp4')
frame_batch = []
while True:
ret,frame = cap.read()
if frame is None:
break
frame = cv2.resize(frame,(800,800))
frame_batch.append([frame,0])
if len(frame_batch) >= 20:
batch_queue.put(frame_batch)
frame_batch = []
在上面的代码中,视频帧被读取为NumPy数组,并将这些帧(列表)的一批发送到create_difference函数。现在,将第一帧与所有其余帧进行比较,以获得差值及其索引。我需要加快比较速度,如下所示,这表明比较期间花费了太多时间。也欢迎一种更好的与多处理相关的解决方案。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)