作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在玩 opencv2,使用 Python 跟踪多个对象 -
cv2.__version__
= 4.5.1
代码-
import imutils
import time
import cv2
import numpy as np
trackers = cv2.legacy_MultiTracker.create()
vs = cv2.VideoCapture('4.mp4')
while True:
frame = vs.read()
if frame is None:
break
frame = frame[1]
(success, boxes) = trackers.update(frame)
for box in boxes:
(x, y, w, h) = [int(v) for v in box]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("s"):
box = cv2.selectROI("Frame", frame, fromCenter=False,
showCrosshair=True)
print(box)
tracker = cv2.TrackerKCF()
trackers.add(tracker, frame, box)
elif key == ord("q"):
break
vs.release()
cv2.destroyAllWindows()
我遇到了一个错误 -
--->trackers.add(tracker, frame, box)
TypeError: Expected Ptr<cv::legacy::Tracker> for argument 'newTracker'
我想知道这个错误,但找不到任何博客。补充一下,我认为 cv2.MultiTracker_create() 函数替换为 cv2.legacy_MultiTracker.create()
帮帮我,谢谢
最佳答案
cv.legacy.TrackerXXX_create() + cv2.legacy_MultiTracker.create() 为我工作。这是新代码....
from __future__ import print_function
import sys
import cv2
from random import randint
trackerTypes = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
def createTrackerByName(trackerType):
# Create a tracker based on tracker name
if trackerType == trackerTypes[0]:
tracker = cv2.legacy.TrackerBoosting_create()
elif trackerType == trackerTypes[1]:
tracker = cv2.legacy.TrackerMIL_create()
elif trackerType == trackerTypes[2]:
tracker = cv2.legacy.TrackerKCF_create()
elif trackerType == trackerTypes[3]:
tracker = cv2.legacy.TrackerTLD_create()
elif trackerType == trackerTypes[4]:
tracker = cv2.legacy.TrackerMedianFlow_create()
elif trackerType == trackerTypes[5]:
tracker = cv2.legacy.TrackerGOTURN_create()
elif trackerType == trackerTypes[6]:
tracker = cv2.TrackerMOSSE_create()
elif trackerType == trackerTypes[7]:
tracker = cv2.legacy.TrackerCSRT_create()
else:
tracker = None
print('Incorrect tracker name')
print('Available trackers are:')
for t in trackerTypes:
print(t)
return tracker
# Set video to load
videoPath = "bikefit.mov"
# Create a video capture object to read videos
cap = cv2.VideoCapture(videoPath)
# Read first frame
success, frame = cap.read()
# quit if unable to read the video file
if not success:
print('Failed to read video')
sys.exit(1)
## Select boxes
bboxes = []
colors = []
# OpenCV's selectROI function doesn't work for selecting multiple objects in Python
# So we will call this function in a loop till we are done selecting all objects
while True:
# draw bounding boxes over objects
# selectROI's default behaviour is to draw box starting from the center
# when fromCenter is set to false, you can draw box starting from top left corner
bbox = cv2.selectROI('MultiTracker', frame)
bboxes.append(bbox)
colors.append((randint(0, 255), randint(0, 255), randint(0, 255)))
print("Press q to quit selecting boxes and start tracking")
print("Press any other key to select next object")
k = cv2.waitKey(0) & 0xFF
print(k)
if (k == 113): # q is pressed
break
print('Selected bounding boxes {}'.format(bboxes))
# Specify the tracker type
trackerType = "CSRT"
createTrackerByName(trackerType)
# Create MultiTracker object
multiTracker = cv2.legacy.MultiTracker_create()
# Initialize MultiTracker
for bbox in bboxes:
multiTracker.add(createTrackerByName(trackerType), frame, bbox)
# Process video and track objects
while cap.isOpened():
success, frame = cap.read()
if not success:
break
# get updated location of objects in subsequent frames
success, boxes = multiTracker.update(frame)
# draw tracked objects
for i, newbox in enumerate(boxes):
p1 = (int(newbox[0]), int(newbox[1]))
p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
cv2.rectangle(frame, p1, p2, colors[i], 2, 1)
# show frame
cv2.imshow('MultiTracker', frame)
# quit on ESC button
if cv2.waitKey(1) & 0xFF == 27: # Esc pressed
break
关于python - 多对象跟踪 - 参数 'newTracker' 的预期 Ptr<cv::legacy::Tracker>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65967096/
我一直在浏览 Google Analytics 网站上的文档和资源,以使用 Android SDK 4 实现它。我浏览了多个堆栈溢出帖子,但其他人似乎有其他问题。我遵循了初始化跟踪器的步骤,创建了一个
我一直在玩 opencv2,使用 Python 跟踪多个对象 - cv2.__version__ = 4.5.1 代码- import imutils import time import cv2 i
我是一名优秀的程序员,十分优秀!