|
| 1 | +""" |
| 2 | +This is adapted from Tensorflow (https://github.com/tensorflow/models/tree/master/research/object_detection); |
| 3 | +Save this code under the directory `models/research/object_detection/` |
| 4 | +
|
| 5 | +To use, run: |
| 6 | +python tf_od_predict.py --model_name=building_od_ssd \ |
| 7 | + --path_to_label=data/building_od.pbtxt \ |
| 8 | + --test_image_path=test_images |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +from os import makedirs, path as op |
| 13 | +import sys |
| 14 | +import glob |
| 15 | +import six.moves.urllib as urllib |
| 16 | +import tensorflow as tf |
| 17 | +import tarfile |
| 18 | + |
| 19 | +from io import StringIO |
| 20 | +import zipfile |
| 21 | +import numpy as np |
| 22 | +from collections import defaultdict |
| 23 | +from matplotlib import pyplot as plt |
| 24 | +from PIL import ImageDraw, Image |
| 25 | + |
| 26 | +sys.path.append("..") |
| 27 | + |
| 28 | +from utils import label_map_util |
| 29 | +from utils import visualization_utils as vis_util |
| 30 | + |
| 31 | +flags = tf.app.flags |
| 32 | +flags.DEFINE_string('model_name', '', 'Path to frozen detection graph') |
| 33 | +flags.DEFINE_string('path_to_label', '', 'Path to label file') |
| 34 | +flags.DEFINE_string('test_image_path', '', 'Path to test imgs and output diractory') |
| 35 | +FLAGS = flags.FLAGS |
| 36 | + |
| 37 | +def load_image_into_numpy_array(image): |
| 38 | + (im_width, im_height) = image.size |
| 39 | + return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8) |
| 40 | + |
| 41 | +def tf_od_pred(): |
| 42 | + with detection_graph.as_default(): |
| 43 | + with tf.Session(graph=detection_graph) as sess: |
| 44 | + # Definite input and output Tensors for detection_graph |
| 45 | + image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') |
| 46 | + # Each box represents a part of the image where a particular object was detected. |
| 47 | + detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') |
| 48 | + # Each score represent how level of confidence for each of the objects. |
| 49 | + # Score is shown on the result image, together with the class label. |
| 50 | + detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') |
| 51 | + detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') |
| 52 | + num_detections = detection_graph.get_tensor_by_name('num_detections:0') |
| 53 | + for image_path in test_imgs: |
| 54 | + image = Image.open(image_path) |
| 55 | + image_np = load_image_into_numpy_array(image) |
| 56 | + # the array based representation of the image will be used later in order to prepare the |
| 57 | + # result image with boxes and labels on it. |
| 58 | + # Expand dimensions since the model expects images to have shape: [1, None, None, 3] |
| 59 | + image_np_expanded = np.expand_dims(image_np, axis=0) |
| 60 | + # Actual detection. |
| 61 | + (boxes, scores, classes, num) = sess.run( |
| 62 | + [detection_boxes, detection_scores, detection_classes, num_detections], |
| 63 | + feed_dict={image_tensor: image_np_expanded}) |
| 64 | + # draw_bounding_box_on_image(image, boxes, ) |
| 65 | + # Visualization of the results of a detection. |
| 66 | + vis_image = vis_util.visualize_boxes_and_labels_on_image_array( |
| 67 | + image_np, |
| 68 | + np.squeeze(boxes), |
| 69 | + np.squeeze(classes).astype(np.int32), |
| 70 | + np.squeeze(scores), |
| 71 | + category_index, |
| 72 | + use_normalized_coordinates=True, |
| 73 | + line_thickness=1) |
| 74 | + print("{} boxes in {} image tile!".format(len(boxes), image_path)) |
| 75 | + image_pil = Image.fromarray(np.uint8(vis_image)).convert('RGB') |
| 76 | + with tf.gfile.Open(image_path, 'w') as fid: |
| 77 | + image_pil.save(fid, 'PNG') |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +if __name__ =='__main__': |
| 82 | + # load your own trained model inference graph. This inference graph was generated from |
| 83 | + # export_inference_graph.py under model directory, see `models/research/object_detection/` |
| 84 | + model_name = op.join(os.getcwd(), FLAGS.model_name) |
| 85 | + # Path to frozen detection graph. |
| 86 | + path_to_ckpt = op.join(model_name, 'frozen_inference_graph.pb') |
| 87 | + # Path to the label file |
| 88 | + path_to_label = op.join(os.getcwd(), FLAGS.path_to_label) |
| 89 | + #only train on buildings |
| 90 | + num_classes = 1 |
| 91 | + #Directory to test images path |
| 92 | + test_image_path = op.join(os.getcwd(), FLAGS.test_image_path) |
| 93 | + test_imgs = glob.glob(test_image_path + "/*.jpg") |
| 94 | + |
| 95 | + ############ |
| 96 | + #Load the frozen tensorflow model |
| 97 | + ############# |
| 98 | + |
| 99 | + detection_graph = tf.Graph() |
| 100 | + with detection_graph.as_default(): |
| 101 | + od_graph_def = tf.GraphDef() |
| 102 | + with tf.gfile.GFile(path_to_ckpt, 'rb') as fid: |
| 103 | + serialized_graph = fid.read() |
| 104 | + od_graph_def.ParseFromString(serialized_graph) |
| 105 | + tf.import_graph_def(od_graph_def, name='') |
| 106 | + |
| 107 | + ############ |
| 108 | + #Load the label file |
| 109 | + ############# |
| 110 | + label_map = label_map_util.load_labelmap(path_to_label) |
| 111 | + categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=num_classes, use_display_name=True) |
| 112 | + category_index = label_map_util.create_category_index(categories) |
| 113 | + tf_od_pred() |
0 commit comments