隐藏生成的边界框的准确率百分比
Hide the accuracy percentage from the bounding box generated
仅显示预测的 class 名称并隐藏 accuracy/confidence 在检测到的对象上制作的边界框的百分比
我已经训练了一个自定义对象检测模型,并获得了具有预测 class 名称的边界框以及我现在对象的置信度百分比。下面是我的代码
def recognize_object(model_name,ckpt_path,label_path,test_img_path):
count=0
sys.path.append("..")
MODEL_NAME = model_name
PATH_TO_CKPT = ckpt_path
PATH_TO_LABELS = label_path
PATH_TO_IMAGE = list(glob(test_img_path))
NUM_CLASSES = 3
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
sess = tf.Session(graph=detection_graph)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
for paths in range(len(PATH_TO_IMAGE)):
image = cv2.imread(PATH_TO_IMAGE[paths])
image_expanded = np.expand_dims(image, axis=0)
(boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections],feed_dict={image_tensor: image_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=4,
min_score_thresh=0.80)
coordinates=vis_util.return_coordinates(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=4,
min_score_thresh=0.80)
threshold=0.80
cv2.imwrite("C:\new_multi_cat\models\research\object_detection\my_imgs\frame%d.jpg"%count,image)
count += 1
cv2.waitKey(0)
cv2.destroyAllWindows()
model_name='inference_graph'
ckpt_path=("C:\new_multi_cat\models\research\object_detection\inference_graph\frozen_inference_graph.pb")
label_path=("C:\new_multi_cat\models\research\object_detection\training\labelmap.pbtxt")
test_img_path=("C:\Python35\target_non_target\Target_images_new\*.jpg")
recognize = recognize_object(model_name,ckpt_path,label_path,test_img_path)
假设我的模型从图像中检测到一只老虎。因此,它在检测到的老虎周围制作了一个边界框,显示预测的 class 名称,置信度百分比为 (TIGER 80%)。我只想在我的边界框上显示预测的 class 名称,而不是像 (TIGER) only
那样制作边界框时的百分比
这里有一个简单的解决方案,只需将skip_scores=True
添加到函数visualize_boxes_and_labels_on_image_array
即可。所以函数调用是:
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index = category_index,
use_normalized_coordinates=True,
line_thickness=4,
min_score_thresh=0.80,
skip_scores=True)
我已经在 Kitti 数据集中的图像上进行了测试。没有分数显示!
仅显示预测的 class 名称并隐藏 accuracy/confidence 在检测到的对象上制作的边界框的百分比
我已经训练了一个自定义对象检测模型,并获得了具有预测 class 名称的边界框以及我现在对象的置信度百分比。下面是我的代码
def recognize_object(model_name,ckpt_path,label_path,test_img_path):
count=0
sys.path.append("..")
MODEL_NAME = model_name
PATH_TO_CKPT = ckpt_path
PATH_TO_LABELS = label_path
PATH_TO_IMAGE = list(glob(test_img_path))
NUM_CLASSES = 3
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
sess = tf.Session(graph=detection_graph)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
for paths in range(len(PATH_TO_IMAGE)):
image = cv2.imread(PATH_TO_IMAGE[paths])
image_expanded = np.expand_dims(image, axis=0)
(boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections],feed_dict={image_tensor: image_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=4,
min_score_thresh=0.80)
coordinates=vis_util.return_coordinates(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=4,
min_score_thresh=0.80)
threshold=0.80
cv2.imwrite("C:\new_multi_cat\models\research\object_detection\my_imgs\frame%d.jpg"%count,image)
count += 1
cv2.waitKey(0)
cv2.destroyAllWindows()
model_name='inference_graph'
ckpt_path=("C:\new_multi_cat\models\research\object_detection\inference_graph\frozen_inference_graph.pb")
label_path=("C:\new_multi_cat\models\research\object_detection\training\labelmap.pbtxt")
test_img_path=("C:\Python35\target_non_target\Target_images_new\*.jpg")
recognize = recognize_object(model_name,ckpt_path,label_path,test_img_path)
假设我的模型从图像中检测到一只老虎。因此,它在检测到的老虎周围制作了一个边界框,显示预测的 class 名称,置信度百分比为 (TIGER 80%)。我只想在我的边界框上显示预测的 class 名称,而不是像 (TIGER) only
那样制作边界框时的百分比这里有一个简单的解决方案,只需将skip_scores=True
添加到函数visualize_boxes_and_labels_on_image_array
即可。所以函数调用是:
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index = category_index,
use_normalized_coordinates=True,
line_thickness=4,
min_score_thresh=0.80,
skip_scores=True)
我已经在 Kitti 数据集中的图像上进行了测试。没有分数显示!