在推理 tensorflow 2 之前获取元素检测 [英] Get element detections before the inference tensorflow 2
问题描述
这周我在玩"使用 Tensorflow 2,我尝试对象检测,但我不知道如何执行以下操作:
this week i'm "playing" with Tensorflow 2 and i try object detection and i dont know how to do the following:
在教程 TF2 object detection 中,获取一张图像中某些元素的推断,如以下代码所示:
In the tutorial TF2 object detection, get the inference of some elements in one image, as i show in the following code:
image_np = load_image_into_numpy_array(image_path)
input_tensor = tf.convert_to_tensor(image_np)
input_tensor = input_tensor[tf.newaxis, ...]
detections = detect_fn(input_tensor)
但我需要在推理之前检测到元素或区域.我的意思是,建议区域的坐标,但我不知道该怎么做.我尝试拆分过程,一方面是区域提议,另一方面是推理.
But i need to get the elements or regions detected, before the inference. I mean, the coordinates of the proposed regions but i dont know how to do that. I try to split the process, in one hand the region proposal and in the other hand the inference.
我的代码如下:
def make_inference(image_path,counter,image_save):
print('Running inference for {}... '.format(image_path), end='')
image_np = load_image_into_numpy_array(image_path)
input_tensor = tf.convert_to_tensor(image_np)
input_tensor = input_tensor[tf.newaxis, ...]
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
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
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'],
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=200,
min_score_thresh=.5,
agnostic_mode=False)
plt.axis('off')
plt.imshow(image_np_with_detections)
nombre = str(counter)+'.jpg'
plt.savefig('/content/RESULTADOS/'+nombre, dpi=dpi ,bbox_inches='tight')
counter = counter+1
plt.clf()
提前致谢.
推荐答案
我是一名软件工程师,数据科学确实需要实现很多 OOPS(但是 Python 中的 OOPS 是一个笑话 [IMO]),我已经采取了可以自由地绘制一个类,并具有以下功能来获取 List[DetectedObj]
I worked as a software engineer and data science really requires a lot of OOPS implemented hence (however OOPS in Python is a Joke [IMO]), I have taken the liberty to draw out a class instead and have the following function to get a List[DetectedObj]
简单的 POJO 类,用于保存您收到的每个检测.
Simple POJO class to hold every detection you received.
from typing import Dict, Any, Optional, List
import numpy as np
class DetectedObject:
def __init__(self, ymin: float, xmin: float, ymax: float, xmax: float, category: str, score: float):
self.xmin = xmin
self.ymin = ymin
self.xmax = xmax
self.ymax = ymax
self.clazz = clazz
self.score=score
调用以下函数&通过您从detect_fn收到的检测
Call the following function & pass your detections which you received from detect_fn
def get_objects_from_detections(detections: Dict[str, Optional[Any]], categories: Dict[int, Optional[Any]], threshold: float = 0.0) -> List[DetectedObject]:
det_objs = []
bbox_list = detections['detection_boxes'].tolist()
for i, clazz in np.ndenumerate(detections['detection_classes']):
score = detections['detection_scores'][i]
if score > threshold:
clazz_cat = categories[clazz]['name']
row = bbox_list[i[0]]
tiny = DetectedObject(row[0], row[1], row[2], row[3], clazz_cat, score)
det_objs .append(tiny)
return det_objs
这篇关于在推理 tensorflow 2 之前获取元素检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!