-
Notifications
You must be signed in to change notification settings - Fork 1
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 #8 from 5sControl/dev
Dev
- Loading branch information
Showing
23 changed files
with
259 additions
and
211 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 |
---|---|---|
|
@@ -7,4 +7,4 @@ __pycache__ | |
.vscode/ | ||
build/ | ||
cmake-build-debug/ | ||
idle_models/weights/ | ||
idle_model/weights/ |
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,9 @@ | ||
# algorithms-python | ||
# algorithms-python | ||
|
||
```bash | ||
docker build ./idle_models -t 5scontrol/idle_python_server:v-.-.- | ||
docker build . -t 5scontrol/idle_python:v-.-.- | ||
|
||
docker run --network host --rm 5scontrol/idle_python_server:v-.-.- | ||
docker run --network host --rm 5scontrol/idle_python:v-.-.- | ||
``` |
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,2 @@ | ||
from .load_configs import load_configs | ||
configs = load_configs() |
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,7 +1,4 @@ | ||
{ | ||
"classes": [ | ||
77 | ||
], | ||
"wait_time": 10, | ||
"threshold": 100 | ||
} |
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,11 +1,8 @@ | ||
import json | ||
import os | ||
|
||
|
||
with open("confs/configs.json", "r") as conf: | ||
configs = json.load(conf) | ||
CONF_THRES = configs.get("conf_thres") | ||
IOU_THRES = configs.get("iou_thres") | ||
MODEL_PATH = configs.get("model_path") | ||
CLASSES = configs.get("classes") | ||
WAIT_TIME = configs.get("wait_time") | ||
THRESHOLD = configs.get("threshold") | ||
def load_configs() -> dict: | ||
with open("confs/configs.json", "r") as conf: | ||
configs = json.load(conf) | ||
return configs |
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,47 @@ | ||
import os | ||
import time | ||
import logging | ||
import requests | ||
import datetime | ||
import uuid | ||
import cv2 | ||
import numpy as np | ||
|
||
|
||
class IdleReporter: | ||
def __init__(self, images_folder: str, server_url: str, wait_time: int, logger: logging.Logger) -> None: | ||
self.images_folder = images_folder | ||
self.server_url = server_url | ||
self.wait_time = wait_time | ||
self.logger = logger | ||
os.makedirs(self.images_folder, exist_ok=True) | ||
|
||
def _save_image(self, image: np.array) -> str: | ||
save_photo_url = f'{self.images_folder}/' + str(uuid.uuid4()) + '.jpg' | ||
cv2.imwrite(save_photo_url, image) | ||
return save_photo_url | ||
|
||
def create_report(self, image: np.array, start_tracking_time: datetime.time) -> dict: | ||
time.sleep(self.wait_time) | ||
stop_tracking_time = str(datetime.datetime.now()) | ||
saved_image_name = self._save_image(image) | ||
report_for_send = { | ||
'camera': self.images_folder.split('/')[1], | ||
'algorithm': 'idle_control', | ||
'start_tracking': start_tracking_time, | ||
'stop_tracking': stop_tracking_time, | ||
'photos': [{'image': saved_image_name, 'date': start_tracking_time}], | ||
'violation_found': True, | ||
} | ||
return report_for_send | ||
|
||
def send_report(self, report: dict) -> None: | ||
try: | ||
self.logger.info(str(report)) | ||
requests.post( | ||
url=f'{self.server_url}:80/api/reports/report-with-photos/', json=report | ||
) | ||
except Exception as exc: | ||
self.logger.error(f"Cannot send report. Following error raised: {str(exc)}") | ||
|
||
|
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,29 @@ | ||
import httplib2 | ||
import logging | ||
import datetime | ||
import numpy as np | ||
import cv2 | ||
from typing import Tuple, Union | ||
|
||
|
||
class ImageHTTPExtractor: | ||
def __init__(self, server_url: str, logger: logging.Logger, **credentials) -> None: | ||
self.http_connection = httplib2.Http(".cache") | ||
self.http_connection.add_credentials(credentials.get("username"), credentials.get("password")) | ||
self.logger = logger | ||
self.server_url = server_url | ||
|
||
def get_snapshot(self) -> Tuple[Union[cv2.Mat, None], Union[datetime.time, None]]: | ||
try: | ||
curr_time = datetime.datetime.now() | ||
response, content = self.http_connection.request( | ||
self.server_url, | ||
"GET", | ||
body="foobar" | ||
) | ||
nparr = np.frombuffer(content, np.uint8) | ||
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) | ||
return img, curr_time | ||
except Exception as exc: | ||
self.logger.error(f"Cannot retrieve image. Following error raised - {exc}") | ||
return None, None |
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,50 @@ | ||
import requests | ||
import numpy as np | ||
from logging import Logger | ||
from PIL import Image | ||
import io | ||
|
||
|
||
PORT = 5001 | ||
|
||
|
||
class ModelPredictionsReceiver: | ||
def __init__(self, server_url: str, logger: Logger) -> None: | ||
self.server_url = server_url | ||
self.logger = logger | ||
|
||
@staticmethod | ||
def _convert_image2bytes(image: np.array, format='PNG') -> io.BytesIO: | ||
pil_image = Image.fromarray(image) | ||
img_byte_arr = io.BytesIO() | ||
pil_image.save(img_byte_arr, format=format) | ||
img_byte_arr.seek(0) | ||
return img_byte_arr | ||
|
||
def _predict(self, img: np.array) -> np.array: | ||
try: | ||
response = requests.post( | ||
f"{self.server_url}:{PORT}/predict", | ||
files={ | ||
"image": ("image", self._convert_image2bytes(img), "image/png") | ||
} | ||
) | ||
response.raise_for_status() | ||
return np.array(response.json().get("coordinates")) | ||
except Exception as exc: | ||
self.logger.critical("Cannot send request to model server. Error - {}".format(exc)) | ||
return np.array([]) | ||
|
||
def predict(self, img: np.array) -> np.array: | ||
imgs = [ | ||
img[:img.shape[0] // 2, :, :], | ||
img[img.shape[0] // 2:, :, :] | ||
] | ||
preds = [self._predict(imgs[0]), self._predict(imgs[1])] | ||
if len(preds[1]): | ||
preds[1][:, 1] += img.shape[0] // 2 | ||
preds[1][:, 3] += img.shape[0] // 2 | ||
result = np.append(*preds) | ||
if len(result.shape) == 1: | ||
result = np.expand_dims(result, 0) | ||
return result |
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,3 @@ | ||
from .IdleReporter import IdleReporter | ||
from .ImageHTTPExtractor import ImageHTTPExtractor | ||
from .ModelPredictionsReceiver import ModelPredictionsReceiver |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,24 @@ | ||
import torch | ||
import numpy as np | ||
import torch | ||
from ultralytics import YOLO | ||
|
||
|
||
class IdleObjectDetectionModel: | ||
def __init__(self, path: str, conf_thresh, iou_thresh, classes) -> None: | ||
self.model = YOLO(path) | ||
self.conf_thresh = conf_thresh | ||
self.iou_thresh = iou_thresh | ||
self.classes = classes | ||
|
||
@torch.no_grad() | ||
def __call__(self, img: np.array) -> np.array: | ||
output = self.model( | ||
source=img, | ||
conf=self.conf_thresh, | ||
iou=self.iou_thresh, | ||
max_det=600, | ||
classes=self.classes, | ||
verbose=False | ||
)[0].boxes | ||
return output.xyxy |
File renamed without changes.
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,10 @@ | ||
import json | ||
|
||
|
||
with open("configs/confs.json", "r") as conf: | ||
configs = json.load(conf) | ||
CONF_THRES = configs.get("conf_thres") | ||
IOU_THRES = configs.get("iou_thres") | ||
MODEL_PATH = configs.get("model_path") | ||
CLASSES = configs.get("classes") | ||
PORT = configs.get("port") |
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 |
---|---|---|
|
@@ -9,5 +9,4 @@ PyYAML==6.0 | |
requests==2.27.1 | ||
Flask==2.2.2 | ||
colorlog==4.8.0 | ||
transformers==4.28.1 | ||
timm==0.6.13 | ||
ultralytics==8.0.136 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.