-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from antoniomtz/business-logic
feat: Business logic with ROI entering/leaving
- Loading branch information
Showing
8 changed files
with
161 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
models/ | ||
models/ | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# | ||
# Copyright (C) 2024 Intel Corporation. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
FROM python:3.9-slim | ||
ENV PYTHONUNBUFFERED=1 | ||
WORKDIR /app | ||
COPY . . | ||
RUN pip install paho-mqtt==1.6.1 | ||
CMD ["python", "loss_prevention.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
''' | ||
* Copyright (C) 2024 Intel Corporation. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
''' | ||
|
||
import os | ||
import json | ||
import paho.mqtt.client as mqtt | ||
|
||
MQTT_BROKER_URL = os.getenv("MQTT_URL", "127.0.0.1") | ||
MQTT_PORT = int(os.getenv("MQTT_PORT", "1883")) | ||
MQTT_TOPIC = os.getenv("MQTT_TOPIC", "event/detection") | ||
ROI_NAME = os.getenv("ROI_NAME", "BASKET") | ||
|
||
current_objects_in_roi = {} | ||
|
||
def on_message(client, userdata, message): | ||
global current_objects_in_roi | ||
|
||
frame_data = json.loads(message.payload.decode("utf-8")) | ||
objects_in_current_frame = {} | ||
|
||
if "objects" in frame_data: | ||
for obj in frame_data["objects"]: | ||
if obj.get("roi_type") == ROI_NAME and obj["detection"].get("label") == ROI_NAME: | ||
continue | ||
|
||
obj_id = obj["id"] | ||
label = obj["detection"]["label"] | ||
objects_in_current_frame[obj_id] = label | ||
|
||
entered_objects = {obj_id: label for obj_id, label in objects_in_current_frame.items() if obj_id not in current_objects_in_roi} | ||
left_objects = {obj_id: label for obj_id, label in current_objects_in_roi.items() if obj_id not in objects_in_current_frame} | ||
|
||
for obj_id, label in entered_objects.items(): | ||
print(f"Object {label} (ID: {obj_id}) entered the {ROI_NAME} ROI.") | ||
|
||
for obj_id, label in left_objects.items(): | ||
print(f"Object {label} (ID: {obj_id}) left the {ROI_NAME} ROI.") | ||
|
||
current_objects_in_roi = objects_in_current_frame | ||
|
||
print(f" Current objects in ROI: {current_objects_in_roi}") | ||
|
||
def on_connect(client, userdata, flags, rc): | ||
print(f"Connected with result code {rc}") | ||
client.subscribe(MQTT_TOPIC) | ||
|
||
client = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol=mqtt.MQTTv311, transport="tcp") | ||
client.on_connect = on_connect | ||
client.on_message = on_message | ||
|
||
client.connect(MQTT_BROKER_URL, MQTT_PORT, 60) | ||
client.loop_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
''' | ||
* Copyright (C) 2024 Intel Corporation. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
''' | ||
|
||
from gi.repository import Gst, GObject | ||
import sys | ||
import gi | ||
import json | ||
from copy import deepcopy | ||
gi.require_version('Gst', '1.0') | ||
|
||
|
||
class RoiMetadata: | ||
def __init__(self, disable=False, roi=""): | ||
self.roi = roi | ||
|
||
def process_frame(self, frame): | ||
|
||
if self.roi == "": | ||
return True | ||
|
||
for message in frame.messages(): | ||
message_obj = json.loads(message) | ||
|
||
if "objects" in message_obj: | ||
frame.remove_message(message) | ||
message_objects_array = deepcopy(message_obj["objects"]) | ||
|
||
roi_objects = [] | ||
other_objects = [] | ||
|
||
for obj in message_objects_array: | ||
if "roi_type" in obj: | ||
if obj["roi_type"] == self.roi: | ||
roi_objects.append(obj) | ||
else: | ||
other_objects.append(obj) | ||
|
||
for obj in other_objects: | ||
for objects in roi_objects: | ||
if (obj["x"] >= objects["x"] and obj["x"] + obj["w"] <= objects["x"] + objects["w"] and | ||
obj["y"] >= objects["y"] and obj["y"] + obj["h"] <= objects["y"] + objects["h"]): | ||
obj["roi_type"] = self.roi | ||
|
||
message_obj["objects"] = other_objects + roi_objects | ||
|
||
frame.add_message(json.dumps( | ||
message_obj, separators=(',', ':'))) | ||
break | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,15 @@ | ||
[ | ||
|
||
{ | ||
|
||
"objects": [ | ||
|
||
{ | ||
|
||
"detection": { | ||
|
||
"label": "ROI1" | ||
|
||
"label": "BASKET" | ||
}, | ||
|
||
"x": 0, | ||
|
||
"y": 0, | ||
|
||
"w": 680, | ||
|
||
"h": 1080 | ||
|
||
"x": 400, | ||
"y": 80, | ||
"w": 2000, | ||
"h": 2000 | ||
} | ||
|
||
|
||
] | ||
|
||
} | ||
|
||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters