【发布时间】: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