gpt4 book ai didi

python - 无法腌制 : attribute lookup cv2. BRISK 失败

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:12 25 4
gpt4 key购买 nike

我正在尝试运行多个 CMT trackers同时地。出于这个原因,我设置了一个线程池:

import argparse
import cv2
from multiprocessing import Pool
import numpy as np
import os
import sys
import time

import VARtracker
import util

CMT1 = VARtracker.CMT()

... # code lines removed

# Clean up
cv2.destroyAllWindows()

if args.inputpath is not None:
# If a path to a file was given, assume it is a single video file
if os.path.isfile(args.inputpath):
cap = cv2.VideoCapture(args.inputpath)
# Skip first frames if required
if args.skip is not None:
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.skip)

# Otherwise assume it is a format string for reading images
else:
cap = util.FileVideoCapture(args.inputpath)
# Skip first frames if required
if args.skip is not None:
cap.frame = 1 + args.skip

# Check if videocapture is working
if not cap.isOpened():
print 'Unable to open video input.'
sys.exit(1)

# Read first frame
status, im0 = cap.read()
im_gray0 = cv2.cvtColor(im0, cv2.COLOR_BGR2GRAY)
im_draw = np.copy(im0)

# Getting initial bounding boxes
tl1 = [405, 160]
br1 = [450, 275]

VARtracker.initialise(CMT1, im_gray0, tl1, br1)

frame = 1
while True:
pool = Pool(processes=4)
print frame

# Read image
status, im = cap.read()
if not status:
break
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_draw = np.copy(im)

tic = time.time()
# Serial approach
#res1 = VARtracker.process_frame(CMT1, im_gray)

# Parallel approach
res1 = pool.apply_async(VARtracker.process_frame, (CMT2, im_gray))
pool.close()
pool.join()
res1 = res1.get()
toc = time.time()

# Display results
if res1.has_result:
cv2.line(im_draw, res1.tl, res1.tr, (255, 0, 0), 4)
cv2.line(im_draw, res1.tr, res1.br, (255, 0, 0), 4)
cv2.line(im_draw, res1.br, res1.bl, (255, 0, 0), 4)
cv2.line(im_draw, res1.bl, res1.tl, (255, 0, 0), 4)

if not args.quiet:
cv2.imshow('main', im_draw)
cv2.waitKey(pause_time)

# Remember image
im_prev = im_gray
frame += 1

每当我评论串行方法并尝试使用线程(并行方法)时,我都会遇到以下错误:

Traceback (most recent call last):

File "/home/rafael/GIT/CMT-Tracker/VaretoCMT/VARmain.py", line 128, in module res1 = res1.get()

File "/usr/lib/python2.7/multiprocessing/pool.py", line 558, in get raise self._value

cPickle.PicklingError:无法腌制:属性查找 cv2.BRISK 失败

其他文件可以在VARmain.py, VARtracker.py and util.py上遇到.

我已经尝试了很多方法,但仍然没有找到克服此 Python 限制的方法。我发现我不能序列化类方法,只能序列化函数。如果可能,我想使用 Python 标准库来解决它。

最佳答案

我设法解决了它。感谢@Matt 和@Yamaneko。基本上,我将读取图像的 block 移到了辅助函数中。因此,如果 pool size = 6 并且有六个边界框,则每个帧将被读取六次(在每个 worker 中)。这是我发现让它发挥作用的唯一方法。

可以找到当前版本here .

import cv2 as cv
import multiprocessing as mp
import time

def worker(folder_path, list_name, top_left, bot_right, index):
frame_path = folder_path + '/' + list_name[0]
image_0 = cv.imread(frame_path)
gray_0 = cv.cvtColor(image_0, cv.COLOR_BGR2GRAY)

cmt = VARtracker.CMT()
cmt.initialise(gray_0, top_left, bot_right)
box_queue = mp.Queue()

for name in list_name:
frame_path = folder_path + '/' + name
image_now = cv.imread(frame_path)
gray_now = cv.cvtColor(image_now, cv.COLOR_BGR2GRAY)

cmt.process_frame(gray_now)
if cmt.has_result:
print index, name, zip(cmt.tl, cmt.br)
output.put((index, name, zip(cmt.tl, cmt.br)))
print 'Process {} finished'.format(index)

def VARmethod(folder_path, final_frame, top_left, bot_right):
tic = time.time()

if len(top_left) == len(bot_right):
list_frame = [index for index in range(1, final_frame + 1)]
list_name = [str(index) + '.jpg' for index in list_frame]

pool = mp.Pool(5)
for index in range(0, len(top_left)):
pool.apply_async(worker, args=(folder_path, list_name, top_left[index], bot_right[index], index))
pool.close()
pool.join()

print 'Finished with the script'

toc = time.time()
print output.qsize()
print (toc - tic)

关于python - 无法腌制 <type 'cv2.BRISK' > : attribute lookup cv2. BRISK 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38150402/

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