天涯盡頭無(wú)女友
2023-09-02 16:22:31
我正在研究實(shí)時(shí)服裝檢測(cè)。所以我從 GitHub 借用了這樣的代碼: https: //github.com/rajkbharali/Real-time-clothes-detection 但是(H, W) = frame.shape[:2]:最后一行出現(xiàn)以下錯(cuò)誤。我應(yīng)該在哪里修復(fù)它?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貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
您的錯(cuò)誤背后的原因是框架是None(Null). 有時(shí),從網(wǎng)絡(luò)攝像頭捕獲的第一幀是“無(wú)”,主要是因?yàn)?(1)網(wǎng)絡(luò)攝像頭尚未準(zhǔn)備好(并且需要一些額外的時(shí)間才能準(zhǔn)備好)或者(2)操作系統(tǒng)不允許您的代碼訪問(wèn)網(wǎng)絡(luò)攝像頭。
在第一種情況下,在對(duì)框架執(zhí)行任何操作之前,您需要檢查框架是否有效:
while True:
?
? ? frame = image.read()
? ? if frame is not None:? # add this line
? ? ? ?
? ? ? (H, W) = frame.shape[:2]
在其他情況下,您需要檢查操作系統(tǒng)中的相機(jī)設(shè)置。
此外,為了捕獲網(wǎng)絡(luò)攝像頭幀,還有另一種基于Opencv 中的VideoCapure類的方法,該方法可能更容易調(diào)試。
添加回答
舉報(bào)
0/150
提交
取消