2017-09-16 53 views
1

我想用张量流对象检测API检测框架区域中的对象。我已经分裂帧分成region_1和region_2,但我怎么只在region_1从帧进行检测,只有在REGION1在框架的特定区域检测对象

def detect_objects(image_np, sess, detection_graph): 

    region_1 = image_np[zone1[1]: zone1[3], zone1[0]:zone1[2]] 
    region_2 = image_np[zone2[1]: zone2[3], zone2[0]:zone2[2]] 

    image_np_expanded = np.expand_dims(image_np, axis=0) 
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') 

    boxes = detection_graph.get_tensor_by_name('detection_boxes:0') 

    scores = detection_graph.get_tensor_by_name('detection_scores:0') 
    classes = detection_graph.get_tensor_by_name('detection_classes:0') 
    num_detections = detection_graph.get_tensor_by_name('num_detections:0') 

    (boxes, scores, classes, num_detections) = sess.run(
     [boxes, scores, classes, num_detections], 
     feed_dict={image_tensor: image_np_expanded}) 

    vis_util.visualize_boxes_and_labels_on_image_array(
     image_np, 
     np.squeeze(boxes), 
     np.squeeze(classes).astype(np.int32), 
     np.squeeze(scores), 
     category_index, 
     use_normalized_coordinates=True, 
     line_thickness=1) 
    return image_np 
+0

您可以通过共享链接到TensorFlow的对象检测API来提供一些背景吗? –

回答

1

你要替换你有兴趣在image_np是以前的区域绘制矩形,所以更改

image_np_expanded = np.expand_dims(image_np, axis=0) 

到:

image_np_expanded = np.expand_dims(region_1, axis=0) 

vis_util.visualize_boxes_and_labels_on_image_array(
    image_np, 
    np.squeeze(boxes), 
    np.squeeze(classes).astype(np.int32), 
    np.squeeze(scores), 
    category_index, 
    use_normalized_coordinates=True, 
    line_thickness=1) 
return image_np 

到:

vis_util.visualize_boxes_and_labels_on_image_array(
     region_1, 
     np.squeeze(boxes), 
     np.squeeze(classes).astype(np.int32), 
     np.squeeze(scores), 
     category_index, 
     use_normalized_coordinates=True, 
     line_thickness=1) 
return region_1 

如果你想返回,只有在region_1侧拉箱的原始图像,你必须要么转换盒从region_1的维度坐标回原来的图像尺寸(其是硬给出TF使用中其np.arrays不同的数据类型)或concatenate区域回到一起: 要垂直堆叠(超过IMG2 IMG1):

vis = np.concatenate((img1, img2), axis=0) 

要水平堆叠(IMG1到IMG2的左侧) :

vis = np.concatenate((img1, img2), axis=1)