我正在嘗試獲取 yolov5s.onnx 模型的輸出并在其上運行 NMSBoxes。但我不斷收到此錯誤:
Traceback (most recent call last):
File "python_detection.py", line 132, in <module>
class_ids, confidences, boxes = wrap_detection(inputImage, outs[0])
File "python_detection.py", line 88, in wrap_detection
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
TypeError: Can't convert vector element for 'scores', index=0
在我所見的任何地方,人們都在使用與我完全相同的代碼。這是有道理的,因為這段代碼主要是從教程中復制的。所以我不知道我做錯了什么,一直給我這個錯誤。
這是完整的功能:
def wrap_detection(input_image, output_data):
class_ids = []
confidences = []
boxes = []
rows = output_data.shape[0]
image_width, image_height, _ = input_image.shape
x_factor = image_width / INPUT_WIDTH
y_factor = image_height / INPUT_HEIGHT
for r in range(rows):
row = output_data[r]
confidence = row[4]
if confidence >= 0.4:
classes_scores = row[5:]
_, _, _, max_indx = cv2.minMaxLoc(classes_scores)
class_id = max_indx[1]
if (classes_scores[class_id] > .25):
confidences.append(confidence)
class_ids.append(class_id)
x, y, w, h = row[0].item(), row[1].item(), row[2].item(), row[3].item()
left = int((x - 0.5 * w) * x_factor)
top = int((y - 0.5 * h) * y_factor)
width = int(w * x_factor)
height = int(h * y_factor)
box = np.array([left, top, width, height])
boxes.append(box)
'''
Print the raw output
'''
# Save output
np.set_printoptions(threshold=sys.maxsize)
file = open("python_raw_model_output.txt", "w ")
for i in range(len(boxes)):
file.write(str(boxes[i]) " " str(confidences[i]) " " str(class_ids[i]))
file.write("\n")
file.close()
# NMS on the lists
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
result_class_ids = []
result_confidences = []
result_boxes = []
for i in indexes:
result_confidences.append(confidences[i])
result_class_ids.append(class_ids[i])
result_boxes.append(boxes[i])
return result_class_ids, result_confidences, result_boxes
uj5u.com熱心網友回復:
我遇到過同樣的問題。它似乎與 cuda 配置有關,因為它在 cpu 上運行良好。我沒有弄清楚到底出了什么問題,但我使用 fastNMS 解決了這個問題:在此處輸入鏈接描述
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457152.html
