我正在嘗試回傳從檢測中回傳的物件串列或至少是物件的名稱。
我的代碼:
while True:
ret, frame = cap.read()
image_np = np.array(frame)
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes'] label_id_offset,
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=1,
min_score_thresh=.85,
agnostic_mode=False)
classes=detections['detection_classes'].astype(np.int64)
scores=detections['detection_scores']
#label_names = [i[0] for i in category_index.items()]
#label_names = np.array(label_names)
#print(label_names[detections['detection_classes']])
print ([category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.8])
cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))
if cv2.waitKey(1) & 0xFF == ord('q'):
cap.release()
break
它給了我以下結果:
TypeError Traceback (most recent call last)
<ipython-input-12-a749c9f2a4b4> in <module>
37
38
---> 39 print ([category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.8])
40 cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))
41
TypeError: 'numpy.int64' object is not iterable
uj5u.com熱心網友回復:
我想你想做類似的事情,
print ([category_index.get(class_) for class_, score in zip(classes, scores) if score > 0.8])
您收到的錯誤是因為您正在傳遞串列中classes[0]的第一個元素classes。單個元素不可迭代enumerate()。
作為旁注,該class_變數以這種方式命名,因為它class是一個保留關鍵字。PEP8 建議在發生沖突時附加下劃線。https://www.python.org/dev/peps/pep-0008/#function-and-method-arguments
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/323904.html
