在我的旧项目中,我使用了一个 yolov3 模型并对其进行了训练,并使用了 cv2.readNetFromDarknet(...)、cv2.dnn.blobFromImage(...) 和 net.forward(...) 来运行我的推论,效果很好。我训练了我的 yolov4 模型,并假设我只需要更改项目中的配置和权重文件即可进行推理,但我没有检测到边界框。除了权重和配置之外应该有变化吗?谢谢你。
编辑:
net = cv2.dnn.readNetFromDarknet(PATHS["model_config"], PATHS["model_weights"])
layer_names = net.getLayerNames()
output_layer_names = [ layer_names[index[0] - 1] for index in net.getUnconnectedOutLayers() ]
#during inference
def method(frame):
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (MAGIC_NUMBER_1,
MAGIC_NUMBER_1), swapRB=True, crop=False)
net.setInput(blob)
layerOutputs = net.forward(output_layer_names)
boxes = []
confidences = []
classIDs = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > .5: # NEVER PASSES, CONFIDENCE IS ALWAYS 0 WHEN PRINTED OUT
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
return boxes, confidences, classIDs