Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #! /usr/bin/env python3
  2.  
  3. # Copyright(c) 2017 Intel Corporation.
  4. # License: MIT See LICENSE file in root directory.
  5.  
  6.  
  7. from mvnc import mvncapi as mvnc
  8. import numpy as np
  9. import cv2
  10. import sys
  11. import time
  12. import matplotlib.pyplot as plt
  13.  
  14. dim=(300,300)
  15. EXAMPLES_BASE_DIR='../../'
  16. IMAGES_DIR = EXAMPLES_BASE_DIR + 'data/images/'
  17. IMAGE_FULL_PATH = IMAGES_DIR + '1.jpg'
  18.  
  19. # ***************************************************************
  20. # Labels for the classifications for the network.
  21. # ***************************************************************
  22. LABELS = ('background',
  23. 'aeroplane', 'bicycle', 'bird', 'boat',
  24. 'bottle', 'bus', 'car', 'cat', 'chair',
  25. 'cow', 'diningtable', 'dog', 'horse',
  26. 'motorbike', 'person', 'pottedplant',
  27. 'sheep', 'sofa', 'train', 'tvmonitor')
  28.  
  29.  
  30. def run_inference(image_to_classify, ssd_mobilenet_graph):
  31.  
  32. # get a resized version of the image that is the dimensions
  33. # SSD Mobile net expects
  34. resized_image = preprocess_image(image_to_classify)
  35. start=time.time()
  36. # ***************************************************************
  37. # Send the image to the NCS
  38. # ***************************************************************
  39. ssd_mobilenet_graph.LoadTensor(resized_image.astype(np.float16), None)
  40.  
  41. # ***************************************************************
  42. # Get the result from the NCS
  43. # ***************************************************************
  44. detections, userobj = ssd_mobilenet_graph.GetResult()
  45. print (detections.shape)
  46.  
  47. detections = np.reshape(detections,(1,1,201,7))
  48.  
  49.  
  50. # Parse the outputs.
  51. det_label = detections[0,0,:,1]
  52. det_conf = detections[0,0,:,2]
  53. det_xmin = detections[0,0,:,3]
  54. det_ymin = detections[0,0,:,4]
  55. det_xmax = detections[0,0,:,5]
  56. det_ymax = detections[0,0,:,6]
  57.  
  58. # Get detections with confidence higher than 0.6.
  59. top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.05]
  60.  
  61. top_conf = det_conf[top_indices]
  62. top_label_indices = det_label[top_indices].tolist()
  63. #top_labels = get_labelname(labelmap, top_label_indices)
  64. top_xmin = det_xmin[top_indices]
  65. top_ymin = det_ymin[top_indices]
  66. top_xmax = det_xmax[top_indices]
  67. top_ymax = det_ymax[top_indices]
  68. #print('total num boxes: ' + str(num_valid_boxes))
  69. colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
  70. infer_image = cv2.imread(IMAGE_FULL_PATH)
  71.  
  72. plt.imshow(infer_image)
  73. currentAxis = plt.gca()
  74.  
  75. for i in range(top_conf.shape[0]):
  76. xmin = int(round(top_xmin[i] * infer_image.shape[1]))
  77. ymin = int(round(top_ymin[i] * infer_image.shape[0]))
  78. xmax = int(round(top_xmax[i] * infer_image.shape[1]))
  79. ymax = int(round(top_ymax[i] * infer_image.shape[0]))
  80. score = top_conf[i]
  81. label = int(top_label_indices[i])
  82. label_name = "top_labels[i]"
  83. display_txt = '%s: %.2f'%(label_name, score)
  84. coords = (xmin, ymin), xmax-xmin+1, ymax-ymin+1
  85. color = colors[label]
  86. currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
  87. currentAxis.text(xmin, ymin, display_txt, bbox={'facecolor':color, 'alpha':0.5})
  88. plt.show()
  89.  
  90. def preprocess_image(src):
  91.  
  92. # scale the image
  93. NETWORK_WIDTH = 300
  94. NETWORK_HEIGHT = 300
  95. img = cv2.resize(src, (NETWORK_WIDTH, NETWORK_HEIGHT))
  96.  
  97. # adjust values to range between -1.0 and + 1.0
  98. img = img - 127.5
  99. img = img * 0.007843
  100. return img
  101.  
  102.  
  103. # This function is called from the entry point to do
  104. # all the work of the program
  105. def main():
  106. # name of the opencv window
  107. cv_window_name = "SSD MobileNet - hit any key to exit"
  108.  
  109. # Get a list of ALL the sticks that are plugged in
  110. # we need at least one
  111. devices = mvnc.EnumerateDevices()
  112. if len(devices) == 0:
  113. print('No devices found')
  114. quit()
  115.  
  116. # Pick the first stick to run the network
  117. device = mvnc.Device(devices[0])
  118.  
  119. # Open the NCS
  120. device.OpenDevice()
  121.  
  122. # The graph file that was created with the ncsdk compiler
  123. graph_file_name = 'graph'
  124.  
  125. # read in the graph file to memory buffer
  126. with open(graph_file_name, mode='rb') as f:
  127. graph_in_memory = f.read()
  128.  
  129. # create the NCAPI graph instance from the memory buffer containing the graph file.
  130. graph = device.AllocateGraph(graph_in_memory)
  131.  
  132. # read the image to run an inference on from the disk
  133. infer_image = cv2.imread(IMAGE_FULL_PATH)
  134.  
  135. # run a single inference on the image and overwrite the
  136. # boxes and labels
  137. start=time.time()
  138. run_inference(infer_image, graph)
  139. stop=time.time()-start
  140. #print("Time taken to process an image is "+str(stop))
  141.  
  142. # display the results and wait for user to hit a key
  143. #cv2.imshow(cv_window_name, infer_image)
  144. cv2.waitKey(0)
  145.  
  146. # Clean up the graph and the device
  147. graph.DeallocateGraph()
  148. device.CloseDevice()
  149.  
  150.  
  151. # main entry point for program. we'll call main() to do what needs to be done.
  152. if __name__ == "__main__":
  153. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement