Advertisement
Guest User

Untitled

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