为什么我在 python 中收到 YOLO 网络摄像头对象检测错误?
Why am I getting this error for YOLO webcam object detection in python?
我正在尝试使用 OpenCV 和 Yolov3 在 python 中进行对象检测。但出于某种原因,我收到此错误:
cv2.error: OpenCV(4.3.0) C:\Users\appveyor\AppData\Local\Temp\pip-req-build-6cwppm05\opencv\modules\dnn\src\darknet\darknet_io.cpp:601: error: (-215:Assertion failed) separator_index < line.size() in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'
这是代码:
import cv2
import numpy as np
net = cv2.dnn.readNet('yolov3.cfg', 'yolov3.weights')
classes = []
with open('coco.names', 'r') as f:
classes = f.read().splitlines()
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
height, width, _ = img.shape
blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), (0, 0, 0), swapRB=True, crop=False)
net.setInput(blob)
output_layers_names = net.getUnconnectedOutLayersNames()
layerOutputs = net.forward(output_layers_names)
boxes = []
confidence = []
class_ids = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
if confidence > 0.5:
center_x = int(detection[0]*width)
center_y = int(detection[1]*height)
w = int(detection[2]*width)
h = int(detection[3]*height)
x = int(center_x - w/2)
y = int(center_y - h/2)
boxes.append([x, y, w, h])
confidence.append((float(confidence)))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidence, 0.5, 0.4)
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size=(len(boxes), 3))
if len(indexes)>0:
for i in indexes.flatten():
x,y,w,h = boxes[i]
label = str(classes[class_ids[i]])
confidence = str(round(confidence[i], 2))
color = colors[i]
cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
cv2.putText(img, label + '' + confidence, (x, y+20), font, 2, (255, 255, 255), 2)
cv2.imshow("Video", img)
if cv2.waitKey(0) & 0xFF == ord('q'):
break
我是 OpenCV 的新手 Python 所以有人可以帮助我吗?
根据您遇到的错误,我建议您在第 3 行尝试此操作:
net = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
并确保 .cfg 和 .weights 文件与您 运行 您的 python 脚本所在的目录相同。
还要确保您的 cfg 文件是正确的。 This 应该是这样的/
我正在尝试使用 OpenCV 和 Yolov3 在 python 中进行对象检测。但出于某种原因,我收到此错误:
cv2.error: OpenCV(4.3.0) C:\Users\appveyor\AppData\Local\Temp\pip-req-build-6cwppm05\opencv\modules\dnn\src\darknet\darknet_io.cpp:601: error: (-215:Assertion failed) separator_index < line.size() in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'
这是代码:
import cv2
import numpy as np
net = cv2.dnn.readNet('yolov3.cfg', 'yolov3.weights')
classes = []
with open('coco.names', 'r') as f:
classes = f.read().splitlines()
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
height, width, _ = img.shape
blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), (0, 0, 0), swapRB=True, crop=False)
net.setInput(blob)
output_layers_names = net.getUnconnectedOutLayersNames()
layerOutputs = net.forward(output_layers_names)
boxes = []
confidence = []
class_ids = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
if confidence > 0.5:
center_x = int(detection[0]*width)
center_y = int(detection[1]*height)
w = int(detection[2]*width)
h = int(detection[3]*height)
x = int(center_x - w/2)
y = int(center_y - h/2)
boxes.append([x, y, w, h])
confidence.append((float(confidence)))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidence, 0.5, 0.4)
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size=(len(boxes), 3))
if len(indexes)>0:
for i in indexes.flatten():
x,y,w,h = boxes[i]
label = str(classes[class_ids[i]])
confidence = str(round(confidence[i], 2))
color = colors[i]
cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
cv2.putText(img, label + '' + confidence, (x, y+20), font, 2, (255, 255, 255), 2)
cv2.imshow("Video", img)
if cv2.waitKey(0) & 0xFF == ord('q'):
break
我是 OpenCV 的新手 Python 所以有人可以帮助我吗?
根据您遇到的错误,我建议您在第 3 行尝试此操作:
net = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
并确保 .cfg 和 .weights 文件与您 运行 您的 python 脚本所在的目录相同。
还要确保您的 cfg 文件是正确的。 This 应该是这样的/