+++ config_files/endpoints.py
... | ... | @@ -0,0 +1,0 @@ |
--- hls_streaming/hls.py
+++ hls_streaming/hls.py
... | ... | @@ -100,6 +100,8 @@ |
100 | 100 |
header = { |
101 | 101 |
'Content-Type': f'image/{image_type}', |
102 | 102 |
'x-time-sent': time_sent, |
103 |
+ # Why are you encoding string? because post method has a problem with sending not english chars. |
|
104 |
+ # (It defaults to latin-1 encoding and not straight forward to change) |
|
103 | 105 |
'x-cctv-name': base64.b64encode(str(self.cctvid).encode('utf-8')).decode('ascii'), |
104 | 106 |
'x-cctv-latitude' : str(self.lat), |
105 | 107 |
'x-cctv-longitude' : str(self.lon), |
--- inference_endpoint.py
+++ inference_endpoint.py
... | ... | @@ -7,6 +7,9 @@ |
7 | 7 |
import cv2 |
8 | 8 |
import time |
9 | 9 |
import base64 |
10 |
+import requests |
|
11 |
+from requests_toolbelt import MultipartEncoder |
|
12 |
+from config_files import API_ENDPOINT_MAIN |
|
10 | 13 |
|
11 | 14 |
app = Flask(__name__) |
12 | 15 |
api = Api(app, version='1.0', title='CCTV Image Upload API', |
... | ... | @@ -44,40 +47,89 @@ |
44 | 47 |
@ns.route('/infer', ) |
45 | 48 |
class ImageUpload(Resource): |
46 | 49 |
# @ns.expect(image_upload_model, validate=True) |
50 |
+ def __init__(self): |
|
51 |
+ super().__init__(api) |
|
52 |
+ self.time_sent = None |
|
53 |
+ self.cctv_latitude = None |
|
54 |
+ self.cctv_longitude = None |
|
55 |
+ self.cctv_info = None |
|
56 |
+ self.mask = None |
|
57 |
+ self.mask_blob = None |
|
58 |
+ self.image = None |
|
59 |
+ self.image_type = None |
|
60 |
+ self.area_percent = 0 |
|
61 |
+ |
|
47 | 62 |
@ns.response(200, 'Success') |
48 | 63 |
@ns.response(400, 'Validation Error') |
49 | 64 |
def post(self): |
50 | 65 |
if 'file' not in request.files: |
51 | 66 |
ns.abort(400, 'No image part in the request') |
52 |
- image = request.files['file'] |
|
53 |
- cctv_info = base64.b64decode(request.headers.get('x-cctv-name', '')).decode('UTF-8') |
|
54 |
- time_sent = request.headers.get('x-time-sent', '') |
|
55 |
- cctv_latitude = request.headers.get('x-cctv-latitude', 'Not provided') |
|
56 |
- cctv_longitude = request.headers.get('x-cctv-longitude', 'Not provided') |
|
67 |
+ self.image = request.files['file'] |
|
68 |
+ self.image_type = request.headers.get('Content-Type') |
|
69 |
+ self.cctv_info = base64.b64decode(request.headers.get('x-cctv-name', '')).decode('UTF-8') |
|
70 |
+ self.time_sent = request.headers.get('x-time-sent', '') |
|
71 |
+ self.cctv_latitude = request.headers.get('x-cctv-latitude', 'Not provided') |
|
72 |
+ self.cctv_longitude = request.headers.get('x-cctv-longitude', 'Not provided') |
|
57 | 73 |
|
58 |
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
|
59 |
- image = image.read() |
|
74 |
+ # timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
|
75 |
+ image = self.image.read() |
|
60 | 76 |
image = np.frombuffer(image, np.uint8) |
61 | 77 |
image = cv2.imdecode(image, cv2.IMREAD_COLOR) |
62 |
- filename = f"{timestamp}_{cctv_info}.png" |
|
78 |
+ # filename = f"{timestamp}_{self.cctv_info}.png" |
|
63 | 79 |
|
64 | 80 |
t1 = time.time() |
65 |
- detections, mask_maps = inference_engine.run_inference(image) |
|
81 |
+ detections, self.mask = inference_engine.run_inference(image) |
|
82 |
+ self.mask_blob = cv2.imencode(self.mask) |
|
83 |
+ self.mask_blob = self.mask.tobytes() |
|
66 | 84 |
t2 = time.time() |
67 | 85 |
|
68 | 86 |
print(t2 - t1) |
69 | 87 |
|
70 |
- if len(mask_maps) != 0: |
|
71 |
- seg_image = overlay_mask(image, mask_maps[0], color=(0, 255, 0), alpha=0.3) |
|
72 |
- area_percent = 0 |
|
88 |
+ if len(self.mask) != 0: |
|
89 |
+ seg_image = overlay_mask(image, self.mask[0], color=(0, 255, 0), alpha=0.3) |
|
90 |
+ self.area_percent = 0 |
|
73 | 91 |
else : |
74 |
- area_percent = np.sum(mask_maps) / image.shape[0] * image.shape[1] |
|
92 |
+ self.area_percent = np.sum(self.mask) / image.shape[0] * image.shape[1] |
|
75 | 93 |
|
94 |
+ self.send_result() |
|
76 | 95 |
# write another post request for pushing a detection result |
77 |
- return {"message": f"Image {filename} uploaded successfully!"} |
|
96 |
+ return {"message": f"Image {self.mask} uploaded successfully!"} |
|
78 | 97 |
|
79 | 98 |
def send_result(self): |
80 |
- pass |
|
99 |
+ time_sent = datetime.now(self.time_zone).strftime("yyyy-MM-dd'T'HH:mm:ss'Z'") |
|
100 |
+ header = { |
|
101 |
+ 'Content-Type': f'{self.image_type}', |
|
102 |
+ 'x-time-sent': time_sent, |
|
103 |
+ 'x-cctv-name': base64.b64encode(str(self.cctv_info).encode('utf-8')).decode('ascii'), |
|
104 |
+ 'x-cctv-latitude': str(self.cctv_latitude), |
|
105 |
+ 'x-cctv-longitude': str(self.cctv_longitude), |
|
106 |
+ 'x-area-percentage' : str(self.area_percent), |
|
107 |
+ } |
|
108 |
+ session = requests.Session() |
|
109 |
+ |
|
110 |
+ try: |
|
111 |
+ multipart_data = MultipartEncoder( |
|
112 |
+ fields={ |
|
113 |
+ 'image': ( |
|
114 |
+ f'frame_{self.cctv_info}.{self.image_type}', |
|
115 |
+ self.image, |
|
116 |
+ f'image/{self.image_type}' |
|
117 |
+ ), |
|
118 |
+ 'mask' : ( |
|
119 |
+ f'frame_mask_{self.cctv_info}.{self.image_type}', |
|
120 |
+ self.mask_blob, |
|
121 |
+ f'image/{self.image_type}' |
|
122 |
+ ) |
|
123 |
+ } |
|
124 |
+ ) |
|
125 |
+ header["Content-Type"] = multipart_data.content_type |
|
126 |
+ response = session.post(self.endpoint, headers=header, data=multipart_data) |
|
127 |
+ |
|
128 |
+ except Exception as e: |
|
129 |
+ print(e) |
|
130 |
+ print("Can not connect to the analyzer server. Check the endpoint address or connection.\n" |
|
131 |
+ f"Can not connect to : {self.endpoint}") |
|
132 |
+ |
|
81 | 133 |
|
82 | 134 |
if __name__ == '__main__': |
83 | 135 |
app.run(debug=True, port=12345) |
--- postprocessing.py
+++ postprocessing.py
... | ... | @@ -14,4 +14,31 @@ |
14 | 14 |
description='A postprocessing that dynamically decides how frequently send image and detection result') |
15 | 15 |
|
16 | 16 |
# Namespace definition |
17 |
-ns = api.namespace('cctv', description='CCTV operations')(파일 끝에 줄바꿈 문자 없음) |
|
17 |
+ns = api.namespace('cctv', description='CCTV operations') |
|
18 |
+ |
|
19 |
[email protected]('/postprocess', ) |
|
20 |
+class get_inference_result(Resource): |
|
21 |
+ def __init__(self): |
|
22 |
+ super().__init__(api) |
|
23 |
+ self.time_sent = None |
|
24 |
+ self.cctv_latitude = None |
|
25 |
+ self.cctv_longitude = None |
|
26 |
+ self.cctv_info = None |
|
27 |
+ self.mask = None |
|
28 |
+ self.image = None |
|
29 |
+ self.image_type = None |
|
30 |
+ self.area_percent = 0 |
|
31 |
+ |
|
32 |
+ @ns.response(200, 'Success') |
|
33 |
+ @ns.response(400, 'Validation Error') |
|
34 |
+ def post(self): |
|
35 |
+ self.image = request.files['image'] |
|
36 |
+ self.mask = request.files['mask'] |
|
37 |
+ self.cctv_info = base64.b64decode(request.headers.get('x-cctv-name', '')).decode('UTF-8') |
|
38 |
+ self.cctv_latitude = request.headers.get('x-cctv-latitude') |
|
39 |
+ self.cctv_longitude = request.headers.get('x-cctv-longitude') |
|
40 |
+ self.image_type = request.headers.get('Content-Type') |
|
41 |
+ self.area_percent = request.headers.get('x-area-percentage') |
|
42 |
+ |
|
43 |
+ |
|
44 |
+ |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?