detect_objects.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import os
  2. import cv2
  3. import time
  4. import datetime
  5. import ctypes
  6. import logging
  7. import multiprocessing as mp
  8. import threading
  9. from contextlib import closing
  10. import numpy as np
  11. import tensorflow as tf
  12. from object_detection.utils import label_map_util
  13. from object_detection.utils import visualization_utils as vis_util
  14. from flask import Flask, Response, make_response
  15. RTSP_URL = os.getenv('RTSP_URL')
  16. # Path to frozen detection graph. This is the actual model that is used for the object detection.
  17. PATH_TO_CKPT = '/frozen_inference_graph.pb'
  18. # List of the strings that is used to add correct label for each box.
  19. PATH_TO_LABELS = '/label_map.pbtext'
  20. # TODO: make dynamic?
  21. NUM_CLASSES = 90
  22. #REGIONS = "600,0,380:600,600,380:600,1200,380"
  23. REGIONS = os.getenv('REGIONS')
  24. DETECTED_OBJECTS = []
  25. # Loading label map
  26. label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
  27. categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
  28. use_display_name=True)
  29. category_index = label_map_util.create_category_index(categories)
  30. def detect_objects(cropped_frame, sess, detection_graph, region_size, region_x_offset, region_y_offset):
  31. # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  32. image_np_expanded = np.expand_dims(cropped_frame, axis=0)
  33. image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
  34. # Each box represents a part of the image where a particular object was detected.
  35. boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
  36. # Each score represent how level of confidence for each of the objects.
  37. # Score is shown on the result image, together with the class label.
  38. scores = detection_graph.get_tensor_by_name('detection_scores:0')
  39. classes = detection_graph.get_tensor_by_name('detection_classes:0')
  40. num_detections = detection_graph.get_tensor_by_name('num_detections:0')
  41. # Actual detection.
  42. (boxes, scores, classes, num_detections) = sess.run(
  43. [boxes, scores, classes, num_detections],
  44. feed_dict={image_tensor: image_np_expanded})
  45. # build an array of detected objects
  46. objects = []
  47. for index, value in enumerate(classes[0]):
  48. score = scores[0, index]
  49. if score > 0.1:
  50. box = boxes[0, index].tolist()
  51. box[0] = (box[0] * region_size) + region_y_offset
  52. box[1] = (box[1] * region_size) + region_x_offset
  53. box[2] = (box[2] * region_size) + region_y_offset
  54. box[3] = (box[3] * region_size) + region_x_offset
  55. objects += [value, scores[0, index]] + box
  56. # only get the first 10 objects
  57. if len(objects) == 60:
  58. break
  59. return objects
  60. class ObjectParser(threading.Thread):
  61. def __init__(self, object_arrays):
  62. threading.Thread.__init__(self)
  63. self._object_arrays = object_arrays
  64. def run(self):
  65. global DETECTED_OBJECTS
  66. while True:
  67. detected_objects = []
  68. for object_array in self._object_arrays:
  69. object_index = 0
  70. while(object_index < 60 and object_array[object_index] > 0):
  71. object_class = object_array[object_index]
  72. detected_objects.append({
  73. 'name': str(category_index.get(object_class).get('name')),
  74. 'score': object_array[object_index+1],
  75. 'ymin': int(object_array[object_index+2]),
  76. 'xmin': int(object_array[object_index+3]),
  77. 'ymax': int(object_array[object_index+4]),
  78. 'xmax': int(object_array[object_index+5])
  79. })
  80. object_index += 6
  81. DETECTED_OBJECTS = detected_objects
  82. time.sleep(0.01)
  83. def main():
  84. # Parse selected regions
  85. regions = []
  86. for region_string in REGIONS.split(':'):
  87. region_parts = region_string.split(',')
  88. regions.append({
  89. 'size': int(region_parts[0]),
  90. 'x_offset': int(region_parts[1]),
  91. 'y_offset': int(region_parts[2])
  92. })
  93. # capture a single frame and check the frame shape so the correct array
  94. # size can be allocated in memory
  95. video = cv2.VideoCapture(RTSP_URL)
  96. ret, frame = video.read()
  97. if ret:
  98. frame_shape = frame.shape
  99. else:
  100. print("Unable to capture video stream")
  101. exit(1)
  102. video.release()
  103. shared_memory_objects = []
  104. for region in regions:
  105. shared_memory_objects.append({
  106. # create shared value for storing the time the frame was captured
  107. # note: this must be a double even though the value you are storing
  108. # is a float. otherwise it stops updating the value in shared
  109. # memory. probably something to do with the size of the memory block
  110. 'frame_time': mp.Value('d', 0.0),
  111. # create shared array for storing 10 detected objects
  112. 'output_array': mp.Array(ctypes.c_double, 6*10)
  113. })
  114. # compute the flattened array length from the array shape
  115. flat_array_length = frame_shape[0] * frame_shape[1] * frame_shape[2]
  116. # create shared array for storing the full frame image data
  117. shared_arr = mp.Array(ctypes.c_uint16, flat_array_length)
  118. # shape current frame so it can be treated as an image
  119. frame_arr = tonumpyarray(shared_arr).reshape(frame_shape)
  120. capture_process = mp.Process(target=fetch_frames, args=(shared_arr, [obj['frame_time'] for obj in shared_memory_objects], frame_shape))
  121. capture_process.daemon = True
  122. detection_processes = []
  123. for index, region in enumerate(regions):
  124. detection_process = mp.Process(target=process_frames, args=(shared_arr,
  125. shared_memory_objects[index]['output_array'],
  126. shared_memory_objects[index]['frame_time'], frame_shape,
  127. region['size'], region['x_offset'], region['y_offset']))
  128. detection_process.daemon = True
  129. detection_processes.append(detection_process)
  130. object_parser = ObjectParser([obj['output_array'] for obj in shared_memory_objects])
  131. object_parser.start()
  132. capture_process.start()
  133. print("capture_process pid ", capture_process.pid)
  134. for detection_process in detection_processes:
  135. detection_process.start()
  136. print("detection_process pid ", detection_process.pid)
  137. app = Flask(__name__)
  138. @app.route('/')
  139. def index():
  140. # return a multipart response
  141. return Response(imagestream(),
  142. mimetype='multipart/x-mixed-replace; boundary=frame')
  143. def imagestream():
  144. global DETECTED_OBJECTS
  145. while True:
  146. # max out at 5 FPS
  147. time.sleep(0.2)
  148. # make a copy of the current detected objects
  149. detected_objects = DETECTED_OBJECTS.copy()
  150. # make a copy of the current frame
  151. frame = frame_arr.copy()
  152. # convert to RGB for drawing
  153. frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  154. # draw the bounding boxes on the screen
  155. for obj in DETECTED_OBJECTS:
  156. vis_util.draw_bounding_box_on_image_array(frame,
  157. obj['ymin'],
  158. obj['xmin'],
  159. obj['ymax'],
  160. obj['xmax'],
  161. color='red',
  162. thickness=2,
  163. display_str_list=["{}: {}%".format(obj['name'],int(obj['score']*100))],
  164. use_normalized_coordinates=False)
  165. for region in regions:
  166. cv2.rectangle(frame, (region['x_offset'], region['y_offset']),
  167. (region['x_offset']+region['size'], region['y_offset']+region['size']),
  168. (255,255,255), 2)
  169. # convert back to BGR
  170. frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  171. # encode the image into a jpg
  172. ret, jpg = cv2.imencode('.jpg', frame)
  173. yield (b'--frame\r\n'
  174. b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n')
  175. app.run(host='0.0.0.0', debug=False)
  176. capture_process.join()
  177. for detection_process in detection_processes:
  178. detection_process.join()
  179. object_parser.join()
  180. # convert shared memory array into numpy array
  181. def tonumpyarray(mp_arr):
  182. return np.frombuffer(mp_arr.get_obj(), dtype=np.uint16)
  183. # fetch the frames as fast a possible, only decoding the frames when the
  184. # detection_process has consumed the current frame
  185. def fetch_frames(shared_arr, shared_frame_times, frame_shape):
  186. # convert shared memory array into numpy and shape into image array
  187. arr = tonumpyarray(shared_arr).reshape(frame_shape)
  188. # start the video capture
  189. video = cv2.VideoCapture(RTSP_URL)
  190. # keep the buffer small so we minimize old data
  191. video.set(cv2.CAP_PROP_BUFFERSIZE,1)
  192. while True:
  193. # grab the frame, but dont decode it yet
  194. ret = video.grab()
  195. # snapshot the time the frame was grabbed
  196. frame_time = datetime.datetime.now()
  197. if ret:
  198. # if the detection_process is ready for the next frame decode it
  199. # otherwise skip this frame and move onto the next one
  200. if all(shared_frame_time.value == 0.0 for shared_frame_time in shared_frame_times):
  201. # go ahead and decode the current frame
  202. ret, frame = video.retrieve()
  203. if ret:
  204. arr[:] = frame
  205. # signal to the detection_processes by setting the shared_frame_time
  206. for shared_frame_time in shared_frame_times:
  207. shared_frame_time.value = frame_time.timestamp()
  208. else:
  209. # sleep a little to reduce CPU usage
  210. time.sleep(0.01)
  211. video.release()
  212. # do the actual object detection
  213. def process_frames(shared_arr, shared_output_arr, shared_frame_time, frame_shape, region_size, region_x_offset, region_y_offset):
  214. # shape shared input array into frame for processing
  215. arr = tonumpyarray(shared_arr).reshape(frame_shape)
  216. # Load a (frozen) Tensorflow model into memory before the processing loop
  217. detection_graph = tf.Graph()
  218. with detection_graph.as_default():
  219. od_graph_def = tf.GraphDef()
  220. with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
  221. serialized_graph = fid.read()
  222. od_graph_def.ParseFromString(serialized_graph)
  223. tf.import_graph_def(od_graph_def, name='')
  224. sess = tf.Session(graph=detection_graph)
  225. no_frames_available = -1
  226. while True:
  227. # if there isnt a frame ready for processing
  228. if shared_frame_time.value == 0.0:
  229. # save the first time there were no frames available
  230. if no_frames_available == -1:
  231. no_frames_available = datetime.datetime.now().timestamp()
  232. # if there havent been any frames available in 30 seconds,
  233. # sleep to avoid using so much cpu if the camera feed is down
  234. if no_frames_available > 0 and (datetime.datetime.now().timestamp() - no_frames_available) > 30:
  235. time.sleep(1)
  236. print("sleeping because no frames have been available in a while")
  237. else:
  238. # rest a little bit to avoid maxing out the CPU
  239. time.sleep(0.01)
  240. continue
  241. # we got a valid frame, so reset the timer
  242. no_frames_available = -1
  243. # if the frame is more than 0.5 second old, discard it
  244. if (datetime.datetime.now().timestamp() - shared_frame_time.value) > 0.5:
  245. # signal that we need a new frame
  246. shared_frame_time.value = 0.0
  247. # rest a little bit to avoid maxing out the CPU
  248. time.sleep(0.01)
  249. continue
  250. # make a copy of the cropped frame
  251. cropped_frame = arr[region_y_offset:region_y_offset+region_size, region_x_offset:region_x_offset+region_size].copy()
  252. frame_time = shared_frame_time.value
  253. # signal that the frame has been used so a new one will be ready
  254. shared_frame_time.value = 0.0
  255. # convert to RGB
  256. cropped_frame_rgb = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2RGB)
  257. # do the object detection
  258. objects = detect_objects(cropped_frame_rgb, sess, detection_graph, region_size, region_x_offset, region_y_offset)
  259. # copy the detected objects to the output array, filling the array when needed
  260. shared_output_arr[:] = objects + [0.0] * (60-len(objects))
  261. if __name__ == '__main__':
  262. mp.freeze_support()
  263. main()