1- from flask import Response
1+ from flask import Response , request
22from flask_restful import Resource
3+ from flask_jwt_extended import jwt_required
34
5+ from src .db import Session
6+ from src .libs .strings import gettext
7+ from src .models import VideoFeedModel
8+ from src .schemas import VideoFeedSchema
49from src .libs .web_utils import RecognitionCamera
510
611
7- class VideoFeed (Resource ):
12+ video_feed_schema = VideoFeedSchema ()
13+ video_feed_list_schema = VideoFeedSchema (many = True )
14+
15+
16+ # TODO: change this to support multiple feeds
17+ class VideoFeedList (Resource ):
818 @classmethod
19+ @jwt_required
920 def get (cls ):
21+ return video_feed_list_schema .dump (VideoFeedModel .find_all ()), 200
22+
23+
24+ class VideoFeed (Resource ):
25+ @classmethod
26+ def get (cls , feed_id : str ):
1027 """Video streaming route "/video_feed". Put this in the src attribute of an img tag."""
11- return Response (
12- VideoFeed .gen_frame (RecognitionCamera ()),
13- mimetype = 'multipart/x-mixed-replace; boundary=frame'
14- )
28+ video_feed = VideoFeedModel .find_by_id (feed_id )
29+
30+ if video_feed :
31+ return video_feed_schema .dump (video_feed ), 200
32+
33+ return {"message" : gettext ('video_feed_not_found' )}, 404
34+
35+
36+ # TODO: make this get() to work with @jwt_required
37+ class VideoFeedPreview (Resource ):
38+ @classmethod
39+ def get (cls , feed_id : str ):
40+ """Video streaming route. Put this route in the src attribute of an img tag."""
41+ video_feed = VideoFeedModel .find_by_id (feed_id )
42+
43+ if video_feed :
44+ return Response (
45+ cls .gen_frame (RecognitionCamera (video_feed )),
46+ mimetype = 'multipart/x-mixed-replace; boundary=frame'
47+ )
48+
49+ return {"message" : gettext ('video_feed_not_found' )}, 404
1550
1651 @classmethod
1752 def gen_frame (cls , camera ):
@@ -22,3 +57,68 @@ def gen_frame(cls, camera):
2257 b'--frame\r \n '
2358 b'Content-Type: image/jpeg\r \n \r \n ' + frame + b'\r \n '
2459 ) # concat frame one by one and show result
60+
61+
62+ # TODO: VideoFeedAdd Resource
63+ class VideoFeedAdd (Resource ):
64+ """Adds a video feed to `feeds` table in the database"""
65+ @classmethod
66+ @jwt_required
67+ def post (cls ):
68+ video_feed_json = request .get_json ()
69+
70+ video_feed = video_feed_schema .load (video_feed_json , session = Session )
71+
72+ try :
73+ video_feed .save_to_db ()
74+ except :
75+ return {"message" : gettext ('error_inserting' )}, 500
76+
77+ return video_feed_schema .dump (video_feed ), 201
78+
79+
80+ class VideoFeedStop (Resource ):
81+ @classmethod
82+ @jwt_required
83+ def get (cls , feed_id : str ):
84+ video_feed = VideoFeedModel .find_by_id (feed_id )
85+ if video_feed :
86+ RecognitionCamera .stop_feed ()
87+ try :
88+ video_feed .is_active = False
89+ video_feed .save_to_db ()
90+ except :
91+ return {"message" : gettext ('internal_server_error' )}, 500
92+ return {"message" : gettext ('video_feed_stopped' )}, 200
93+
94+ return {"message" : gettext ('video_feed_not_found' )}, 404
95+
96+
97+ class VideoFeedStart (Resource ):
98+ """Restart video feed for specific feed given by its feed_id"""
99+ @classmethod
100+ @jwt_required
101+ def get (cls , feed_id : str ):
102+ video_feed = VideoFeedModel .find_by_id (feed_id )
103+ if video_feed :
104+ # RecognitionCamera.start_feed()
105+ try :
106+ video_feed .is_active = True
107+ video_feed .save_to_db ()
108+ except :
109+ return {"message" : gettext ('internal_server_error' )}, 500
110+ return {"message" : gettext ('video_feed_stopped' )}, 200
111+
112+ return {"message" : gettext ('video_feed_not_found' )}, 404
113+
114+
115+ class VideoFeedDelete (Resource ):
116+ @classmethod
117+ @jwt_required
118+ def delete (cls , feed_id : str ):
119+ video_feed = VideoFeedModel .find_by_id (feed_id )
120+ if video_feed :
121+ video_feed .delete_from_db ()
122+ return {"message" : gettext ('video_feed_deleted' ).format (video_feed .id )}, 200
123+
124+ return {"message" : gettext ('video_feed_not_found' )}, 404
0 commit comments