Spaces:
Sleeping
Sleeping
feat: adding testing
Browse files- api/routers/camera_stream.py +6 -2
- domain/detection_box_center.py +1 -1
- services/pipeline.py +9 -9
api/routers/camera_stream.py
CHANGED
|
@@ -85,8 +85,12 @@ async def websocket_detect(
|
|
| 85 |
while True:
|
| 86 |
frame_bytes = await frame_queue.get()
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# Note that JSONResponse doesn't work here, as it is for HTTP
|
| 92 |
await websocket.send_json(results)
|
|
|
|
| 85 |
while True:
|
| 86 |
frame_bytes = await frame_queue.get()
|
| 87 |
|
| 88 |
+
try:
|
| 89 |
+
results = await pipeline.run(camera_id, frame_bytes, next(step_counter))
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logger.warn(f"Error happened while processing a frame in {camera_id}: {e}")
|
| 92 |
+
logger.exception(e)
|
| 93 |
+
continue
|
| 94 |
|
| 95 |
# Note that JSONResponse doesn't work here, as it is for HTTP
|
| 96 |
await websocket.send_json(results)
|
domain/detection_box_center.py
CHANGED
|
@@ -2,7 +2,7 @@ def calculate_detection_box_center(detections, image_width:float):
|
|
| 2 |
|
| 3 |
boxes_center = []
|
| 4 |
boxes_center_ratio = []
|
| 5 |
-
for box in detections
|
| 6 |
xmin, ymin, xmax, ymax = box.xyxy
|
| 7 |
xcenter = (xmax + xmin) / 2
|
| 8 |
ycenter = (ymax + ymin) / 2
|
|
|
|
| 2 |
|
| 3 |
boxes_center = []
|
| 4 |
boxes_center_ratio = []
|
| 5 |
+
for box in detections:
|
| 6 |
xmin, ymin, xmax, ymax = box.xyxy
|
| 7 |
xcenter = (xmax + xmin) / 2
|
| 8 |
ycenter = (ymax + ymin) / 2
|
services/pipeline.py
CHANGED
|
@@ -11,13 +11,13 @@ import numpy as np
|
|
| 11 |
|
| 12 |
|
| 13 |
class ProcessingPipeline:
|
| 14 |
-
def __init__(self, detector, depth_model, safety_detector,
|
| 15 |
self.detector = detector
|
| 16 |
self.depth_model = depth_model
|
| 17 |
self.safety_detector = safety_detector
|
| 18 |
-
self.
|
| 19 |
|
| 20 |
-
def _decode_frame(fb):
|
| 21 |
return cv.imdecode(np.frombuffer(fb, np.uint8), cv.IMREAD_COLOR)
|
| 22 |
|
| 23 |
def _camera_metadata(self, camera_id, safety_detection, depth_points, boxes_center_ratio) -> CameraMetadata:
|
|
@@ -31,23 +31,23 @@ class ProcessingPipeline:
|
|
| 31 |
)
|
| 32 |
return metadata
|
| 33 |
|
| 34 |
-
async def run(self, camera_id:str,
|
| 35 |
loop = asyncio.get_running_loop()
|
| 36 |
|
| 37 |
with profile_step("frame_processing_time", decode_duration_seconds, camera_id, frame_count):
|
| 38 |
-
|
| 39 |
|
| 40 |
with profile_step("detection_duration_seconds", detection_duration_seconds, camera_id, frame_count):
|
| 41 |
-
detection_task = loop.run_in_executor(None, self.detector.detect,
|
| 42 |
-
safety_task = loop.run_in_executor(None, self.safety_detector.detect,
|
| 43 |
detections, safety_detection = await asyncio.gather(detection_task, safety_task)
|
| 44 |
|
| 45 |
-
boxes_center, boxes_center_ratio = calculate_detection_box_center(detections.detections,
|
| 46 |
|
| 47 |
depth_points = []
|
| 48 |
if boxes_center:
|
| 49 |
with profile_step("depth_duration_seconds", depth_duration_seconds, camera_id, frame_count):
|
| 50 |
-
depth_points = await loop.run_in_executor(None, self.depth_model.calculate_depth,
|
| 51 |
|
| 52 |
metadata = self._camera_metadata(camera_id, safety_detection, depth_points, boxes_center_ratio)
|
| 53 |
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
class ProcessingPipeline:
|
| 14 |
+
def __init__(self, detector, depth_model, safety_detector, redis):
|
| 15 |
self.detector = detector
|
| 16 |
self.depth_model = depth_model
|
| 17 |
self.safety_detector = safety_detector
|
| 18 |
+
self.redis = redis
|
| 19 |
|
| 20 |
+
def _decode_frame(self, fb):
|
| 21 |
return cv.imdecode(np.frombuffer(fb, np.uint8), cv.IMREAD_COLOR)
|
| 22 |
|
| 23 |
def _camera_metadata(self, camera_id, safety_detection, depth_points, boxes_center_ratio) -> CameraMetadata:
|
|
|
|
| 31 |
)
|
| 32 |
return metadata
|
| 33 |
|
| 34 |
+
async def run(self, camera_id:str, frame_bytes, frame_count):
|
| 35 |
loop = asyncio.get_running_loop()
|
| 36 |
|
| 37 |
with profile_step("frame_processing_time", decode_duration_seconds, camera_id, frame_count):
|
| 38 |
+
frame_bytes = await loop.run_in_executor(None, self._decode_frame, frame_bytes)
|
| 39 |
|
| 40 |
with profile_step("detection_duration_seconds", detection_duration_seconds, camera_id, frame_count):
|
| 41 |
+
detection_task = loop.run_in_executor(None, self.detector.detect, frame_bytes)
|
| 42 |
+
safety_task = loop.run_in_executor(None, self.safety_detector.detect, frame_bytes)
|
| 43 |
detections, safety_detection = await asyncio.gather(detection_task, safety_task)
|
| 44 |
|
| 45 |
+
boxes_center, boxes_center_ratio = calculate_detection_box_center(detections.detections, frame_bytes.shape[1])
|
| 46 |
|
| 47 |
depth_points = []
|
| 48 |
if boxes_center:
|
| 49 |
with profile_step("depth_duration_seconds", depth_duration_seconds, camera_id, frame_count):
|
| 50 |
+
depth_points = await loop.run_in_executor(None, self.depth_model.calculate_depth, frame_bytes, boxes_center)
|
| 51 |
|
| 52 |
metadata = self._camera_metadata(camera_id, safety_detection, depth_points, boxes_center_ratio)
|
| 53 |
|