gpt4 book ai didi

python - 多对象跟踪 - 参数 'newTracker' 的预期 Ptr

转载 作者:行者123 更新时间:2023-12-04 16:25:16 30 4
gpt4 key购买 nike

我一直在玩 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/

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