detect_objects.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. import os
  2. import cv2
  3. import imutils
  4. import time
  5. import datetime
  6. import ctypes
  7. import logging
  8. import multiprocessing as mp
  9. import threading
  10. import json
  11. from contextlib import closing
  12. import numpy as np
  13. import tensorflow as tf
  14. from object_detection.utils import label_map_util
  15. from object_detection.utils import visualization_utils as vis_util
  16. from flask import Flask, Response, make_response
  17. import paho.mqtt.client as mqtt
  18. RTSP_URL = os.getenv('RTSP_URL')
  19. # Path to frozen detection graph. This is the actual model that is used for the object detection.
  20. PATH_TO_CKPT = '/frozen_inference_graph.pb'
  21. # List of the strings that is used to add correct label for each box.
  22. PATH_TO_LABELS = '/label_map.pbtext'
  23. MQTT_HOST = os.getenv('MQTT_HOST')
  24. MQTT_TOPIC_PREFIX = os.getenv('MQTT_TOPIC_PREFIX')
  25. MQTT_OBJECT_CLASSES = os.getenv('MQTT_OBJECT_CLASSES')
  26. # TODO: make dynamic?
  27. NUM_CLASSES = 90
  28. # REGIONS = "350,0,300,50:400,350,250,50:400,750,250,50"
  29. # REGIONS = "400,350,250,50"
  30. REGIONS = os.getenv('REGIONS')
  31. DETECTED_OBJECTS = []
  32. # Loading label map
  33. label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
  34. categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
  35. use_display_name=True)
  36. category_index = label_map_util.create_category_index(categories)
  37. def detect_objects(cropped_frame, sess, detection_graph, region_size, region_x_offset, region_y_offset, debug):
  38. # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  39. image_np_expanded = np.expand_dims(cropped_frame, axis=0)
  40. image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
  41. # Each box represents a part of the image where a particular object was detected.
  42. boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
  43. # Each score represent how level of confidence for each of the objects.
  44. # Score is shown on the result image, together with the class label.
  45. scores = detection_graph.get_tensor_by_name('detection_scores:0')
  46. classes = detection_graph.get_tensor_by_name('detection_classes:0')
  47. num_detections = detection_graph.get_tensor_by_name('num_detections:0')
  48. # Actual detection.
  49. (boxes, scores, classes, num_detections) = sess.run(
  50. [boxes, scores, classes, num_detections],
  51. feed_dict={image_tensor: image_np_expanded})
  52. if debug:
  53. if len([category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.5]) > 0:
  54. vis_util.visualize_boxes_and_labels_on_image_array(
  55. cropped_frame,
  56. np.squeeze(boxes),
  57. np.squeeze(classes).astype(np.int32),
  58. np.squeeze(scores),
  59. category_index,
  60. use_normalized_coordinates=True,
  61. line_thickness=4)
  62. cv2.imwrite("/lab/debug/obj-{}-{}-{}.jpg".format(region_x_offset, region_y_offset, datetime.datetime.now().timestamp()), cropped_frame)
  63. # build an array of detected objects
  64. objects = []
  65. for index, value in enumerate(classes[0]):
  66. score = scores[0, index]
  67. if score > 0.5:
  68. box = boxes[0, index].tolist()
  69. box[0] = (box[0] * region_size) + region_y_offset
  70. box[1] = (box[1] * region_size) + region_x_offset
  71. box[2] = (box[2] * region_size) + region_y_offset
  72. box[3] = (box[3] * region_size) + region_x_offset
  73. objects += [value, scores[0, index]] + box
  74. # only get the first 10 objects
  75. if len(objects) == 60:
  76. break
  77. return objects
  78. class ObjectParser(threading.Thread):
  79. def __init__(self, object_arrays):
  80. threading.Thread.__init__(self)
  81. self._object_arrays = object_arrays
  82. def run(self):
  83. global DETECTED_OBJECTS
  84. while True:
  85. detected_objects = []
  86. for object_array in self._object_arrays:
  87. object_index = 0
  88. while(object_index < 60 and object_array[object_index] > 0):
  89. object_class = object_array[object_index]
  90. detected_objects.append({
  91. 'name': str(category_index.get(object_class).get('name')),
  92. 'score': object_array[object_index+1],
  93. 'ymin': int(object_array[object_index+2]),
  94. 'xmin': int(object_array[object_index+3]),
  95. 'ymax': int(object_array[object_index+4]),
  96. 'xmax': int(object_array[object_index+5])
  97. })
  98. object_index += 6
  99. DETECTED_OBJECTS = detected_objects
  100. time.sleep(0.1)
  101. class MqttPublisher(threading.Thread):
  102. def __init__(self, host, topic_prefix, object_classes, motion_flags):
  103. threading.Thread.__init__(self)
  104. self.client = mqtt.Client()
  105. self.client.will_set(topic_prefix+'/available', payload='offline', qos=1, retain=True)
  106. self.client.connect(host, 1883, 60)
  107. self.client.loop_start()
  108. self.client.publish(topic_prefix+'/available', 'online', retain=True)
  109. self.topic_prefix = topic_prefix
  110. self.object_classes = object_classes
  111. self.motion_flags = motion_flags
  112. def run(self):
  113. global DETECTED_OBJECTS
  114. last_sent_payload = ""
  115. last_motion = ""
  116. while True:
  117. # initialize the payload
  118. payload = {}
  119. for obj in self.object_classes:
  120. payload[obj] = []
  121. # loop over detected objects and populate
  122. # the payload
  123. detected_objects = DETECTED_OBJECTS.copy()
  124. for obj in detected_objects:
  125. if obj['name'] in self.object_classes:
  126. payload[obj['name']].append(obj)
  127. # send message for objects
  128. new_payload = json.dumps(payload, sort_keys=True)
  129. if new_payload != last_sent_payload:
  130. last_sent_payload = new_payload
  131. self.client.publish(self.topic_prefix+'/objects', new_payload, retain=False)
  132. # send message for motion
  133. motion_status = 'OFF'
  134. if any(obj.value == 1 for obj in self.motion_flags):
  135. motion_status = 'ON'
  136. if motion_status != last_motion:
  137. last_motion = motion_status
  138. self.client.publish(self.topic_prefix+'/motion', motion_status, retain=False)
  139. time.sleep(0.1)
  140. def main():
  141. # Parse selected regions
  142. regions = []
  143. for region_string in REGIONS.split(':'):
  144. region_parts = region_string.split(',')
  145. regions.append({
  146. 'size': int(region_parts[0]),
  147. 'x_offset': int(region_parts[1]),
  148. 'y_offset': int(region_parts[2]),
  149. 'min_object_size': int(region_parts[3]),
  150. # shared value for signaling to the capture process that we are ready for the next frame
  151. # (1 for ready 0 for not ready)
  152. 'ready_for_frame': mp.Value('i', 1),
  153. # shared value for motion detection signal (1 for motion 0 for no motion)
  154. 'motion_detected': mp.Value('i', 0),
  155. # create shared array for storing 10 detected objects
  156. # note: this must be a double even though the value you are storing
  157. # is a float. otherwise it stops updating the value in shared
  158. # memory. probably something to do with the size of the memory block
  159. 'output_array': mp.Array(ctypes.c_double, 6*10)
  160. })
  161. # capture a single frame and check the frame shape so the correct array
  162. # size can be allocated in memory
  163. video = cv2.VideoCapture(RTSP_URL)
  164. ret, frame = video.read()
  165. if ret:
  166. frame_shape = frame.shape
  167. else:
  168. print("Unable to capture video stream")
  169. exit(1)
  170. video.release()
  171. # compute the flattened array length from the array shape
  172. flat_array_length = frame_shape[0] * frame_shape[1] * frame_shape[2]
  173. # create shared array for storing the full frame image data
  174. shared_arr = mp.Array(ctypes.c_uint16, flat_array_length)
  175. # create shared value for storing the frame_time
  176. shared_frame_time = mp.Value('d', 0.0)
  177. # shape current frame so it can be treated as an image
  178. frame_arr = tonumpyarray(shared_arr).reshape(frame_shape)
  179. capture_process = mp.Process(target=fetch_frames, args=(shared_arr,
  180. shared_frame_time, [region['ready_for_frame'] for region in regions], frame_shape))
  181. capture_process.daemon = True
  182. detection_processes = []
  183. for index, region in enumerate(regions):
  184. detection_process = mp.Process(target=process_frames, args=(shared_arr,
  185. region['output_array'],
  186. shared_frame_time,
  187. region['motion_detected'],
  188. frame_shape,
  189. region['size'], region['x_offset'], region['y_offset']))
  190. detection_process.daemon = True
  191. detection_processes.append(detection_process)
  192. motion_processes = []
  193. for index, region in enumerate(regions):
  194. motion_process = mp.Process(target=detect_motion, args=(shared_arr,
  195. shared_frame_time,
  196. region['ready_for_frame'],
  197. region['motion_detected'],
  198. frame_shape,
  199. region['size'], region['x_offset'], region['y_offset'],
  200. region['min_object_size'],
  201. True))
  202. motion_process.daemon = True
  203. motion_processes.append(motion_process)
  204. object_parser = ObjectParser([region['output_array'] for region in regions])
  205. object_parser.start()
  206. mqtt_publisher = MqttPublisher(MQTT_HOST, MQTT_TOPIC_PREFIX,
  207. MQTT_OBJECT_CLASSES.split(','),
  208. [region['motion_detected'] for region in regions])
  209. mqtt_publisher.start()
  210. capture_process.start()
  211. print("capture_process pid ", capture_process.pid)
  212. for detection_process in detection_processes:
  213. detection_process.start()
  214. print("detection_process pid ", detection_process.pid)
  215. for motion_process in motion_processes:
  216. motion_process.start()
  217. print("motion_process pid ", motion_process.pid)
  218. app = Flask(__name__)
  219. @app.route('/')
  220. def index():
  221. # return a multipart response
  222. return Response(imagestream(),
  223. mimetype='multipart/x-mixed-replace; boundary=frame')
  224. def imagestream():
  225. global DETECTED_OBJECTS
  226. while True:
  227. # max out at 5 FPS
  228. time.sleep(0.2)
  229. # make a copy of the current detected objects
  230. detected_objects = DETECTED_OBJECTS.copy()
  231. # make a copy of the current frame
  232. frame = frame_arr.copy()
  233. # convert to RGB for drawing
  234. frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  235. # draw the bounding boxes on the screen
  236. for obj in detected_objects:
  237. vis_util.draw_bounding_box_on_image_array(frame,
  238. obj['ymin'],
  239. obj['xmin'],
  240. obj['ymax'],
  241. obj['xmax'],
  242. color='red',
  243. thickness=2,
  244. display_str_list=["{}: {}%".format(obj['name'],int(obj['score']*100))],
  245. use_normalized_coordinates=False)
  246. for region in regions:
  247. color = (255,255,255)
  248. if region['motion_detected'].value == 1:
  249. color = (0,255,0)
  250. cv2.rectangle(frame, (region['x_offset'], region['y_offset']),
  251. (region['x_offset']+region['size'], region['y_offset']+region['size']),
  252. color, 2)
  253. cv2.putText(frame, datetime.datetime.now().strftime("%H:%M:%S"), (1125, 20),
  254. cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
  255. # convert back to BGR
  256. frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  257. # encode the image into a jpg
  258. ret, jpg = cv2.imencode('.jpg', frame)
  259. yield (b'--frame\r\n'
  260. b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n')
  261. app.run(host='0.0.0.0', debug=False)
  262. capture_process.join()
  263. for detection_process in detection_processes:
  264. detection_process.join()
  265. for motion_process in motion_processes:
  266. motion_process.join()
  267. object_parser.join()
  268. mqtt_publisher.join()
  269. # convert shared memory array into numpy array
  270. def tonumpyarray(mp_arr):
  271. return np.frombuffer(mp_arr.get_obj(), dtype=np.uint16)
  272. # fetch the frames as fast a possible, only decoding the frames when the
  273. # detection_process has consumed the current frame
  274. def fetch_frames(shared_arr, shared_frame_time, ready_for_frame_flags, frame_shape):
  275. # convert shared memory array into numpy and shape into image array
  276. arr = tonumpyarray(shared_arr).reshape(frame_shape)
  277. # start the video capture
  278. video = cv2.VideoCapture(RTSP_URL)
  279. # keep the buffer small so we minimize old data
  280. video.set(cv2.CAP_PROP_BUFFERSIZE,1)
  281. while True:
  282. # grab the frame, but dont decode it yet
  283. ret = video.grab()
  284. # snapshot the time the frame was grabbed
  285. frame_time = datetime.datetime.now()
  286. if ret:
  287. # if the anyone is ready for the next frame decode it
  288. # otherwise skip this frame and move onto the next one
  289. if any(flag.value == 1 for flag in ready_for_frame_flags):
  290. # go ahead and decode the current frame
  291. ret, frame = video.retrieve()
  292. if ret:
  293. arr[:] = frame
  294. shared_frame_time.value = frame_time.timestamp()
  295. # signal to the detection_processes by setting the shared_frame_time
  296. for flag in ready_for_frame_flags:
  297. flag.value = 0
  298. else:
  299. # sleep a little to reduce CPU usage
  300. time.sleep(0.1)
  301. video.release()
  302. # do the actual object detection
  303. def process_frames(shared_arr, shared_output_arr, shared_frame_time, shared_motion, frame_shape, region_size, region_x_offset, region_y_offset):
  304. debug = True
  305. # shape shared input array into frame for processing
  306. arr = tonumpyarray(shared_arr).reshape(frame_shape)
  307. # Load a (frozen) Tensorflow model into memory before the processing loop
  308. detection_graph = tf.Graph()
  309. with detection_graph.as_default():
  310. od_graph_def = tf.GraphDef()
  311. with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
  312. serialized_graph = fid.read()
  313. od_graph_def.ParseFromString(serialized_graph)
  314. tf.import_graph_def(od_graph_def, name='')
  315. sess = tf.Session(graph=detection_graph)
  316. no_frames_available = -1
  317. frame_time = 0.0
  318. while True:
  319. now = datetime.datetime.now().timestamp()
  320. # if there is no motion detected
  321. if shared_motion.value == 0:
  322. time.sleep(0.1)
  323. continue
  324. # if there isnt a new frame ready for processing
  325. if shared_frame_time.value == frame_time:
  326. # save the first time there were no frames available
  327. if no_frames_available == -1:
  328. no_frames_available = now
  329. # if there havent been any frames available in 30 seconds,
  330. # sleep to avoid using so much cpu if the camera feed is down
  331. if no_frames_available > 0 and (now - no_frames_available) > 30:
  332. time.sleep(1)
  333. print("sleeping because no frames have been available in a while")
  334. else:
  335. # rest a little bit to avoid maxing out the CPU
  336. time.sleep(0.1)
  337. continue
  338. # we got a valid frame, so reset the timer
  339. no_frames_available = -1
  340. # if the frame is more than 0.5 second old, ignore it
  341. if (now - shared_frame_time.value) > 0.5:
  342. # rest a little bit to avoid maxing out the CPU
  343. time.sleep(0.1)
  344. continue
  345. # make a copy of the cropped frame
  346. cropped_frame = arr[region_y_offset:region_y_offset+region_size, region_x_offset:region_x_offset+region_size].copy()
  347. frame_time = shared_frame_time.value
  348. # convert to RGB
  349. cropped_frame_rgb = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2RGB)
  350. # do the object detection
  351. objects = detect_objects(cropped_frame_rgb, sess, detection_graph, region_size, region_x_offset, region_y_offset, True)
  352. # copy the detected objects to the output array, filling the array when needed
  353. shared_output_arr[:] = objects + [0.0] * (60-len(objects))
  354. # do the actual motion detection
  355. def detect_motion(shared_arr, shared_frame_time, ready_for_frame, shared_motion, frame_shape, region_size, region_x_offset, region_y_offset, min_motion_area, debug):
  356. # shape shared input array into frame for processing
  357. arr = tonumpyarray(shared_arr).reshape(frame_shape)
  358. no_frames_available = -1
  359. avg_frame = None
  360. last_motion = -1
  361. frame_time = 0.0
  362. motion_frames = 0
  363. while True:
  364. now = datetime.datetime.now().timestamp()
  365. # if it has been long enough since the last motion, clear the flag
  366. if last_motion > 0 and (now - last_motion) > 2:
  367. last_motion = -1
  368. shared_motion.value = 0
  369. # if there isnt a frame ready for processing
  370. if shared_frame_time.value == frame_time:
  371. # save the first time there were no frames available
  372. if no_frames_available == -1:
  373. no_frames_available = now
  374. # if there havent been any frames available in 30 seconds,
  375. # sleep to avoid using so much cpu if the camera feed is down
  376. if no_frames_available > 0 and (now - no_frames_available) > 30:
  377. time.sleep(1)
  378. print("sleeping because no frames have been available in a while")
  379. else:
  380. # rest a little bit to avoid maxing out the CPU
  381. time.sleep(0.1)
  382. if ready_for_frame.value == 0:
  383. ready_for_frame.value = 1
  384. continue
  385. # we got a valid frame, so reset the timer
  386. no_frames_available = -1
  387. # if the frame is more than 0.5 second old, discard it
  388. if (now - shared_frame_time.value) > 0.5:
  389. # signal that we need a new frame
  390. ready_for_frame.value = 1
  391. # rest a little bit to avoid maxing out the CPU
  392. time.sleep(0.1)
  393. continue
  394. # make a copy of the cropped frame
  395. cropped_frame = arr[region_y_offset:region_y_offset+region_size, region_x_offset:region_x_offset+region_size].copy().astype('uint8')
  396. frame_time = shared_frame_time.value
  397. # signal that the frame has been used so a new one will be ready
  398. ready_for_frame.value = 1
  399. # convert to grayscale
  400. gray = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2GRAY)
  401. # apply gaussian blur
  402. gray = cv2.GaussianBlur(gray, (21, 21), 0)
  403. if avg_frame is None:
  404. avg_frame = gray.copy().astype("float")
  405. continue
  406. # look at the delta from the avg_frame
  407. cv2.accumulateWeighted(gray, avg_frame, 0.5)
  408. frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(avg_frame))
  409. thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
  410. # dilate the thresholded image to fill in holes, then find contours
  411. # on thresholded image
  412. thresh = cv2.dilate(thresh, None, iterations=2)
  413. cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
  414. cv2.CHAIN_APPROX_SIMPLE)
  415. cnts = imutils.grab_contours(cnts)
  416. # if there are no contours, there is no motion
  417. if len(cnts) < 1:
  418. motion_frames = 0
  419. continue
  420. motion_found = False
  421. # loop over the contours
  422. for c in cnts:
  423. # if the contour is big enough, count it as motion
  424. contour_area = cv2.contourArea(c)
  425. if contour_area > min_motion_area:
  426. motion_found = True
  427. if debug:
  428. cv2.drawContours(cropped_frame, [c], -1, (0, 255, 0), 2)
  429. x, y, w, h = cv2.boundingRect(c)
  430. cv2.putText(cropped_frame, str(contour_area), (x, y),
  431. cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 100, 0), 2)
  432. else:
  433. break
  434. if motion_found:
  435. motion_frames += 1
  436. # if there have been enough consecutive motion frames, report motion
  437. if motion_frames >= 3:
  438. shared_motion.value = 1
  439. last_motion = now
  440. else:
  441. motion_frames = 0
  442. if debug and motion_frames > 0:
  443. cv2.imwrite("/lab/debug/motion-{}-{}-{}.jpg".format(region_x_offset, region_y_offset, datetime.datetime.now().timestamp()), cropped_frame)
  444. if __name__ == '__main__':
  445. mp.freeze_support()
  446. main()