Guest User

Untitled

a guest
Dec 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import numpy as np
  2. import tensorflow as tf
  3. import cv2 as cv
  4.  
  5. # Read the graph.
  6. with tf.gfile.FastGFile('frozen_inference_graph.pb', 'rb') as f:
  7. graph_def = tf.GraphDef()
  8. graph_def.ParseFromString(f.read())
  9.  
  10. with tf.Session() as sess:
  11. # Restore session
  12. sess.graph.as_default()
  13. tf.import_graph_def(graph_def, name='')
  14.  
  15. # Read and preprocess an image.
  16. img = cv.imread('example.jpg')
  17. rows = img.shape[0]
  18. cols = img.shape[1]
  19. inp = cv.resize(img, (300, 300))
  20. inp = inp[:, :, [2, 1, 0]] # BGR2RGB
  21.  
  22. # Run the model
  23. out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
  24. sess.graph.get_tensor_by_name('detection_scores:0'),
  25. sess.graph.get_tensor_by_name('detection_boxes:0'),
  26. sess.graph.get_tensor_by_name('detection_classes:0')],
  27. feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)})
  28.  
  29. # Visualize detected bounding boxes.
  30. num_detections = int(out[0][0])
  31. for i in range(num_detections):
  32. classId = int(out[3][0][i])
  33. score = float(out[1][0][i])
  34. bbox = [float(v) for v in out[2][0][i]]
  35. if score > 0.3:
  36. x = bbox[1] * cols
  37. y = bbox[0] * rows
  38. right = bbox[3] * cols
  39. bottom = bbox[2] * rows
  40. cv.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=2)
Add Comment
Please, Sign In to add comment