在推理tensorflow 2之前获取元素检测



本周我是"播放";使用Tensorflow 2和我尝试对象检测,但我不知道如何做到以下几点:

在TF2对象检测教程中,获取一个图像中某些元素的推断,如我在以下代码中所示:

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)

但在推理之前,我需要得到检测到的元素或区域。我的意思是,拟议区域的坐标,但我不知道如何做到这一点。我试图把这个过程分开,一方面是区域建议,另一方面是推理。

我的代码如下:

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]

简单的POJO类可以容纳您收到的每一个检测。

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 收到的检测


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

最新更新