Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. ######## Picamera Object Detection Using Tensorflow Classifier #########
  2. #
  3. # Author: Evan Juras
  4. # Date: 4/15/18
  5. # Description:
  6. # This program uses a TensorFlow classifier to perform object detection.
  7. # It loads the classifier uses it to perform object detection on a Picamera feed.
  8. # It draws boxes and scores around the objects of interest in each frame from
  9. # the Picamera. It also can be used with a webcam by adding "--usbcam"
  10. # when executing this script from the terminal.
  11.  
  12. ## Some of the code is copied from Google's example at
  13. ## https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb
  14.  
  15. ## and some is copied from Dat Tran's example at
  16. ## https://github.com/datitran/object_detector_app/blob/master/object_detection_app.py
  17.  
  18. ## but I changed it to make it more understandable to me.
  19.  
  20.  
  21. # Import packages
  22. import os
  23. import cv2
  24. import numpy as np
  25. from picamera.array import PiRGBArray
  26. from picamera import PiCamera
  27. import tensorflow as tf
  28. import argparse
  29. import sys
  30. from multiprocessing import Process
  31.  
  32.  
  33.  
  34. # Set up camera constants
  35. IM_WIDTH = 1280
  36. IM_HEIGHT = 720
  37. #IM_WIDTH = 640 Use smaller resolution for
  38. #IM_HEIGHT = 480 slightly faster framerate
  39. object_detected = ''
  40. # Select camera type (if user enters --usbcam when calling this script,
  41. # a USB webcam will be used)
  42. camera_type = 'picamera'
  43. parser = argparse.ArgumentParser()
  44. parser.add_argument('--usbcam', help='Use a USB webcam instead of picamera',
  45. action='store_true')
  46. args = parser.parse_args()
  47. if args.usbcam:
  48. camera_type = 'usb'
  49.  
  50. # This is needed since the working directory is the object_detection folder.
  51. sys.path.append('..')
  52.  
  53. # Import utilites
  54. from utils import label_map_util
  55. from utils import visualization_utils as vis_util
  56.  
  57. # Name of the directory containing the object detection module we're using
  58. MODEL_NAME = 'alphabot_model'
  59.  
  60. # Grab path to current working directory
  61. CWD_PATH = os.getcwd()
  62.  
  63. # Path to frozen detection graph .pb file, which contains the model that is used
  64. # for object detection.
  65. PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,'frozen_inference_graph.pb')
  66.  
  67. # Path to label map file
  68. PATH_TO_LABELS = os.path.join(CWD_PATH,'data','alphabot_labelmap.pbtxt')
  69.  
  70. # Number of classes the object detector can identify
  71. NUM_CLASSES = 4
  72.  
  73. ## Load the label map.
  74. # Label maps map indices to category names, so that when the convolution
  75. # network predicts `5`, we know that this corresponds to `airplane`.
  76. # Here we use internal utility functions, but anything that returns a
  77. # dictionary mapping integers to appropriate string labels would be fine
  78. label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
  79. categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
  80. category_index = label_map_util.create_category_index(categories)
  81.  
  82. # Load the Tensorflow model into memory.
  83. detection_graph = tf.Graph()
  84. with detection_graph.as_default():
  85. od_graph_def = tf.GraphDef()
  86. with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
  87. serialized_graph = fid.read()
  88. od_graph_def.ParseFromString(serialized_graph)
  89. tf.import_graph_def(od_graph_def, name='')
  90.  
  91. sess = tf.Session(graph=detection_graph)
  92.  
  93.  
  94. # Define input and output tensors (i.e. data) for the object detection classifier
  95.  
  96. # Input tensor is the image
  97. image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
  98.  
  99. # Output tensors are the detection boxes, scores, and classes
  100. # Each box represents a part of the image where a particular object was detected
  101. detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
  102.  
  103. # Each score represents level of confidence for each of the objects.
  104. # The score is shown on the result image, together with the class label.
  105. detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
  106. detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
  107.  
  108. # Number of objects detected
  109. num_detections = detection_graph.get_tensor_by_name('num_detections:0')
  110.  
  111. # Initialize frame rate calculation
  112.  
  113.  
  114. # Initialize camera and perform object detection.
  115. # The camera has to be set up and used differently depending on if it's a
  116. # Picamera or USB webcam.
  117.  
  118. # I know this is ugly, but I basically copy+pasted the code for the object
  119. # detection loop twice, and made one work for Picamera and the other work
  120. # for USB.
  121.  
  122. def startRecord():
  123. frame_rate_calc = 1
  124. freq = cv2.getTickFrequency()
  125. font = cv2.FONT_HERSHEY_SIMPLEX
  126. camera = PiCamera()
  127. camera.resolution = (IM_WIDTH, IM_HEIGHT)
  128. camera.framerate = 10
  129. camera.vflip = True
  130. rawCapture = PiRGBArray(camera, size=(IM_WIDTH, IM_HEIGHT))
  131. rawCapture.truncate(0)
  132.  
  133. for frame1 in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
  134. object_detected = "none"
  135. t1 = cv2.getTickCount()
  136.  
  137. # Acquire frame and expand frame dimensions to have shape: [1, None, None, 3]
  138. # i.e. a single-column array, where each item in the column has the pixel RGB value
  139. frame = np.copy(frame1.array)
  140. frame.setflags(write=1)
  141. frame_expanded = np.expand_dims(frame, axis=0)
  142.  
  143. # Perform the actual detection by running the model with the image as input
  144. (boxes, scores, classes, num) = sess.run(
  145. [detection_boxes, detection_scores, detection_classes, num_detections],
  146. feed_dict={image_tensor: frame_expanded})
  147.  
  148. # Draw the results of the detection (aka 'visualize the results')
  149. vis_util.visualize_boxes_and_labels_on_image_array(
  150. frame,
  151. np.squeeze(boxes),
  152. np.squeeze(classes).astype(np.int32),
  153. np.squeeze(scores),
  154. category_index,
  155. use_normalized_coordinates=True,
  156. line_thickness=8,
  157. min_score_thresh=0.40)
  158.  
  159. if classes[0][0] == 1 and scores[0][0] > 0.98:
  160. object_detected = "circle"
  161. elif classes[0][0] == 2 and scores[0][0] > 0.98:
  162. object_detected = "donnut"
  163. elif classes[0][0] == 3 and scores[0][0] > 0.98:
  164. object_detected = "square"
  165. elif classes[0][0] == 4 and scores[0][0] > 0.98:
  166. object_detected = "alphabot"
  167.  
  168. cv2.putText(frame, "FPS: {0:.2f}".format(frame_rate_calc), (30, 50), font, 1, (255, 255, 0), 2, cv2.LINE_AA)
  169.  
  170. # All the results have been drawn on the frame, so it's time to display it.
  171. cv2.imshow('Object detector', frame)
  172.  
  173. t2 = cv2.getTickCount()
  174. time1 = (t2 - t1) / freq
  175. frame_rate_calc = 1 / time1
  176.  
  177. # Press 'q' to quit
  178. if cv2.waitKey(1) == ord('q'):
  179. break
  180.  
  181. rawCapture.truncate(0)
  182. camera.close()
  183.  
  184.  
  185. def computation():
  186. print("OUTSIDE OF CAPTURE")
  187. print(object_detected)
  188. ### Picamera ###
  189. if camera_type == 'picamera':
  190. # Initialize Picamera and grab reference to the raw capture
  191. p1 = Process(target=startRecord())
  192. p2 = Process(target=computation())
  193. p1.start()
  194. p2.start()
  195.  
  196.  
  197. p1.join()
  198. p2.join()
  199.  
  200. ### USB webcam ###
  201. elif camera_type == 'usb':
  202. # Initialize USB webcam feed
  203. camera = cv2.VideoCapture(0)
  204. ret = camera.set(3,IM_WIDTH)
  205. ret = camera.set(4,IM_HEIGHT)
  206.  
  207. while(True):
  208.  
  209. t1 = cv2.getTickCount()
  210.  
  211. # Acquire frame and expand frame dimensions to have shape: [1, None, None, 3]
  212. # i.e. a single-column array, where each item in the column has the pixel RGB value
  213. ret, frame = camera.read()
  214. frame_expanded = np.expand_dims(frame, axis=0)
  215.  
  216. # Perform the actual detection by running the model with the image as input
  217. (boxes, scores, classes, num) = sess.run(
  218. [detection_boxes, detection_scores, detection_classes, num_detections],
  219. feed_dict={image_tensor: frame_expanded})
  220.  
  221. # Draw the results of the detection (aka 'visulaize the results')
  222. vis_util.visualize_boxes_and_labels_on_image_array(
  223. frame,
  224. np.squeeze(boxes),
  225. np.squeeze(classes).astype(np.int32),
  226. np.squeeze(scores),
  227. category_index,
  228. use_normalized_coordinates=True,
  229. line_thickness=8,
  230. min_score_thresh=0.85)
  231.  
  232. cv2.putText(frame,"FPS: {0:.2f}".format(frame_rate_calc),(30,50),font,1,(255,255,0),2,cv2.LINE_AA)
  233.  
  234. # All the results have been drawn on the frame, so it's time to display it.
  235. cv2.imshow('Object detector', frame)
  236.  
  237. t2 = cv2.getTickCount()
  238. time1 = (t2-t1)/freq
  239. frame_rate_calc = 1/time1
  240.  
  241. # Press 'q' to quit
  242. if cv2.waitKey(1) == ord('q'):
  243. break
  244.  
  245. camera.release()
  246.  
  247. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement