我的YOLO Python代码仅检测到一个对象,而在Google Colab命令中它检测到多个对象

时间:2019-09-21 17:32:26

标签: python computer-vision yolo

我正在开发一个程序,使用YOLO从视频中检测鱼。我制作了一个可以在YOLO v3的Google Colab命令上正常运行的数据集,但是当我在计算机上将其更改为Python代码时,它只能检测到一条鱼。

我尝试更改置信度,并在其他计算机上尝试了该代码,但没有任何效果,它只能按预期使用Google Colab。

import cv2
import numpy as np

# Load Yolo
net = cv2.dnn.readNet("yolov3-tiny-obj_last.weights", "yolov3-tiny-obj.cfg")
classes = []
with open("obj.name.txt", "r") as f:
    classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))

# Loading image
img = cv2.imread("fish.jpg")
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape

# Detecting objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)

net.setInput(blob)
outs = net.forward(output_layers)

# Showing informations on the screen
class_ids = []
confidences = []
boxes = []

for out in outs:
    for detection in out:
        scores = detection[5:]

        class_id = np.argmax(scores)
        print(class_id)
        confidence = scores[class_id]

        if confidence > 0.1:
            # Object detected
            center_x = int(detection[0] * width)
            center_y = int(detection[1] * height)
            w = int(detection[2] * width)
            h = int(detection[3] * height)

            # Rectangle coordinates
            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.1, 0.4)

font = cv2.FONT_HERSHEY_PLAIN
for i in range(len(boxes)):
    if i in indexes:
        x, y, w, h = boxes[i]
        label = str(classes[class_ids[i]])
        color = colors[0]
        cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
        cv2.putText(img, label, (x, y + 30), font, 3, color, 3)

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

这是我期望的: https://cdn.discordapp.com/attachments/607646929307828237/620716352466059265/wNWrVWhKhO9kAAAAABJRU5ErkJggg.png

这是我目前得到的: https://cdn.discordapp.com/attachments/607646929307828237/625020531015548928/unknown.png

我想念的是什么,你能告诉我吗?

0 个答案:

没有答案