【问题标题】:How can I get the class name of tensorflow models from zoo如何从动物园获取张量流模型的类名
【发布时间】:2019-12-18 17:17:21
【问题描述】:

最近我一直在玩一些来自 tensorflow object detection api zoo 的模型。

主要是coco ssd_inception和oidv4 ssd_mobilenet。问题在于在图像上标记检测。

例如,当我在 this 图像上运行 oidv4 检测器时,它会显示出现的标签。 This是我从oidv下载的classes文件。

当我在 this 图像上使用 coco 模型时出现另一个问题:

label = "{}: {:.2f}%".format(CLASSES[idx],
IndexError: list index out of range

显然,它将此图像标记为索引 84,而 coco 只有 80 个要检测的对象。我也让我的 coco classes 归档。

以及我目前用于检测的代码:

# import the necessary packages
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import time
import cv2


# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to image")
ap.add_argument("-c", "--confidence", type=float, default=0.3,
    help="minimum probability to filter weak detections")
args = vars(ap.parse_args())



# Leemos las clases disponibles en openImages
CLASSES = [line.rstrip('\n') for line in open('classes.txt', 'r')]
print(CLASSES)

# Le damos colores a las cajas para cada clase
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) 

# Importamos el modelo de red
cvNet = cv2.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'graph.pbtxt')

# Leemos una imagen
img = cv2.imread(args["image"])

# Obtenemos las dimensiones de la imagen
h = img.shape[0] # Alto
w = img.shape[1] # Ancho
cvNet.setInput(cv2.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
detections = cvNet.forward()


# loop over the detections
for i in np.arange(0, detections.shape[2]):
    # extract the confidence (i.e., probability) associated with
    # the prediction
    confidence = detections[0, 0, i, 2]

    # filter out weak detections by ensuring the `confidence` is
    # greater than the minimum confidence
    if confidence > args["confidence"]:
        # extract the index of the class label from the
        # `detections`, then compute the (x, y)-coordinates of
        # the bounding box for the object
        idx = int(detections[0, 0, i, 1])
        print(idx   )
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")

        # draw the prediction on the frame
        label = "{}: {:.2f}%".format(CLASSES[idx],
            confidence * 100)
        cv2.rectangle(img, (startX, startY), (endX, endY),
            COLORS[idx], 2)
        y = startY - 15 if startY - 15 > 15 else startY + 15
        cv2.putText(img, label, (startX, y),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)



cv2.imshow('img', img)
cv2.waitKey()

提前感谢您的帮助!

【问题讨论】:

    标签: python opencv tensorflow object-detection


    【解决方案1】:

    这里的问题是因为COCO dataset80 class names 放入90 class index

    您可以使用 90 个类别的索引来创建标签并使用预测的索引值进行映射。

    下面是COCO dataset 的 90 个类的列表。

    classes_90 = ["background", "person", "bicycle", "car", "motorcycle",
                "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant",
                "unknown", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse",
                "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "unknown", "backpack",
                "umbrella", "unknown", "unknown", "handbag", "tie", "suitcase", "frisbee", "skis",
                "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard",
                "surfboard", "tennis racket", "bottle", "unknown", "wine glass", "cup", "fork", "knife",
                "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog",
                "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "unknown", "dining table",
                "unknown", "unknown", "toilet", "unknown", "tv", "laptop", "mouse", "remote", "keyboard",
                "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "unknown",
                "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush" ] 
    

    以下是修改后的代码,它将解决您的问题。

    # import the necessary packages
    from imutils.video import VideoStream
    from imutils.video import FPS
    import numpy as np
    import argparse
    import imutils
    import time
    import cv2
    
    
    # construct the argument parse and parse the arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", required=True,
        help="path to image")
    ap.add_argument("-c", "--confidence", type=float, default=0.3,
        help="minimum probability to filter weak detections")
    args = vars(ap.parse_args())
    
    
    
    # Leemos las clases disponibles en openImages
    CLASSES = classes_90  #New list of classess with 90 classess.
    print(CLASSES)
    
    # Le damos colores a las cajas para cada clase
    COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) 
    
    # Importamos el modelo de red
    cvNet = cv2.dnn.readNetFromTensorflow('frozen_inference_graph.pb', 'graph.pbtxt')
    
    # Leemos una imagen
    img = cv2.imread(args["image"])
    
    # Obtenemos las dimensiones de la imagen
    h = img.shape[0] # Alto
    w = img.shape[1] # Ancho
    cvNet.setInput(cv2.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
    detections = cvNet.forward()
    
    
    # loop over the detections
    for i in np.arange(0, detections.shape[2]):
        # extract the confidence (i.e., probability) associated with
        # the prediction
        confidence = detections[0, 0, i, 2]
    
        # filter out weak detections by ensuring the `confidence` is
        # greater than the minimum confidence
        if confidence > args["confidence"]:
            # extract the index of the class label from the
            # `detections`, then compute the (x, y)-coordinates of
            # the bounding box for the object
            idx = int(detections[0, 0, i, 1])
            print(idx   )
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")
    
            # draw the prediction on the frame
            label = "{}: {:.2f}%".format(CLASSES[idx],
                confidence * 100)
            cv2.rectangle(img, (startX, startY), (endX, endY),
                COLORS[idx], 2)
            y = startY - 15 if startY - 15 > 15 else startY + 15
            cv2.putText(img, label, (startX, y),
                cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
    
    
    
    cv2.imshow('img', img)
    cv2.waitKey()
    

    【讨论】:

    • @yieniggu - 如果以上答案解决了您的问题,请接受并投票,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 2021-12-10
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多