我不断尝试,我认为列表项在列表的范围内,但显然不是?真的不知道我做错了什么。错误在第57行或
prediction_text = f"{classes[class_index]}: {confidence:.2f}%"
代码:
import tensorflow as tf
import numpy as np
import cv2
import os
print(tf.__version__)
print(cv2.__version__)
#paths for image and models, will work on later for live image recog
image_path = 'object recog/images/1.png'
prototxt_path = 'A:\MobileNetSSD_deploy.prototxt'
model_path = 'A:\MobileNetSSD_deploy.caffemodel'
min_confidence = 0.2
#what objects to recog
classes = ['BG', 'person']
#filler BG as in list needs index at 0 for background appropriately named BG, then classes are defined....
#box colors, when surrounding element
colors = np.random.seed(42)
#load pre-trained model
net = cv2.dnn.readNet(prototxt_path, model_path)
#image elements
image = cv2.imread(image_path)
height, width = image.shape[0], image.shape[1]
#BLOB, store image as binary code, scale factor : 2nd number after 300, ratio for scaling between old and new image
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 0.007, (300, 300), 130)
#prediction
net.setInput(blob)
detected_objects = net.forward()
print(detected_objects[0][0][1])
#output example [ 0. 15. 0.7585785 0.63882416 0.2551059 0.75395054 0.6825095 ]
#2nd number is class index, 3rd is confidence, rest is coordinates from upper left upper right lower left lower right respectively
#max 6 right now
font_text = cv2.FONT_HERSHEY_SIMPLEX
for i in range(detected_objects.shape[1]):
confidence = detected_objects[0][0][i][2]
if confidence > min_confidence:
class_index = int(detected_objects[0, 0, i, 1])
upper_left_x = int(detected_objects[0][0][i][3] * width)
upper_left_y = int(detected_objects[0][0][i][4] * width)
lower_right_x = int(detected_objects[0][0][i][5] * width)
lower_right_y = int(detected_objects[0][0][i][6] * width)
text_y_coords = upper_left_y - 15 if upper_left_y < 30 else upper_left_y + 15
#decides if coordinate of class text is going to be above or below the rectangle depending on if the coords of rectangle are close to top line
#f string for niceness
prediction_text = f"{classes[class_index]}: {confidence:.2f}%"
cv2.rectangle(image, (upper_left_x, upper_left_y), (lower_right_x, lower_right_y), colors[class_index], 2)
#params: starting point x and y from upper left and ending point x and y from lower right, the randomized colors established, thickness of line
cv2.putText(image, prediction_text, (upper_left_x, text_y_coords), font_text, 1, colors[class_index], 2)
#params: image, text, coords where text will be, font, font scale, color, thickness
cv2.imshow("Detected Objects ", image)
cv2.waitKey(0)
#waitkey 0 so doesn't close until a key is pressed
cv2.destroyAllWindows()
尝试更改列表大小,我从另一个堆栈帖子添加了"BG"???没有工作困惑
1条答案
按热度按时间23c0lvtd1#
您可以在模型的prototxt文件中找到可能的输出类的数量:
如果this是您正在使用的模型,则它能够识别21个不同的类。此repo的
demo.py
告诉: