我正在关注有关制作对象检测软件的 YouTube 教程(教程链接),在视频播放 10 分钟时我遇到了问题。我添加了自己的测试视频来查看代码是否有效,但是当我运行它时,出现以下错误:
TypeError: onVideo() missing 1 required positional argument: 'self'
我对 Python 中的类很陌生,我不确定它们是如何工作的。任何解决此错误的帮助将不胜感激。下面是我的代码:
import cv2
import numpy as np
import time
class Detector:
def __init__(self, videoPath, configPath, modelPath, classesPath):
self.videoPath = videoPath
self.configPath = configPath
self.modelPath = modelPath
self.classesPath = classesPath
self.net = cv2.dnn_DetectionModel(self.modelPath, self.configPath)
self.net.setInputSize(320,320)
self.net.setInputScale(1.0/127.5)
self.net.setInputMean((127.5, 127.5, 127.5))
self.net.setInputSwapRB(True)
self.readClasses()
def readClasses(self):
with open(self.classesPath, 'r') as f:
self.classesList = f.read().splitlines()
self.classesList.insert(0, '__Background__')
self.colorList = np.random.uniform(low=0, high=255, size=(len(self.classesList), 3))
def onVideo(self):
cap = cv2.VideoCapture(self.videoPath)
if not cap.isOpened():
print("Error opening the file...")
return
success, image = cap.read()
while success:
classLabelIDs, confidences, bboxs = self.net.detect(image, confThreshold=0.4)
bboxs = list(bboxs)
confidences = list(np.array(confidences).reshape(1, -1)[0])
confidences = list(map(float, confidences))
bboxIdx = cv2.dnn.NMSBoxes(bboxs, confidences, score_threshold=0.5, nms_threshold=0.2)
if len(bboxIdx) != 0:
for i in range(len(bboxIdx)):
bbox = bboxs[np.squeeze(bboxIdx[i])]
classConfidence = confidences[np.squeeze(bboxIdx[i])]
classLabelID = np.squeeze(classLabelIDs[np.squeeze(bboxIdx[i])])
classLabel = self.classesList[classLabelID]
classColor = [int(c) for c in self.colorList[classLabelID]]
displayText = "{}:{:.2f}".format(classLabel, classConfidence)
x, y, w, h = bbox
cv2.putText(image, displayText, (x, y-10), cv2.FONT_HERSHEY_PLAIN, 1, classColor, 2)
cv2.rectangle(image, (x, y), (x+w, y+h), color=classColor, thickness=1)
cv2.imshow("Result", image)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
success, image = cap.read()
cv2.destroyAllWindows()
主要
from Detector import *
import os
def main():
videoPath = "test_videos/gamevid.mp4"
configPath = os.path.join("model_data", "ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt")
modelPath = os.path.join("model_data", "frozen_inference_graph.pb")
classesPath = os.path.join("model_data", "coco.names")
detector = Detector(videoPath, configPath, modelPath, classesPath)
detector.onVideo()
if __name__ == '__main__':
main()
错误信息
Traceback (most recent call last):
File "c:\Users\mondo\Desktop\Final DT software\real_time_object_detection_cpu-main\model_data\DetectorMain.py", line 12, in main
Detector.onVideo()
TypeError: onVideo() missing 1 required positional argument: 'self'
我创建了 Detector 类的实例,但当我尝试调用 onVideo 时仍然看到错误。我相信我可能在调用方法或传递参数的方式上犯了错误。
我不熟悉使用 Python 课程和后续教程。 尝试调用 onVideo 方法时发生错误。 如何修复我的代码以调用 onVideo 方法并正确解决 TypeError?
您创建了
Detector
的实例,但您没有使用它。
d = Detector(videoPath, configPath, modelPath, classesPath)
d.onVideo()
由于描述符协议的工作原理,
d.onVideo()
相当于Dectector.onVideo(d)
。当您调用 Decector.onVideo()
时,您没有提供描述符协议隐式提供的实例。