Skip to content

Commit 82f2d7a

Browse files
Merge pull request #7 from 5sControl/dev
Dev
2 parents 7dc5f3f + a57bcab commit 82f2d7a

26 files changed

+44
-6348
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ __pycache__
66
.env
77
.vscode/
88
build/
9-
cmake-build-debug/
9+
cmake-build-debug/
10+
idle_models/weights/

confs/configs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"classes": [
3-
67
3+
77
44
],
55
"wait_time": 10,
66
"threshold": 100

confs/settings.env

Lines changed: 1 addition & 1 deletion
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.162/onvif-http/snapshot?Profile_1'
3+
camera_url = 'http://192.168.1.110:3456/onvif-http/snapshot?camera_ip=192.168.1.168'
44
server_url = 'http://127.0.0.1'
55
folder = 'images/192.168.1.163'

idle_models/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ WORKDIR /var/www/5scontrol
99
COPY . .
1010
RUN mkdir -p /usr/src/app
1111
EXPOSE 5001
12-
CMD [ "flask", "run","--host","0.0.0.0","--port","5001"]
12+
CMD [ "flask", "run","--host","0.0.0.0","--port", "5001"]
Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
import torch
2-
from yolor.model import get_model
32
import numpy as np
4-
from yolor.utils.datasets import letterbox
5-
from yolor.utils.general import non_max_suppression, scale_coords
3+
from transformers import DetrImageProcessor, DetrForObjectDetection, DetrConfig
4+
import torch
65

76

87
class IdleObjectDetectionModel:
98
def __init__(self, path: str, conf_thresh, iou_thresh, classes) -> None:
10-
self.model, self.device = get_model(path, "yolor/yolor_csp_x.cfg")
9+
self.model = DetrForObjectDetection.from_pretrained(path)
10+
self.model.eval()
11+
self.processor = DetrImageProcessor.from_pretrained(path)
1112
self.conf_thresh = conf_thresh
1213
self.iou_thresh = iou_thresh
1314
self.classes = classes
1415

15-
def __preprocess_image__(self, img: np.array) -> np.array:
16-
self.img_shape = img.shape
17-
img = letterbox(img.copy(), new_shape=1280, auto_size=64)[0]
18-
img = img[:, :, ::-1].transpose(2, 0, 1)
19-
img = np.ascontiguousarray(img)
20-
img = torch.from_numpy(img).to(self.device)
21-
img = img.float()
22-
img /= 255.0
23-
img = img.unsqueeze(0)
24-
return img
25-
2616
@torch.no_grad()
27-
def __call__(self, img: np.array) -> torch.Tensor:
28-
img = self.__preprocess_image__(img)
29-
pred = self.model(img, augment=False)[0]
30-
pred = non_max_suppression(
31-
pred, 0.45, 0.5, classes=[67], agnostic=False)[0]
32-
pred[:, :4] = scale_coords(
33-
img.shape[2:], pred[:, :4], self.img_shape).round()
34-
return pred[:, :5]
17+
def __call__(self, img: np.array) -> np.array:
18+
inputs = self.processor(images=img, return_tensors="pt")
19+
outputs = self.model(**inputs)
20+
target_sizes = torch.tensor([img.shape[:-1]])
21+
results = self.processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=self.conf_thresh)[0]
22+
pred = []
23+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
24+
if self.model.config.id2label[label.item()] == 'cell phone':
25+
pred.append([round(i, 2) for i in box.tolist()] + [score.item()])
26+
return np.array(pred)

idle_models/app.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from IdleObjectDetectionModel import IdleObjectDetectionModel
55
import numpy as np
66
import io
7+
import colorlog
8+
import logging
79

810

911
app = Flask(__name__)
@@ -14,13 +16,29 @@
1416
CLASSES
1517
)
1618

19+
logger = logging.getLogger('min_max_logger')
20+
handler = colorlog.StreamHandler()
21+
handler.setFormatter(colorlog.ColoredFormatter(
22+
'%(log_color)s%(asctime)s %(levelname)s: %(message)s',
23+
datefmt='%Y-%m-%d %H:%M:%S',
24+
log_colors={
25+
'DEBUG': 'cyan',
26+
'INFO': 'green',
27+
'WARNING': 'yellow',
28+
'CRITICAL': 'bold_red,bg_white',
29+
}))
30+
logger.addHandler(handler)
31+
logger.setLevel(logging.DEBUG)
32+
logger.propagate = False
33+
1734
convert_bytes2image = lambda bytes: np.array(Image.open(io.BytesIO(bytes)), dtype=np.uint8)
1835

1936
@app.route('/predict', methods=['POST'])
2037
def predict():
2138
if request.method == 'POST':
2239
image = convert_bytes2image(request.files["image"].read())
2340
coords = model(image)
41+
logger.info(f"Request to predict - {len(coords)}")
2442
return jsonify(
2543
{
2644
"coordinates": coords.tolist()

idle_models/configs/confs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
67
44
],
55
"iou_thres": 0.5,
6-
"conf_thres": 0.5,
7-
"model_path": "yolor/yolor_csp_x_star.pt",
6+
"conf_thres": 0.65,
7+
"model_path": "./weights",
88
"port": 5001
99
}

idle_models/requirements.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ pydantic==1.10.2
77
python-dotenv==1.0.0
88
PyYAML==6.0
99
requests==2.27.1
10-
Flask==2.2.2
10+
Flask==2.2.2
11+
colorlog==4.8.0
12+
transformers==4.28.1
13+
timm==0.6.13

idle_models/yolor/coco.names

Lines changed: 0 additions & 80 deletions
This file was deleted.

idle_models/yolor/model.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

idle_models/yolor/models/__init__.py

Whitespace-only changes.

idle_models/yolor/models/export.py

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)