daily pastebin goal
39%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import os
  3. import six.moves.urllib as urllib
  4. import sys
  5. import tarfile
  6. import tensorflow as tf
  7. import zipfile
  8.  
  9. from collections import defaultdict
  10. from io import StringIO
  11. from matplotlib import pyplot as plt
  12. from PIL import Image
  13.  
  14. from object_detection.utils import label_map_util
  15. from object_detection.utils import visualization_utils as vis_util
  16.  
  17. PATH_TO_CKPT = 'frozen_graph/frozen_inference_graph.pb'
  18.  
  19. PATH_TO_LABELS = 'object_detection/pascal_label_map.pbtxt'
  20.  
  21. NUM_CLASSES = 7
  22.  
  23. detection_graph = tf.Graph()
  24. with detection_graph.as_default():
  25.   od_graph_def = tf.GraphDef()
  26.   with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
  27.     serialized_graph = fid.read()
  28.     od_graph_def.ParseFromString(serialized_graph)
  29.     tf.import_graph_def(od_graph_def, name='')
  30.  
  31. label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
  32. categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
  33. category_index = label_map_util.create_category_index(categories)
  34.  
  35. def load_image_into_numpy_array(image):
  36.   (im_width, im_height) = image.size
  37.   return np.array(image.getdata()).reshape(
  38.       (im_height, im_width, 3)).astype(np.uint8)
  39.  
  40.  
  41. PATH_TO_TEST_IMAGES_DIR = 'object_detection/test_images/'
  42. TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 2) ]
  43.  
  44. IMAGE_SIZE = (12, 8)
  45.  
  46. with detection_graph.as_default():
  47.   with tf.Session(graph=detection_graph) as sess:
  48.     sess.run(tf.global_variables_initializer())
  49.     img = 1
  50.     for image_path in TEST_IMAGE_PATHS:
  51.       image = Image.open(image_path)
  52.       image_np = load_image_into_numpy_array(image)
  53.       # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  54.       image_np_expanded = np.expand_dims(image_np, axis=0)
  55.       image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
  56.       # Each box represents a part of the image where a particular object was detected.
  57.       boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
  58.       scores = detection_graph.get_tensor_by_name('detection_scores:0')
  59.       classes = detection_graph.get_tensor_by_name('detection_classes:0')
  60.       num_detections = detection_graph.get_tensor_by_name('num_detections:0')
  61.  
  62.       (boxes, scores, classes, num_detections) = sess.run(
  63.           [boxes, scores, classes, num_detections],
  64.           feed_dict={image_tensor: image_np_expanded})
  65.  
  66.       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=8)
  74.       plt.figure(figsize=IMAGE_SIZE)
  75.       plt.imsave('RESULTS/' + str(img) + '.jpg', image_np)
  76.       img += 1
  77.  
  78.       # Return found objects
  79.       print([category_index.get(i) for i in classes[0]])
  80.       print(boxes.shape)
  81.       print(num_detections)
RAW Paste Data
Top