【发布时间】:2025-11-26 15:30:01
【问题描述】:
我正在尝试同时运行多个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(最近一次调用最后一次):
文件“/home/rafael/GIT/CMT-Tracker/VaretoCMT/VARmain.py”,第 128 行,模块 res1 = res1.get()
文件“/usr/lib/python2.7/multiprocessing/pool.py”,第 558 行,在 get raise self._value 中
cPickle.PicklingError: Can't pickle : 属性查找 cv2.BRISK 失败
可以在VARmain.py, VARtracker.py and util.py 上遇到其他文件。
我尝试了很多方法,但仍然没有找到克服 Python 限制的方法。我发现我不能序列化类方法,只能序列化函数。如果可能的话,我想用 Python 标准库来解决它。
【问题讨论】:
-
您的 GitHub 不允许访问那里的文件...
-
一个 PyCharm 项目可以在这里找到:link
-
在此处找到缺少的用于 Python 3.5 构建的
cv2库:lfd.uci.edu/~gohlke/pythonlibs/#opencv 但在安装并运行VARmain后,出现此错误:VARtracker.initialise(CMT1, im_gray0, tl1, br1) NameError : 名称“im_gray0”未定义 -
if args.inputpath is not None:循环永远不会到达,所以cap永远不会被设置,导致上述错误 - 我应该调用VARmain指向video_tennis目录吗?请告知@RafaelHenrique 如何执行此操作,以便我可以尝试提供帮助,谢谢
标签: python multithreading opencv