Advertisement
Guest User

Untitled

a guest
May 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. detection_graph = tf.Graph()
  2. with detection_graph.as_default():
  3.     od_graph_def = tf.GraphDef()
  4.     with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
  5.         serialized_graph = fid.read()
  6.         od_graph_def.ParseFromString(serialized_graph)
  7.         tf.import_graph_def(od_graph_def, name='')
  8.  
  9.     sess = tf.Session(graph=detection_graph)
  10.  
  11. # Define input and output tensors (i.e. data) for the object detection classifier
  12.  
  13. # Input tensor is the image
  14. image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
  15.  
  16. # Output tensors are the detection boxes, scores, and classes
  17. # Each box represents a part of the image where a particular object was detected
  18. detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
  19.  
  20. # Each score represents level of confidence for each of the objects.
  21. # The score is shown on the result image, together with the class label.
  22. detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
  23. detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
  24.  
  25. # Number of objects detected
  26. num_detections = detection_graph.get_tensor_by_name('num_detections:0')
  27.  
  28. # Load image using OpenCV and
  29. # expand image dimensions to have shape: [1, None, None, 3]
  30. # i.e. a single-column array, where each item in the column has the pixel RGB value
  31. image = cv2.imread(PATH_TO_IMAGE)
  32. image_expanded = np.expand_dims(image, axis=0)
  33.  
  34. # Perform the actual detection by running the model with the image as input
  35. (boxes, scores, classes, num) = sess.run(
  36.     [detection_boxes, detection_scores, detection_classes, num_detections],
  37.     feed_dict={image_tensor: image_expanded})
  38.  
  39. # Draw the results of the detection (aka 'visulaize the results')
  40.  
  41. vis_util.visualize_boxes_and_labels_on_image_array(
  42.     image,
  43.     np.squeeze(boxes),
  44.     np.squeeze(classes).astype(np.int32),
  45.     np.squeeze(scores),
  46.     category_index,
  47.     use_normalized_coordinates=True,
  48.     line_thickness=8,
  49.     min_score_thresh=0.60)
  50.  
  51. # All the results have been drawn on image. Now display the image.
  52. cv2.imshow('Object detector', image)
  53.  
  54. # Press any key to close the image
  55. cv2.waitKey(0)
  56.  
  57. # Clean up
  58. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement