fork download
  1. #!/usr/bin/env python3
  2.  
  3. # [NCSDK2 API](https://m...content-available-to-author-only...b.io/ncsdk/ncapi/ncapi2/py_api/readme.html)
  4. from mvnc import mvncapi as mvnc
  5. import numpy
  6. import cv2
  7. from ImageProcessor import ImageProcessor
  8. processor = ImageProcessor()
  9. test_image = './data/photo_6.jpg'
  10. input_image = cv2.imread(test_image)
  11. cropped_input, cropped = processor.preprocess_image(input_image)
  12. # Using NCS Predict
  13. # set the logging level for the NC API
  14. # mvnc.global_set_option(mvnc.GlobalOption.RW_LOG_LEVEL, 0)
  15.  
  16. # get a list of names for all the devices plugged into the system
  17. devices = mvnc.enumerate_devices()
  18. if len(devices) == 0:
  19. print('No devices found')
  20. quit()
  21.  
  22. # get the first NCS device by its name. For this program we will always open the first NCS device.
  23. dev = mvnc.Device(devices[0])
  24.  
  25. # try to open the device. this will throw an exception if someone else has it open already
  26. try:
  27. dev.open()
  28. except:
  29. print("Error - Could not open NCS device.")
  30. quit()
  31.  
  32. # Read a compiled network graph from file (set the graph_filepath correctly for your graph file)
  33. graph_filepath = './graph'
  34. with open("graph", mode='rb') as f:
  35. graphFileBuff = f.read()
  36.  
  37. graph = mvnc.Graph('graph1')
  38.  
  39. # Allocate the graph on the device and create input and output Fifos
  40. in_fifo, out_fifo = graph.allocate_with_fifos(dev, graphFileBuff)
  41.  
  42. # Write the input to the input_fifo buffer and queue an inference in one call
  43. graph.queue_inference_with_fifo_elem(in_fifo, out_fifo, cropped_input.astype('float32'), 'user object')
  44.  
  45. # Read the result to the output Fifo
  46. output, userobj = out_fifo.read_elem()
  47.  
  48. # Deallocate and destroy the fifo and graph handles, close the device, and destroy the device handle
  49. try:
  50. print("0\n")
  51. in_fifo.destroy()
  52. print("1\n")
  53. out_fifo.destroy()
  54. print("2\n")
  55. graph.destroy()
  56. print("3\n")
  57. dev.close()
  58. print("4\n")
  59. dev.destroy()
  60. print("5\n")
  61. except:
  62. print("Error - could not close/destroy Graph/NCS device.")
  63. quit()
  64.  
  65. print("NCS \r\n", output, '\r\nPredicted:',output.argmax())
Runtime error #stdin #stdout #stderr 0.02s 118784KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 4, in <module>
    from mvnc import mvncapi as mvnc
ImportError: No module named mvnc