Skip to content

Commit 2459296

Browse files
Merge branch 'dev' into main
2 parents 65a090d + a16b1d1 commit 2459296

14 files changed

+119
-46
lines changed

.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.cache
2-
__pycache__
2+
__pycache__
3+
idle_models

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ images/
44
models/
55
env/
66
__pycache__
7-
.env
7+
.env
8+
.vscode/

confs/configs.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
"classes": [
33
67
44
],
5-
"iou_thres": 0.5,
6-
"conf_thres": 0.3,
7-
"model_path": "idle_models/idle_v0.3.11.pt",
85
"wait_time": 10,
96
"threshold": 100
107
}

confs/settings.env

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
username = 'admin'
22
password = 'just4Taqtile'
3-
camera_url = 'http://192.168.1.166/onvif-http/snapshot?Profile_1'
4-
server_url = 'http://192.168.1.999'
5-
folder = 'images/192.168.1.168'
3+
camera_url = 'http://192.168.1.162/onvif-http/snapshot?Profile_1'
4+
server_url = 'http://127.0.0.1'
5+
folder = 'images/192.168.1.163'

functional.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import requests
1010
import httplib2
1111
import numpy as np
12-
from idle_models.ObjectDetectionModel import ObjDetectionModel
1312
import time
14-
from dotenv import load_dotenv
1513

1614

1715
def create_logger():
@@ -34,9 +32,6 @@ def create_logger():
3432

3533
def init_connection():
3634
password = os.environ.get("password")
37-
if password is None:
38-
load_dotenv("confs/settings.env")
39-
password = os.environ.get("password")
4035
username = os.environ.get("username")
4136
try:
4237
h = httplib2.Http(".cache")
@@ -47,20 +42,16 @@ def init_connection():
4742
return None
4843

4944

50-
def init_model():
51-
model = ObjDetectionModel(MODEL_PATH, CONF_THRES, IOU_THRES, CLASSES)
52-
return model
53-
54-
5545
def get_frame(h):
5646
try:
47+
time_ = datetime.datetime.now()
5748
_, content = h.request(os.environ.get(
5849
"camera_url"), "GET", body="foobar")
5950
nparr = np.frombuffer(content, np.uint8)
60-
img0 = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
61-
return img0
51+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
52+
return img, time_
6253
except Exception as exc:
63-
return None
54+
return None, None
6455

6556

6657
def put_rectangle(img, boxes, scores):
@@ -78,25 +69,23 @@ def check_coordinates_diffs(coords_1: np.array, coords_2: np.array, threshold=TH
7869
return diff > threshold
7970

8071

81-
def send_report_and_save_photo(img0):
72+
def send_report_and_save_photo(img0, start_track_time):
8273
server_url = os.environ.get("server_url")
8374
folder = os.environ.get("folder")
8475

8576
pathlib.Path(folder).mkdir(exist_ok=True, parents=True)
8677
save_photo_url = f'{folder}/' + str(uuid.uuid4()) + '.jpg'
8778
cv2.imwrite(save_photo_url, img0)
8879

89-
time_delta_seconds = WAIT_TIME
90-
start_tracking = str(datetime.datetime.now())
91-
time.sleep(time_delta_seconds)
80+
time.sleep(WAIT_TIME)
9281
stop_tracking = str(datetime.datetime.now())
9382

9483
report_for_send = {
9584
'camera': folder.split('/')[1],
9685
'algorithm': 'idle_control',
97-
'start_tracking': start_tracking,
86+
'start_tracking': start_track_time,
9887
'stop_tracking': stop_tracking,
99-
'photos': [{'image': save_photo_url, 'date': start_tracking}],
88+
'photos': [{'image': save_photo_url, 'date': start_track_time}],
10089
'violation_found': True,
10190
}
10291
try:

idle_models/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM python:3.10
2+
RUN apt-get update
3+
RUN pip install torch==1.13.1+cpu torchvision==0.14.1+cpu --extra-index-url https://download.pytorch.org/whl/cpu
4+
COPY requirements.txt .
5+
RUN pip install -r requirements.txt
6+
RUN apt-get install ffmpeg libsm6 libxext6 -y
7+
8+
WORKDIR /var/www/5scontrol
9+
COPY . .
10+
RUN mkdir -p /usr/src/app
11+
EXPOSE 5000
12+
CMD [ "flask", "run","--host","0.0.0.0","--port","5000"]

idle_models/ObjectDetectionModel.py renamed to idle_models/IdleObjectDetectionModel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from ultralytics import YOLO
21
import torch
2+
from ultralytics import YOLO
33

44

5-
class ObjDetectionModel:
5+
class IdleObjectDetectionModel:
66
def __init__(self, path: str, conf_thresh, iou_thresh, classes) -> None:
77
self.model = YOLO(path)
88
self.conf_thresh = conf_thresh

idle_models/app.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from PIL import Image
2+
from flask import Flask, jsonify, request
3+
from configs.load_configs import *
4+
from IdleObjectDetectionModel import IdleObjectDetectionModel
5+
import numpy as np
6+
7+
8+
app = Flask(__name__)
9+
model = IdleObjectDetectionModel(
10+
MODEL_PATH,
11+
CONF_THRES,
12+
IOU_THRES,
13+
CLASSES
14+
)
15+
16+
17+
@app.route('/predict', methods=['POST'])
18+
def predict():
19+
if request.method == 'POST':
20+
image = np.array(request.json['image']).astype(np.float32)
21+
coords, confs = model(image)
22+
return jsonify(
23+
{
24+
"coordinates": coords.tolist(),
25+
"confidences": confs.tolist()
26+
}
27+
)

idle_models/configs/confs.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"classes": [
3+
67
4+
],
5+
"iou_thres": 0.5,
6+
"conf_thres": 0.3,
7+
"model_path": "idle_v0.3.11.pt",
8+
"port": 5000
9+
}

idle_models/configs/load_configs.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import json
2+
3+
4+
with open("configs/confs.json", "r") as conf:
5+
configs = json.load(conf)
6+
CONF_THRES = configs.get("conf_thres")
7+
IOU_THRES = configs.get("iou_thres")
8+
MODEL_PATH = configs.get("model_path")
9+
CLASSES = configs.get("classes")
10+
PORT = configs.get("port")

idle_models/requirements.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
httplib2==0.22.0
2+
numpy==1.22.3
3+
opencv_python==4.7.0.72
4+
pafy==0.5.5
5+
Pillow==9.5.0
6+
pydantic==1.10.2
7+
python-dotenv==1.0.0
8+
PyYAML==6.0
9+
requests==2.27.1
10+
ultralytics==8.0.112
11+
Flask==2.2.2

main.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
import time
22
from functional import *
3+
from send_request import predict
4+
from dotenv import load_dotenv
35

46

7+
password = os.environ.get("password")
8+
if password is None:
9+
load_dotenv("confs/settings.env")
510
logger = create_logger()
611

712

813
def run():
14+
server_url = os.environ.get("server_url")
915
while (h := init_connection()) is None:
1016
logger.warning("Cannot create connection")
1117
time.sleep(1)
12-
model = init_model()
13-
logger.info("Model initialized")
1418
prev_preds = None
1519
iter_idx = 0
1620
while True:
1721
iter_idx += 1
1822
if iter_idx % 60 == 0:
19-
logger.info("60 iterations was passed")
20-
if (img := get_frame(h)) is None:
23+
logger.info("60 iterations passed")
24+
img, start_tracking = get_frame(h)
25+
start_tracking = str(start_tracking)
26+
if img is None:
2127
logger.warning("Empty photo")
22-
time.sleep(4)
28+
time.sleep(1)
2329
continue
24-
time.sleep(3)
25-
preds, scores = model(img)
26-
img = put_rectangle(img, preds.numpy(), scores.numpy())
30+
time.sleep(2)
31+
preds, scores = predict(img, server_url)
32+
img = put_rectangle(img, preds, scores)
2733
if len(scores) > 0:
28-
logger.info("Telephone was detected")
34+
logger.info("Telephone is detected")
2935
if check_coordinates_diffs(prev_preds, preds):
30-
send_report_and_save_photo(img)
36+
send_report_and_save_photo(img, start_tracking)
3137
prev_preds = preds
3238

3339

requirements.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
colorlog==4.8.0
22
httplib2==0.22.0
3-
matplotlib==3.6.3
43
numpy==1.22.3
5-
onnx==1.13.1
64
opencv_python==4.7.0.72
75
pafy==0.5.5
8-
pandas==1.5.3
96
Pillow==9.5.0
107
pydantic==1.10.2
118
python-dotenv==1.0.0
129
PyYAML==6.0
1310
requests==2.27.1
14-
scipy==1.10.1
15-
seaborn==0.12.2
16-
tqdm==4.64.1
17-
ultralytics==8.0.112

send_request.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import requests
2+
import numpy as np
3+
4+
5+
PORT = 5000
6+
7+
8+
def predict(img: np.array, server_url: str):
9+
response = requests.post(
10+
f"{server_url}:{PORT}/predict",
11+
json={
12+
"image": img.tolist()
13+
}
14+
)
15+
coordinates = np.array(response.json().get("coordinates"))
16+
confidences = np.array(response.json().get("confidences"))
17+
return [coordinates, confidences]

0 commit comments

Comments
 (0)