【问题标题】:using YOLOv3 for single-class使用 YOLOv3 进行单类
【发布时间】:2021-04-08 15:12:19
【问题描述】:

我找不到将 YOLOv3 用于单类的解决方案。我只想检测摩托车。我为摩托车编辑了 coco.names,并在 cfg 文件中编辑了过滤器、类。 但是每当我运行我的代码时,它都会出错

line 48, in <module>
    for i in indexes.flatten():
AttributeError: 'tuple' object has no attribute 'flatten'". Here is my code.
import cv2
import numpy as np

net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
classes = []
with open('coco.names', 'r') as f:
    classes = f.read().splitlines()

cap = cv2.VideoCapture('test.mp4')
#img = cv2.imread('image.jpg')

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 = []
    confidences = []
    class_ids = []

    for output in layerOutputs:
        for detection in output:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            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])
                confidences.append((float(confidence)))
                class_ids.append(class_id)

    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)

    font = cv2.FONT_HERSHEY_PLAIN
    colors = np.random.uniform(0, 255, size=(len(boxes), 3))

    for i in indexes.flatten():
        x, y, w, h = boxes[i]
        label = str(classes[class_ids[i]])
        confidence = str(round(confidences[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, (0, 0, 0), 2)

    cv2.imshow('Image', img)
    key = cv2.waitKey(1)
    if key==27:
        break

cap.release()
cv2.destroyAllWindows() 

【问题讨论】:

  • 如果标签不等于“摩托车”:跳过
  • 如果你想使用没有其他所有类的网络,你必须自己训练它

标签: python numpy opencv yolo


【解决方案1】:

你必须再次为你的模型训练你想要的类,你可以参考this question了解详情。

【讨论】:

    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2014-10-31
    • 1970-01-01
    • 2019-12-25
    • 2018-04-19
    • 2021-02-25
    相关资源
    最近更新 更多