http.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import base64
  2. from collections import OrderedDict
  3. from datetime import datetime, timedelta
  4. import json
  5. import glob
  6. import logging
  7. import os
  8. import re
  9. import time
  10. from functools import reduce
  11. from pathlib import Path
  12. import cv2
  13. import numpy as np
  14. from flask import (
  15. Blueprint,
  16. Flask,
  17. Response,
  18. current_app,
  19. jsonify,
  20. make_response,
  21. request,
  22. )
  23. from peewee import SqliteDatabase, operator, fn, DoesNotExist, Value
  24. from playhouse.shortcuts import model_to_dict
  25. from frigate.const import CLIPS_DIR, RECORD_DIR
  26. from frigate.models import Event, Recordings
  27. from frigate.stats import stats_snapshot
  28. from frigate.util import calculate_region
  29. from frigate.version import VERSION
  30. logger = logging.getLogger(__name__)
  31. bp = Blueprint("frigate", __name__)
  32. def create_app(
  33. frigate_config,
  34. database: SqliteDatabase,
  35. stats_tracking,
  36. detected_frames_processor,
  37. ):
  38. app = Flask(__name__)
  39. @app.before_request
  40. def _db_connect():
  41. database.connect()
  42. @app.teardown_request
  43. def _db_close(exc):
  44. if not database.is_closed():
  45. database.close()
  46. app.frigate_config = frigate_config
  47. app.stats_tracking = stats_tracking
  48. app.detected_frames_processor = detected_frames_processor
  49. app.register_blueprint(bp)
  50. return app
  51. @bp.route("/")
  52. def is_healthy():
  53. return "Frigate is running. Alive and healthy!"
  54. @bp.route("/events/summary")
  55. def events_summary():
  56. has_clip = request.args.get("has_clip", type=int)
  57. has_snapshot = request.args.get("has_snapshot", type=int)
  58. clauses = []
  59. if not has_clip is None:
  60. clauses.append((Event.has_clip == has_clip))
  61. if not has_snapshot is None:
  62. clauses.append((Event.has_snapshot == has_snapshot))
  63. if len(clauses) == 0:
  64. clauses.append((True))
  65. groups = (
  66. Event.select(
  67. Event.camera,
  68. Event.label,
  69. fn.strftime(
  70. "%Y-%m-%d", fn.datetime(Event.start_time, "unixepoch", "localtime")
  71. ).alias("day"),
  72. Event.zones,
  73. fn.COUNT(Event.id).alias("count"),
  74. )
  75. .where(reduce(operator.and_, clauses))
  76. .group_by(
  77. Event.camera,
  78. Event.label,
  79. fn.strftime(
  80. "%Y-%m-%d", fn.datetime(Event.start_time, "unixepoch", "localtime")
  81. ),
  82. Event.zones,
  83. )
  84. )
  85. return jsonify([e for e in groups.dicts()])
  86. @bp.route("/events/<id>", methods=("GET",))
  87. def event(id):
  88. try:
  89. return model_to_dict(Event.get(Event.id == id))
  90. except DoesNotExist:
  91. return "Event not found", 404
  92. @bp.route("/events/<id>", methods=("DELETE",))
  93. def delete_event(id):
  94. try:
  95. event = Event.get(Event.id == id)
  96. except DoesNotExist:
  97. return make_response(
  98. jsonify({"success": False, "message": "Event" + id + " not found"}), 404
  99. )
  100. media_name = f"{event.camera}-{event.id}"
  101. if event.has_snapshot:
  102. media = Path(f"{os.path.join(CLIPS_DIR, media_name)}.jpg")
  103. media.unlink(missing_ok=True)
  104. if event.has_clip:
  105. media = Path(f"{os.path.join(CLIPS_DIR, media_name)}.mp4")
  106. media.unlink(missing_ok=True)
  107. event.delete_instance()
  108. return make_response(
  109. jsonify({"success": True, "message": "Event" + id + " deleted"}), 200
  110. )
  111. @bp.route("/events/<id>/thumbnail.jpg")
  112. def event_thumbnail(id):
  113. format = request.args.get("format", "ios")
  114. thumbnail_bytes = None
  115. try:
  116. event = Event.get(Event.id == id)
  117. thumbnail_bytes = base64.b64decode(event.thumbnail)
  118. except DoesNotExist:
  119. # see if the object is currently being tracked
  120. try:
  121. camera_states = current_app.detected_frames_processor.camera_states.values()
  122. for camera_state in camera_states:
  123. if id in camera_state.tracked_objects:
  124. tracked_obj = camera_state.tracked_objects.get(id)
  125. if not tracked_obj is None:
  126. thumbnail_bytes = tracked_obj.get_thumbnail()
  127. except:
  128. return "Event not found", 404
  129. if thumbnail_bytes is None:
  130. return "Event not found", 404
  131. # android notifications prefer a 2:1 ratio
  132. if format == "android":
  133. jpg_as_np = np.frombuffer(thumbnail_bytes, dtype=np.uint8)
  134. img = cv2.imdecode(jpg_as_np, flags=1)
  135. thumbnail = cv2.copyMakeBorder(
  136. img,
  137. 0,
  138. 0,
  139. int(img.shape[1] * 0.5),
  140. int(img.shape[1] * 0.5),
  141. cv2.BORDER_CONSTANT,
  142. (0, 0, 0),
  143. )
  144. ret, jpg = cv2.imencode(".jpg", thumbnail, [int(cv2.IMWRITE_JPEG_QUALITY), 70])
  145. thumbnail_bytes = jpg.tobytes()
  146. response = make_response(thumbnail_bytes)
  147. response.headers["Content-Type"] = "image/jpg"
  148. return response
  149. @bp.route("/events/<id>/snapshot.jpg")
  150. def event_snapshot(id):
  151. jpg_bytes = None
  152. try:
  153. event = Event.get(Event.id == id)
  154. if not event.has_snapshot:
  155. return "Snapshot not available", 404
  156. # read snapshot from disk
  157. with open(
  158. os.path.join(CLIPS_DIR, f"{event.camera}-{id}.jpg"), "rb"
  159. ) as image_file:
  160. jpg_bytes = image_file.read()
  161. except DoesNotExist:
  162. # see if the object is currently being tracked
  163. try:
  164. camera_states = current_app.detected_frames_processor.camera_states.values()
  165. for camera_state in camera_states:
  166. if id in camera_state.tracked_objects:
  167. tracked_obj = camera_state.tracked_objects.get(id)
  168. if not tracked_obj is None:
  169. jpg_bytes = tracked_obj.get_jpg_bytes(
  170. timestamp=request.args.get("timestamp", type=int),
  171. bounding_box=request.args.get("bbox", type=int),
  172. crop=request.args.get("crop", type=int),
  173. height=request.args.get("h", type=int),
  174. )
  175. except:
  176. return "Event not found", 404
  177. except:
  178. return "Event not found", 404
  179. response = make_response(jpg_bytes)
  180. response.headers["Content-Type"] = "image/jpg"
  181. return response
  182. @bp.route("/events")
  183. def events():
  184. limit = request.args.get("limit", 100)
  185. camera = request.args.get("camera")
  186. label = request.args.get("label")
  187. zone = request.args.get("zone")
  188. after = request.args.get("after", type=float)
  189. before = request.args.get("before", type=float)
  190. has_clip = request.args.get("has_clip", type=int)
  191. has_snapshot = request.args.get("has_snapshot", type=int)
  192. include_thumbnails = request.args.get("include_thumbnails", default=1, type=int)
  193. clauses = []
  194. excluded_fields = []
  195. if camera:
  196. clauses.append((Event.camera == camera))
  197. if label:
  198. clauses.append((Event.label == label))
  199. if zone:
  200. clauses.append((Event.zones.cast("text") % f'*"{zone}"*'))
  201. if after:
  202. clauses.append((Event.start_time >= after))
  203. if before:
  204. clauses.append((Event.start_time <= before))
  205. if not has_clip is None:
  206. clauses.append((Event.has_clip == has_clip))
  207. if not has_snapshot is None:
  208. clauses.append((Event.has_snapshot == has_snapshot))
  209. if not include_thumbnails:
  210. excluded_fields.append(Event.thumbnail)
  211. if len(clauses) == 0:
  212. clauses.append((True))
  213. events = (
  214. Event.select()
  215. .where(reduce(operator.and_, clauses))
  216. .order_by(Event.start_time.desc())
  217. .limit(limit)
  218. )
  219. return jsonify([model_to_dict(e, exclude=excluded_fields) for e in events])
  220. @bp.route("/config")
  221. def config():
  222. return jsonify(current_app.frigate_config.to_dict())
  223. @bp.route("/version")
  224. def version():
  225. return VERSION
  226. @bp.route("/stats")
  227. def stats():
  228. stats = stats_snapshot(current_app.stats_tracking)
  229. return jsonify(stats)
  230. @bp.route("/<camera_name>/<label>/best.jpg")
  231. def best(camera_name, label):
  232. if camera_name in current_app.frigate_config.cameras:
  233. best_object = current_app.detected_frames_processor.get_best(camera_name, label)
  234. best_frame = best_object.get("frame")
  235. if best_frame is None:
  236. best_frame = np.zeros((720, 1280, 3), np.uint8)
  237. else:
  238. best_frame = cv2.cvtColor(best_frame, cv2.COLOR_YUV2BGR_I420)
  239. crop = bool(request.args.get("crop", 0, type=int))
  240. if crop:
  241. box = best_object.get("box", (0, 0, 300, 300))
  242. region = calculate_region(
  243. best_frame.shape, box[0], box[1], box[2], box[3], 1.1
  244. )
  245. best_frame = best_frame[region[1] : region[3], region[0] : region[2]]
  246. height = int(request.args.get("h", str(best_frame.shape[0])))
  247. width = int(height * best_frame.shape[1] / best_frame.shape[0])
  248. best_frame = cv2.resize(
  249. best_frame, dsize=(width, height), interpolation=cv2.INTER_AREA
  250. )
  251. ret, jpg = cv2.imencode(".jpg", best_frame, [int(cv2.IMWRITE_JPEG_QUALITY), 70])
  252. response = make_response(jpg.tobytes())
  253. response.headers["Content-Type"] = "image/jpg"
  254. return response
  255. else:
  256. return "Camera named {} not found".format(camera_name), 404
  257. @bp.route("/<camera_name>")
  258. def mjpeg_feed(camera_name):
  259. fps = int(request.args.get("fps", "3"))
  260. height = int(request.args.get("h", "360"))
  261. draw_options = {
  262. "bounding_boxes": request.args.get("bbox", type=int),
  263. "timestamp": request.args.get("timestamp", type=int),
  264. "zones": request.args.get("zones", type=int),
  265. "mask": request.args.get("mask", type=int),
  266. "motion_boxes": request.args.get("motion", type=int),
  267. "regions": request.args.get("regions", type=int),
  268. }
  269. if camera_name in current_app.frigate_config.cameras:
  270. # return a multipart response
  271. return Response(
  272. imagestream(
  273. current_app.detected_frames_processor,
  274. camera_name,
  275. fps,
  276. height,
  277. draw_options,
  278. ),
  279. mimetype="multipart/x-mixed-replace; boundary=frame",
  280. )
  281. else:
  282. return "Camera named {} not found".format(camera_name), 404
  283. @bp.route("/<camera_name>/latest.jpg")
  284. def latest_frame(camera_name):
  285. draw_options = {
  286. "bounding_boxes": request.args.get("bbox", type=int),
  287. "timestamp": request.args.get("timestamp", type=int),
  288. "zones": request.args.get("zones", type=int),
  289. "mask": request.args.get("mask", type=int),
  290. "motion_boxes": request.args.get("motion", type=int),
  291. "regions": request.args.get("regions", type=int),
  292. }
  293. if camera_name in current_app.frigate_config.cameras:
  294. # max out at specified FPS
  295. frame = current_app.detected_frames_processor.get_current_frame(
  296. camera_name, draw_options
  297. )
  298. if frame is None:
  299. frame = np.zeros((720, 1280, 3), np.uint8)
  300. height = int(request.args.get("h", str(frame.shape[0])))
  301. width = int(height * frame.shape[1] / frame.shape[0])
  302. frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_AREA)
  303. ret, jpg = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 70])
  304. response = make_response(jpg.tobytes())
  305. response.headers["Content-Type"] = "image/jpg"
  306. return response
  307. else:
  308. return "Camera named {} not found".format(camera_name), 404
  309. @bp.route("/<camera_name>/recordings")
  310. def recordings(camera_name):
  311. dates = OrderedDict()
  312. # Retrieve all recordings for this camera
  313. recordings = (
  314. Recordings.select()
  315. .where(Recordings.camera == camera_name)
  316. .order_by(Recordings.start_time.asc())
  317. )
  318. last_end = 0
  319. recording: Recordings
  320. for recording in recordings:
  321. date = datetime.fromtimestamp(recording.start_time)
  322. key = date.strftime("%Y-%m-%d")
  323. hour = date.strftime("%H")
  324. # Create Day Record
  325. if key not in dates:
  326. dates[key] = OrderedDict()
  327. # Create Hour Record
  328. if hour not in dates[key]:
  329. dates[key][hour] = {"delay": {}, "events": []}
  330. # Check for delay
  331. the_hour = datetime.strptime(f"{key} {hour}", "%Y-%m-%d %H").timestamp()
  332. # diff current recording start time and the greater of the previous end time or top of the hour
  333. diff = recording.start_time - max(last_end, the_hour)
  334. # Determine seconds into recording
  335. seconds = 0
  336. if datetime.fromtimestamp(last_end).strftime("%H") == hour:
  337. seconds = int(last_end - the_hour)
  338. # Determine the delay
  339. delay = min(int(diff), 3600 - seconds)
  340. if delay > 1:
  341. # Add an offset for any delay greater than a second
  342. dates[key][hour]["delay"][seconds] = delay
  343. last_end = recording.end_time
  344. # Packing intervals to return all events with same label and overlapping times as one row.
  345. # See: https://blogs.solidq.com/en/sqlserver/packing-intervals/
  346. events = Event.raw(
  347. """WITH C1 AS
  348. (
  349. SELECT id, label, camera, top_score, start_time AS ts, +1 AS type, 1 AS sub
  350. FROM event
  351. WHERE camera = ?
  352. UNION ALL
  353. SELECT id, label, camera, top_score, end_time + 15 AS ts, -1 AS type, 0 AS sub
  354. FROM event
  355. WHERE camera = ?
  356. ),
  357. C2 AS
  358. (
  359. SELECT C1.*,
  360. SUM(type) OVER(PARTITION BY label ORDER BY ts, type DESC
  361. ROWS BETWEEN UNBOUNDED PRECEDING
  362. AND CURRENT ROW) - sub AS cnt
  363. FROM C1
  364. ),
  365. C3 AS
  366. (
  367. SELECT id, label, camera, top_score, ts,
  368. (ROW_NUMBER() OVER(PARTITION BY label ORDER BY ts) - 1) / 2 + 1
  369. AS grpnum
  370. FROM C2
  371. WHERE cnt = 0
  372. )
  373. SELECT MIN(id) as id, label, camera, MAX(top_score) as top_score, MIN(ts) AS start_time, max(ts) AS end_time
  374. FROM C3
  375. GROUP BY label, grpnum
  376. ORDER BY start_time;""",
  377. camera_name,
  378. camera_name,
  379. )
  380. event: Event
  381. for event in events:
  382. date = datetime.fromtimestamp(event.start_time)
  383. key = date.strftime("%Y-%m-%d")
  384. hour = date.strftime("%H")
  385. if key in dates and hour in dates[key]:
  386. dates[key][hour]["events"].append(
  387. model_to_dict(
  388. event,
  389. exclude=[
  390. Event.false_positive,
  391. Event.zones,
  392. Event.thumbnail,
  393. Event.has_clip,
  394. Event.has_snapshot,
  395. ],
  396. )
  397. )
  398. return jsonify(
  399. [
  400. {
  401. "date": date,
  402. "events": sum([len(value["events"]) for value in hours.values()]),
  403. "recordings": [
  404. {"hour": hour, "delay": value["delay"], "events": value["events"]}
  405. for hour, value in hours.items()
  406. ],
  407. }
  408. for date, hours in dates.items()
  409. ]
  410. )
  411. @bp.route("/vod/<year_month>/<day>/<hour>/<camera>")
  412. def vod(year_month, day, hour, camera):
  413. start_date = datetime.strptime(f"{year_month}-{day} {hour}", "%Y-%m-%d %H")
  414. end_date = start_date + timedelta(hours=1) - timedelta(milliseconds=1)
  415. start_ts = start_date.timestamp()
  416. end_ts = end_date.timestamp()
  417. # Select all recordings where either the start or end dates fall in the requested hour
  418. recordings = (
  419. Recordings.select()
  420. .where(
  421. (Recordings.start_time.between(start_ts, end_ts))
  422. | (Recordings.end_time.between(start_ts, end_ts))
  423. )
  424. .where(Recordings.camera == camera)
  425. .order_by(Recordings.start_time.asc())
  426. )
  427. clips = []
  428. durations = []
  429. recording: Recordings
  430. for recording in recordings:
  431. clip = {"type": "source", "path": recording.path}
  432. duration = int(recording.duration * 1000)
  433. # Determine if offset is needed for first clip
  434. if recording.start_time < start_ts:
  435. offset = int((start_ts - recording.start_time) * 1000)
  436. clip["clipFrom"] = offset
  437. duration -= offset
  438. # Determine if we need to end the last clip early
  439. if recording.end_time > end_ts:
  440. duration -= int((recording.end_time - end_ts) * 1000)
  441. clips.append(clip)
  442. durations.append(duration)
  443. return jsonify(
  444. {
  445. "cache": datetime.now() - timedelta(hours=1) > start_date,
  446. "discontinuity": False,
  447. "durations": durations,
  448. "sequences": [{"clips": clips}],
  449. }
  450. )
  451. def imagestream(detected_frames_processor, camera_name, fps, height, draw_options):
  452. while True:
  453. # max out at specified FPS
  454. time.sleep(1 / fps)
  455. frame = detected_frames_processor.get_current_frame(camera_name, draw_options)
  456. if frame is None:
  457. frame = np.zeros((height, int(height * 16 / 9), 3), np.uint8)
  458. width = int(height * frame.shape[1] / frame.shape[0])
  459. frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_LINEAR)
  460. ret, jpg = cv2.imencode(".jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 70])
  461. yield (
  462. b"--frame\r\n"
  463. b"Content-Type: image/jpeg\r\n\r\n" + jpg.tobytes() + b"\r\n\r\n"
  464. )