Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. # Download and create graph
  2. maybe_download_and_extract()
  3. create_graph()
  4.  
  5. # Variables declarations
  6. frame_count=0
  7. score=0
  8. start = time.time()
  9. pygame.mixer.init()
  10. pred=0
  11. last=0
  12. human_str=None
  13. font=cv2.FONT_HERSHEY_TRIPLEX
  14. font_color=(255,255,255)
  15.  
  16. # Init video stream
  17. vs = VideoStream(src=0).start()
  18.  
  19. # Start tensroflow session
  20. with tf.Session() as sess:
  21. softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
  22.  
  23. while True:
  24. frame = vs.read()
  25. frame_count+=1
  26.  
  27. # Only run every 5 frames
  28. if frame_count%5==0:
  29.  
  30. # Save the image as the fist layer of inception is a DecodeJpeg
  31. cv2.imwrite("current_frame.jpg",frame)
  32.  
  33. image_data = tf.gfile.FastGFile("./current_frame.jpg", 'rb').read()
  34. predictions=sess.run(softmax_tensor,{'DecodeJpeg/contents:0':image_data})
  35.  
  36. predictions = np.squeeze(predictions)
  37. node_lookup = NodeLookup()
  38.  
  39. # change n_pred for more predictions
  40. n_pred=1
  41. top_k = predictions.argsort()[-n_pred:][::-1]
  42. for node_id in top_k:
  43. human_str_n = node_lookup.id_to_string(node_id)
  44. score = predictions[node_id]
  45. if score>.5:
  46. # Some manual corrections
  47. if human_str_n=="stethoscope":human_str_n="Headphones"
  48. if human_str_n=="spatula":human_str_n="fork"
  49. if human_str_n=="iPod":human_str_n="iPhone"
  50. human_str=human_str_n
  51.  
  52. lst=human_str.split()
  53. human_str=" ".join(lst[0:2])
  54. human_str_filename=str(lst[0])
  55.  
  56. current= time.time()
  57. fps=frame_count/(current-start)
  58.  
  59. # Speech module
  60. if last>40 and not pygame.mixer.music.get_busy() and human_str==human_str_n:
  61. pred+=1
  62. name=human_str_filename+".mp3"
  63.  
  64. # Only get from google if we dont have it
  65. if not os.path.isfile(name):
  66. tts = gTTS(text="I see a "+human_str, lang='en')
  67. tts.save(name)
  68.  
  69. last=0
  70. pygame.mixer.music.load(name)
  71. pygame.mixer.music.play()
  72.  
  73. # Show info during some time
  74. if last<40 and frame_count>10:
  75. # Change the text position depending on your camera resolution
  76. cv2.putText(frame,human_str, (20,400),font, 1, font_color)
  77. cv2.putText(frame,str(np.round(score,2))+"%",(20,440),font,1,font_color)
  78.  
  79. if frame_count>20:
  80. fps_text="fps: "+str(np.round(fps,2))
  81. cv2.putText(frame, fps_text, (460,460), font, 1, font_color)
  82.  
  83. cv2.imshow("Frame", frame)
  84. last+=1
  85.  
  86.  
  87. # if the 'q' key is pressed, stop the loop
  88. if cv2.waitKey(1) & 0xFF == ord("q"):break
  89.  
  90. # cleanup everything
  91. vs.stop()
  92. cv2.destroyAllWindows()
  93. sess.close()
  94. print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement