gpt4 book ai didi

python - 使用 GOTURN 跟踪调用 traker.update 时,我需要帮助来修复错误

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

我正在学习使用 GOTURN openCV api 来跟踪对象。我正在遵循 learnopenCV 的指南。初始化跟踪器并进入循环后,更新跟踪器时出现错误

ok, bbox = tracker.update(frame)
Traceback (most recent call last):

File "<ipython-input-64-e7c5a34c2f7a>", line 1, in <module>
ok, bbox = tracker.update(frame)

error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\dnn\src\layers\convolution_layer.cpp:282: error: (-2:Unspecified error) Number of input channels should be multiple of 3 but got 1 in function 'cv::dnn::ConvolutionLayerImpl::getMemoryShapes'

我不确定 channel 是什么意思。我从视频文件中得到了帧,形状是(行,宽度,3)。我以为 channel 是 3,但它不起作用。我确实尝试将框架转换为具有形状(行,宽度)的灰度,但它仍然无法正常工作。

下面是我的代码:
import cv2
import sys

background_path = 'images/images_G1_323/background.png'
background_img = cv2.imread(background_path,cv2.IMREAD_GRAYSCALE)
#cv2.imshow('background image',background_img)

tracker = cv2.TrackerGOTURN_create()

video_path = 'videos/G1_323.avi'
cap = cv2.VideoCapture(video_path)

#fgbg = cv2.createBackgroundSubtractorMOG2()
if cap.isOpened() == False:
print('ERROR FILE NOT FOUND OR WRONG CODEC USED!')
sys.exit()

# Read first frame
ok, frame = cap.read()
ok, frame = cap.read()
ok, frame = cap.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if not ok:
print('Cannot read video file')
sys.exit()

#motion = fgbg.apply(frame)
motion = cv2.absdiff(background_img,frame_gray)

_, thresh1 = cv2.threshold(motion, 10, 255, cv2.THRESH_BINARY)
#gray = cv2.cvtColor(thresh1, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(thresh1, (5, 5), 0)
thresh = cv2.threshold(blurred, 30, 255, cv2.THRESH_BINARY)[1]
erosion_size = 10
dilate_size = 14
thresh = cv2.erode(thresh, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (erosion_size, erosion_size)))
thresh = cv2.dilate(thresh, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (dilate_size, dilate_size)))

contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
c = contours[0]
(x, y, w, h) = cv2.boundingRect(c)
bbox = (x, y, w, h)

# Initialize tracker with first frame and bounding box
ok = tracker.init(frame,bbox)

while (cap.isOpened):
#
#if ret is true than no error with cap.isOpened
ok, frame = cap.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

if ok==True:

# Start timer
timer = cv2.getTickCount()

# Update tracker
ok, bbox = tracker.update(frame)

# Calculate Frames per second (FPS)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);

# Draw bounding box
if ok:
# Tracking success
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame_gray, p1, p2, (255,0,0), 2, 1)
else :
# Tracking failure
cv2.putText(frame_gray, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)

# Display tracker type on frame
cv2.putText(frame_gray, "GOTURN Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);

# Display FPS on frame
cv2.putText(frame_gray, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);

# Display result
cv2.imshow("Tracking", frame_gray)

# Exit if ESC pressed
if cv2.waitKey(100) & 0xFF == ord("q"):
break
else:
break

cap.release()
cv2.destroyAllWindows()

最佳答案

我正在做同样的事情,所以我希望我遇到的一些事情可以帮助你(否则我很抱歉给出一个糟糕的答案)。我认为你不应该把它放在灰色,因为它要求 3 个 channel (RGB 而不是灰色/BW)。当您保留“convert2gray”时,您仍然输入:“thresh = cv2.threshold(blurred, 30, 255, cv2.THRESH_BINARY)[1]”,这也只会给您 1 个 channel 。删除最后的“[1]”并转换为灰色,也许它会起作用?

关于python - 使用 GOTURN 跟踪调用 traker.update 时,我需要帮助来修复错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853597/

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