Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(recognition)!: refactor whole recognition service #236

Merged
merged 14 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions alarm/src/infrastructure/events/KafkaAlarmEventsHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class KafkaAlarmEventsHub implements AlarmEventsHub {
subscribeToDetections(handler: (_detection: Detection) => void): void {
this.getDetectionsTopics()
.then((topics: string[]): void => {
console.log(topics)
this.detectionsConsumer
.startConsuming(topics, false, (message: KafkaMessage): void => {
if (message.value) {
Expand Down
14 changes: 1 addition & 13 deletions device/db/device-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,4 @@ db = new Mongo().getDB('device')

db.createCollection('device')

db.device.insert([
{
id: 'thing-1',
description: 'Revue federated device',
locationId: 'room-1',
endpoint: {
ipAddress: 'localhost',
port: 6001
},
isEnabled: true,
__v: 0
}
])
db.device.insert([])
9 changes: 4 additions & 5 deletions frontend/src/components/NavbarComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ nav {
max-height: 50px;
overflow: hidden;
transition: max-height 200ms linear;

&.expanded {
max-height: 300px;
}

background-color: $primary-color;
padding: 10px;

Expand All @@ -118,6 +113,10 @@ nav {
align-items: center;
gap: 0.3rem;

&.expanded {
max-height: 300px;
}

div.title {
display: flex;
align-items: center;
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/admin/ContactBadge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ li {
}

.infos {
.name {
font-size: 18px;
}

display: flex;
flex-direction: column;
gap: 2px;

.name {
font-size: 18px;
}
}

.delete {
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/admin/PermissionBadge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ li {
}

.infos {
.name {
font-size: 18px;
}

display: flex;
flex-direction: column;
gap: 2px;

.name {
font-size: 18px;
}
}

.delete {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/admin/UserListElement.vue
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ li {
}

.infos {
display: flex;
flex-direction: row;

.name {
font-size: 18px;
}
Expand All @@ -150,9 +153,6 @@ li {
height: 3px;
}

display: flex;
flex-direction: row;

.contacts {
display: flex;
flex-direction: column;
Expand Down
11 changes: 6 additions & 5 deletions frontend/src/components/devices/DeviceBadge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,19 @@ button {
}

ul {
@media (min-width: 576px) {
&.device {
height: 150px;
}
}
display: flex;
flex-direction: column;
justify-content: space-evenly;
margin-left: 7px;
list-style-type: none;
padding: 0;

@media (min-width: 576px) {
&.device {
height: 150px;
}
}

li {
margin-bottom: 0.2rem;

Expand Down
15 changes: 8 additions & 7 deletions frontend/src/components/security-rule/RuleBadge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ button {
}

ul {
display: flex;
flex-direction: column;
justify-content: space-evenly;
margin-top: 25px;
margin-left: 7px;
list-style-type: none;
padding: 0;

@media (min-width: 576px) {
&.range {
height: 150px;
Expand All @@ -121,13 +129,6 @@ ul {
height: 130px;
}
}
display: flex;
flex-direction: column;
justify-content: space-evenly;
margin-top: 25px;
margin-left: 7px;
list-style-type: none;
padding: 0;

li {
margin-bottom: 0.2rem;
Expand Down
12 changes: 12 additions & 0 deletions recognition/app/application/RecognitionEventsHub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from abc import ABC, abstractmethod


class RecognitionEventsHub(ABC):

@abstractmethod
def publish_detection(self, camera_code: str, object_class: str) -> None:
"""
Publishes a detection event
:param camera_code: the camera code on which the object was detected
:param object_class: the class of the detected object
"""
26 changes: 26 additions & 0 deletions recognition/app/application/RecognitionService.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from abc import ABC, abstractmethod


class RecognitionService(ABC):

@abstractmethod
def start_recognizing(self, camera_code: str) -> None:
"""
It starts to recognize the video stream produced by a camera.
If it is already recognizing the camera, it does nothing.
:param camera_code: the camera code of the camera to start recognizing
"""

@abstractmethod
def stop_recognizing(self, camera_code: str) -> None:
"""
It stops recognizing the video stream produced by a camera.
If it is not recognizing the camera, it does nothing.
:param camera_code: the camera code of the camera to stop recognizing
"""

@abstractmethod
def stop_all_recognizing(self):
"""
It stops recognizing the video stream produced by all cameras.
"""
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
from typing import Callable

import cv2 as cv
import numpy as np

from app.recognizer.Producer import Producer
from app.utils.interval import set_timeout
from app.utils.env import ENV


class Recognizer:
Expand All @@ -13,26 +13,24 @@ def __init__(self, camera_code: str, rtsp_stream_url: str):
yolo_resource: str = "app/resources/yolov3"
with open(f"{yolo_resource}/coco.names", "r") as f:
self.classes: [str] = [line.strip() for line in f.readlines()]
self._camera_code: str = camera_code
self._rtsp_stream_url: str = rtsp_stream_url
self.camera_code: str = camera_code
self.rtsp_stream_url: str = rtsp_stream_url
self._is_recognizing: bool = False
self._recognized_objects: [str] = []
if os.environ["TEST"] != "true":
self._producer = Producer()

if ENV != "test":
self._net = cv.dnn_DetectionModel(
f"{yolo_resource}/yolov3.weights", f"{yolo_resource}/yolov3.cfg"
)
self._net.setInputSize(320, 320)
self._net.setInputScale(1.0 / 255)
self._net.setInputSwapRB(True)

def start_recognizing(self) -> None:
if os.environ["TEST"] != "true":
def start_recognizing(self, recognition_handler: Callable[[str], None]) -> None:
if ENV != "test":
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;tcp"

# load capture
capture = cv.VideoCapture(self._rtsp_stream_url, cv.CAP_FFMPEG)
capture = cv.VideoCapture(self.rtsp_stream_url, cv.CAP_FFMPEG)
capture.set(cv.CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv.CAP_PROP_FRAME_HEIGHT, 480)

Expand Down Expand Up @@ -62,9 +60,7 @@ def start_recognizing(self) -> None:
recognized_object: str = self.classes[classId]
if recognized_object not in self._recognized_objects:
self._recognized_objects.append(recognized_object)
self._producer.produce(
"CAMERA_" + self._camera_code, recognized_object
)
recognition_handler(recognized_object)
set_timeout(
lambda: self._recognized_objects.remove(
recognized_object
Expand Down
3 changes: 3 additions & 0 deletions recognition/app/application/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from app.application.RecognitionService import RecognitionService
from app.application.RecognitionEventsHub import RecognitionEventsHub
from app.application.Recognizer import Recognizer
41 changes: 41 additions & 0 deletions recognition/app/application/impl/RecognitionServiceImpl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from concurrent.futures import ThreadPoolExecutor
from typing import Set

from app.application import RecognitionService, RecognitionEventsHub, Recognizer
from app.utils.Logger import logger
from app.utils.env import MEDIA_SERVER_HOST, MEDIA_SERVER_RTSP_PORT


class RecognitionServiceImpl(RecognitionService):

def __init__(self, events_hub: RecognitionEventsHub):
self._events = events_hub
self._executor = ThreadPoolExecutor()
self.recognizers: Set[Recognizer] = set()

def start_recognizing(self, camera_code: str):
if camera_code not in list(map(lambda r: r.camera_code, self.recognizers)):

def publish_detection(object_class: str):
logger.info(f"Detected {object_class} in camera {camera_code}")
self._events.publish_detection(camera_code, object_class)

rtsp_stream_url: str = (
f"rtsp://{MEDIA_SERVER_HOST}:{MEDIA_SERVER_RTSP_PORT}/{camera_code}/stream"
)
recognizer = Recognizer(camera_code, rtsp_stream_url)
self._executor.submit(recognizer.start_recognizing, publish_detection)
self.recognizers.add(recognizer)

def stop_recognizing(self, camera_code: str):
if camera_code in list(map(lambda r: r.camera_code, self.recognizers)):
recognizer = next(
filter(lambda r: r.camera_code == camera_code, self.recognizers)
)
recognizer.stop_recognizing()
self.recognizers.remove(recognizer)

def stop_all_recognizing(self):
for recognizer in self.recognizers:
recognizer.stop_recognizing()
self.recognizers.clear()
1 change: 1 addition & 0 deletions recognition/app/application/impl/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from app.application.impl.RecognitionServiceImpl import RecognitionServiceImpl
24 changes: 0 additions & 24 deletions recognition/app/domain/anomaly/core/Anomaly.py

This file was deleted.

22 changes: 0 additions & 22 deletions recognition/app/domain/anomaly/core/Intrusion.py

This file was deleted.

Loading
Loading