video.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import base64
  2. import copy
  3. import ctypes
  4. import datetime
  5. import itertools
  6. import json
  7. import logging
  8. import multiprocessing as mp
  9. import os
  10. import queue
  11. import subprocess as sp
  12. import signal
  13. import threading
  14. import time
  15. from collections import defaultdict
  16. from setproctitle import setproctitle
  17. from typing import Dict, List
  18. import cv2
  19. import numpy as np
  20. from frigate.config import CameraConfig
  21. from frigate.edgetpu import RemoteObjectDetector
  22. from frigate.log import LogPipe
  23. from frigate.motion import MotionDetector
  24. from frigate.objects import ObjectTracker
  25. from frigate.util import (EventsPerSecond, FrameManager,
  26. SharedMemoryFrameManager, area, calculate_region,
  27. clipped, draw_box_with_label, intersection,
  28. intersection_over_union, listen, yuv_region_2_rgb)
  29. logger = logging.getLogger(__name__)
  30. def filtered(obj, objects_to_track, object_filters, mask=None):
  31. object_name = obj[0]
  32. if not object_name in objects_to_track:
  33. return True
  34. if object_name in object_filters:
  35. obj_settings = object_filters[object_name]
  36. # if the min area is larger than the
  37. # detected object, don't add it to detected objects
  38. if obj_settings.min_area > obj[3]:
  39. return True
  40. # if the detected object is larger than the
  41. # max area, don't add it to detected objects
  42. if obj_settings.max_area < obj[3]:
  43. return True
  44. # if the score is lower than the min_score, skip
  45. if obj_settings.min_score > obj[1]:
  46. return True
  47. # compute the coordinates of the object and make sure
  48. # the location isnt outside the bounds of the image (can happen from rounding)
  49. y_location = min(int(obj[2][3]), len(mask)-1)
  50. x_location = min(int((obj[2][2]-obj[2][0])/2.0)+obj[2][0], len(mask[0])-1)
  51. # if the object is in a masked location, don't add it to detected objects
  52. if (not mask is None) and (mask[y_location][x_location] == 0):
  53. return True
  54. return False
  55. def create_tensor_input(frame, model_shape, region):
  56. cropped_frame = yuv_region_2_rgb(frame, region)
  57. # Resize to 300x300 if needed
  58. if cropped_frame.shape != (model_shape[0], model_shape[1], 3):
  59. cropped_frame = cv2.resize(cropped_frame, dsize=model_shape, interpolation=cv2.INTER_LINEAR)
  60. # Expand dimensions since the model expects images to have shape: [1, height, width, 3]
  61. return np.expand_dims(cropped_frame, axis=0)
  62. def stop_ffmpeg(ffmpeg_process, logger):
  63. logger.info("Terminating the existing ffmpeg process...")
  64. ffmpeg_process.terminate()
  65. try:
  66. logger.info("Waiting for ffmpeg to exit gracefully...")
  67. ffmpeg_process.communicate(timeout=30)
  68. except sp.TimeoutExpired:
  69. logger.info("FFmpeg didnt exit. Force killing...")
  70. ffmpeg_process.kill()
  71. ffmpeg_process.communicate()
  72. ffmpeg_process = None
  73. def start_or_restart_ffmpeg(ffmpeg_cmd, logger, logpipe: LogPipe, frame_size=None, ffmpeg_process=None):
  74. if not ffmpeg_process is None:
  75. stop_ffmpeg(ffmpeg_process, logger)
  76. if frame_size is None:
  77. process = sp.Popen(ffmpeg_cmd, stdout = sp.DEVNULL, stderr=logpipe, stdin = sp.DEVNULL, start_new_session=True)
  78. else:
  79. process = sp.Popen(ffmpeg_cmd, stdout = sp.PIPE, stderr=logpipe, stdin = sp.DEVNULL, bufsize=frame_size*10, start_new_session=True)
  80. return process
  81. def capture_frames(ffmpeg_process, camera_name, frame_shape, frame_manager: FrameManager,
  82. frame_queue, fps:mp.Value, skipped_fps: mp.Value, current_frame: mp.Value):
  83. frame_size = frame_shape[0] * frame_shape[1]
  84. frame_rate = EventsPerSecond()
  85. frame_rate.start()
  86. skipped_eps = EventsPerSecond()
  87. skipped_eps.start()
  88. while True:
  89. fps.value = frame_rate.eps()
  90. skipped_fps = skipped_eps.eps()
  91. current_frame.value = datetime.datetime.now().timestamp()
  92. frame_name = f"{camera_name}{current_frame.value}"
  93. frame_buffer = frame_manager.create(frame_name, frame_size)
  94. try:
  95. frame_buffer[:] = ffmpeg_process.stdout.read(frame_size)
  96. except Exception as e:
  97. logger.info(f"{camera_name}: ffmpeg sent a broken frame. {e}")
  98. if ffmpeg_process.poll() != None:
  99. logger.info(f"{camera_name}: ffmpeg process is not running. exiting capture thread...")
  100. frame_manager.delete(frame_name)
  101. break
  102. continue
  103. frame_rate.update()
  104. # if the queue is full, skip this frame
  105. if frame_queue.full():
  106. skipped_eps.update()
  107. frame_manager.delete(frame_name)
  108. continue
  109. # close the frame
  110. frame_manager.close(frame_name)
  111. # add to the queue
  112. frame_queue.put(current_frame.value)
  113. class CameraWatchdog(threading.Thread):
  114. def __init__(self, camera_name, config, frame_queue, camera_fps, ffmpeg_pid, stop_event):
  115. threading.Thread.__init__(self)
  116. self.logger = logging.getLogger(f"watchdog.{camera_name}")
  117. self.camera_name = camera_name
  118. self.config = config
  119. self.capture_thread = None
  120. self.ffmpeg_detect_process = None
  121. self.logpipe = LogPipe(f"ffmpeg.{self.camera_name}.detect", logging.ERROR)
  122. self.ffmpeg_other_processes = []
  123. self.camera_fps = camera_fps
  124. self.ffmpeg_pid = ffmpeg_pid
  125. self.frame_queue = frame_queue
  126. self.frame_shape = self.config.frame_shape_yuv
  127. self.frame_size = self.frame_shape[0] * self.frame_shape[1]
  128. self.stop_event = stop_event
  129. def run(self):
  130. self.start_ffmpeg_detect()
  131. for c in self.config.ffmpeg_cmds:
  132. if 'detect' in c['roles']:
  133. continue
  134. logpipe = LogPipe(f"ffmpeg.{self.camera_name}.{'_'.join(sorted(c['roles']))}", logging.ERROR)
  135. self.ffmpeg_other_processes.append({
  136. 'cmd': c['cmd'],
  137. 'logpipe': logpipe,
  138. 'process': start_or_restart_ffmpeg(c['cmd'], self.logger, logpipe)
  139. })
  140. time.sleep(10)
  141. while True:
  142. if self.stop_event.is_set():
  143. stop_ffmpeg(self.ffmpeg_detect_process, self.logger)
  144. for p in self.ffmpeg_other_processes:
  145. stop_ffmpeg(p['process'], self.logger)
  146. p['logpipe'].close()
  147. self.logpipe.close()
  148. break
  149. now = datetime.datetime.now().timestamp()
  150. if not self.capture_thread.is_alive():
  151. self.start_ffmpeg_detect()
  152. elif now - self.capture_thread.current_frame.value > 20:
  153. self.logger.info(f"No frames received from {self.camera_name} in 20 seconds. Exiting ffmpeg...")
  154. self.ffmpeg_detect_process.terminate()
  155. try:
  156. self.logger.info("Waiting for ffmpeg to exit gracefully...")
  157. self.ffmpeg_detect_process.communicate(timeout=30)
  158. except sp.TimeoutExpired:
  159. self.logger.info("FFmpeg didnt exit. Force killing...")
  160. self.ffmpeg_detect_process.kill()
  161. self.ffmpeg_detect_process.communicate()
  162. for p in self.ffmpeg_other_processes:
  163. poll = p['process'].poll()
  164. if poll == None:
  165. continue
  166. p['process'] = start_or_restart_ffmpeg(p['cmd'], self.logger, p['logpipe'], ffmpeg_process=p['process'])
  167. # wait a bit before checking again
  168. time.sleep(10)
  169. def start_ffmpeg_detect(self):
  170. ffmpeg_cmd = [c['cmd'] for c in self.config.ffmpeg_cmds if 'detect' in c['roles']][0]
  171. self.ffmpeg_detect_process = start_or_restart_ffmpeg(ffmpeg_cmd, self.logger, self.logpipe, self.frame_size)
  172. self.ffmpeg_pid.value = self.ffmpeg_detect_process.pid
  173. self.capture_thread = CameraCapture(self.camera_name, self.ffmpeg_detect_process, self.frame_shape, self.frame_queue,
  174. self.camera_fps)
  175. self.capture_thread.start()
  176. class CameraCapture(threading.Thread):
  177. def __init__(self, camera_name, ffmpeg_process, frame_shape, frame_queue, fps):
  178. threading.Thread.__init__(self)
  179. self.name = f"capture:{camera_name}"
  180. self.camera_name = camera_name
  181. self.frame_shape = frame_shape
  182. self.frame_queue = frame_queue
  183. self.fps = fps
  184. self.skipped_fps = EventsPerSecond()
  185. self.frame_manager = SharedMemoryFrameManager()
  186. self.ffmpeg_process = ffmpeg_process
  187. self.current_frame = mp.Value('d', 0.0)
  188. self.last_frame = 0
  189. def run(self):
  190. self.skipped_fps.start()
  191. capture_frames(self.ffmpeg_process, self.camera_name, self.frame_shape, self.frame_manager, self.frame_queue,
  192. self.fps, self.skipped_fps, self.current_frame)
  193. def capture_camera(name, config: CameraConfig, process_info):
  194. stop_event = mp.Event()
  195. def receiveSignal(signalNumber, frame):
  196. stop_event.set()
  197. signal.signal(signal.SIGTERM, receiveSignal)
  198. signal.signal(signal.SIGINT, receiveSignal)
  199. frame_queue = process_info['frame_queue']
  200. camera_watchdog = CameraWatchdog(name, config, frame_queue, process_info['camera_fps'], process_info['ffmpeg_pid'], stop_event)
  201. camera_watchdog.start()
  202. camera_watchdog.join()
  203. def track_camera(name, config: CameraConfig, model_shape, detection_queue, result_connection, detected_objects_queue, process_info):
  204. stop_event = mp.Event()
  205. def receiveSignal(signalNumber, frame):
  206. stop_event.set()
  207. signal.signal(signal.SIGTERM, receiveSignal)
  208. signal.signal(signal.SIGINT, receiveSignal)
  209. threading.current_thread().name = f"process:{name}"
  210. setproctitle(f"frigate.process:{name}")
  211. listen()
  212. frame_queue = process_info['frame_queue']
  213. frame_shape = config.frame_shape
  214. objects_to_track = config.objects.track
  215. object_filters = config.objects.filters
  216. mask = config.mask
  217. motion_detector = MotionDetector(frame_shape, mask, config.motion)
  218. object_detector = RemoteObjectDetector(name, '/labelmap.txt', detection_queue, result_connection, model_shape)
  219. object_tracker = ObjectTracker(config.detect)
  220. frame_manager = SharedMemoryFrameManager()
  221. process_frames(name, frame_queue, frame_shape, model_shape, frame_manager, motion_detector, object_detector,
  222. object_tracker, detected_objects_queue, process_info, objects_to_track, object_filters, mask, stop_event)
  223. logger.info(f"{name}: exiting subprocess")
  224. def reduce_boxes(boxes):
  225. if len(boxes) == 0:
  226. return []
  227. reduced_boxes = cv2.groupRectangles([list(b) for b in itertools.chain(boxes, boxes)], 1, 0.2)[0]
  228. return [tuple(b) for b in reduced_boxes]
  229. def detect(object_detector, frame, model_shape, region, objects_to_track, object_filters, mask):
  230. tensor_input = create_tensor_input(frame, model_shape, region)
  231. detections = []
  232. region_detections = object_detector.detect(tensor_input)
  233. for d in region_detections:
  234. box = d[2]
  235. size = region[2]-region[0]
  236. x_min = int((box[1] * size) + region[0])
  237. y_min = int((box[0] * size) + region[1])
  238. x_max = int((box[3] * size) + region[0])
  239. y_max = int((box[2] * size) + region[1])
  240. det = (d[0],
  241. d[1],
  242. (x_min, y_min, x_max, y_max),
  243. (x_max-x_min)*(y_max-y_min),
  244. region)
  245. # apply object filters
  246. if filtered(det, objects_to_track, object_filters, mask):
  247. continue
  248. detections.append(det)
  249. return detections
  250. def process_frames(camera_name: str, frame_queue: mp.Queue, frame_shape, model_shape,
  251. frame_manager: FrameManager, motion_detector: MotionDetector,
  252. object_detector: RemoteObjectDetector, object_tracker: ObjectTracker,
  253. detected_objects_queue: mp.Queue, process_info: Dict,
  254. objects_to_track: List[str], object_filters, mask, stop_event,
  255. exit_on_empty: bool = False):
  256. fps = process_info['process_fps']
  257. detection_fps = process_info['detection_fps']
  258. current_frame_time = process_info['detection_frame']
  259. fps_tracker = EventsPerSecond()
  260. fps_tracker.start()
  261. while True:
  262. if stop_event.is_set():
  263. break
  264. if exit_on_empty and frame_queue.empty():
  265. logger.info(f"Exiting track_objects...")
  266. break
  267. try:
  268. frame_time = frame_queue.get(True, 10)
  269. except queue.Empty:
  270. continue
  271. current_frame_time.value = frame_time
  272. frame = frame_manager.get(f"{camera_name}{frame_time}", (frame_shape[0]*3//2, frame_shape[1]))
  273. if frame is None:
  274. logger.info(f"{camera_name}: frame {frame_time} is not in memory store.")
  275. continue
  276. # look for motion
  277. motion_boxes = motion_detector.detect(frame)
  278. tracked_object_boxes = [obj['box'] for obj in object_tracker.tracked_objects.values()]
  279. # combine motion boxes with known locations of existing objects
  280. combined_boxes = reduce_boxes(motion_boxes + tracked_object_boxes)
  281. # compute regions
  282. regions = [calculate_region(frame_shape, a[0], a[1], a[2], a[3], 1.2)
  283. for a in combined_boxes]
  284. # combine overlapping regions
  285. combined_regions = reduce_boxes(regions)
  286. # re-compute regions
  287. regions = [calculate_region(frame_shape, a[0], a[1], a[2], a[3], 1.0)
  288. for a in combined_regions]
  289. # resize regions and detect
  290. detections = []
  291. for region in regions:
  292. detections.extend(detect(object_detector, frame, model_shape, region, objects_to_track, object_filters, mask))
  293. #########
  294. # merge objects, check for clipped objects and look again up to 4 times
  295. #########
  296. refining = True
  297. refine_count = 0
  298. while refining and refine_count < 4:
  299. refining = False
  300. # group by name
  301. detected_object_groups = defaultdict(lambda: [])
  302. for detection in detections:
  303. detected_object_groups[detection[0]].append(detection)
  304. selected_objects = []
  305. for group in detected_object_groups.values():
  306. # apply non-maxima suppression to suppress weak, overlapping bounding boxes
  307. boxes = [(o[2][0], o[2][1], o[2][2]-o[2][0], o[2][3]-o[2][1])
  308. for o in group]
  309. confidences = [o[1] for o in group]
  310. idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
  311. for index in idxs:
  312. obj = group[index[0]]
  313. if clipped(obj, frame_shape):
  314. box = obj[2]
  315. # calculate a new region that will hopefully get the entire object
  316. region = calculate_region(frame_shape,
  317. box[0], box[1],
  318. box[2], box[3])
  319. regions.append(region)
  320. selected_objects.extend(detect(object_detector, frame, model_shape, region, objects_to_track, object_filters, mask))
  321. refining = True
  322. else:
  323. selected_objects.append(obj)
  324. # set the detections list to only include top, complete objects
  325. # and new detections
  326. detections = selected_objects
  327. if refining:
  328. refine_count += 1
  329. # now that we have refined our detections, we need to track objects
  330. object_tracker.match_and_update(frame_time, detections)
  331. # add to the queue if not full
  332. if(detected_objects_queue.full()):
  333. frame_manager.delete(f"{camera_name}{frame_time}")
  334. continue
  335. else:
  336. fps_tracker.update()
  337. fps.value = fps_tracker.eps()
  338. detected_objects_queue.put((camera_name, frame_time, object_tracker.tracked_objects, motion_boxes, regions))
  339. detection_fps.value = object_detector.fps.eps()
  340. frame_manager.close(f"{camera_name}{frame_time}")