我正在研究實時服裝檢測。所以我從 GitHub 借用了這樣的代碼: https: //github.com/rajkbharali/Real-time-clothes-detection 但是(H, W) = frame.shape[:2]:最后一行出現以下錯誤。我應該在哪里修復它?from time import sleepimport cv2 as cvimport argparseimport sysimport numpy as npimport os.pathfrom glob import globimport imutilsfrom imutils.video import WebcamVideoStreamfrom imutils.video import FPSfrom google.colab import drivedrive.mount('/content/drive')%cd /content/drive/My Drive/experiment/Yolo_mark-master/x64/Release/Labels = []classesFile1 = "data/obj.names";with open(classesFile1, 'rt') as f: Labels = f.read().rstrip('\n').split('\n')np.random.seed(42)COLORS = np.random.randint(0, 255, size=(len(Labels), 3), dtype="uint8")weightsPath = "obj_4000.weights"configPath = "obj.cfg"net1 = cv.dnn.readNetFromDarknet(configPath, weightsPath)net1.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)net1.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)image = WebcamVideoStream(src=0).start()fps = FPS().start()#'/home/raj/Documents/yolov3-Helmet-Detection-master/safety.mp4'#while fps._numFrames<100:while True:#for fn in glob('images/*.jpg'): frame = image.read() #frame = imutils.resize(frame,width=500) (H, W) = frame.shape[:2]
1 回答

蕭十郎
TA貢獻1815條經驗 獲得超13個贊
您的錯誤背后的原因是框架是None(Null). 有時,從網絡攝像頭捕獲的第一幀是“無”,主要是因為 (1)網絡攝像頭尚未準備好(并且需要一些額外的時間才能準備好)或者(2)操作系統不允許您的代碼訪問網絡攝像頭。
在第一種情況下,在對框架執行任何操作之前,您需要檢查框架是否有效:
while True:
?
? ? frame = image.read()
? ? if frame is not None:? # add this line
? ? ? ?
? ? ? (H, W) = frame.shape[:2]
在其他情況下,您需要檢查操作系統中的相機設置。
此外,為了捕獲網絡攝像頭幀,還有另一種基于Opencv 中的VideoCapure類的方法,該方法可能更容易調試。
添加回答
舉報
0/150
提交
取消