-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
111 lines (88 loc) · 2.59 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import base64
import cv2
import flask
import numpy as np
import supervision as sv
from flask import request
from ultralytics import YOLO
# Initialize the Flask application
app = flask.Flask(__name__)
# Initialize the YOLO model
model = YOLO("model/yolov8_v2.pt")
def _decode(img: str) -> np.ndarray:
"""Decode the image from base64 format.
Args:
img: The image in base64 format.
Returns:
The decoded image.
"""
decodedData = base64.b64decode((img))
return cv2.imdecode(np.frombuffer(decodedData, np.uint8), cv2.IMREAD_COLOR)
def _encode(img: np.ndarray) -> str:
"""Encode the image to base64 format.
Args:
img: The image to be encoded.
Returns:
The encoded image.
"""
_, buffer = cv2.imencode(".jpg", img)
return base64.b64encode(buffer).decode("utf-8")
@app.route("/predict", methods=["POST"])
def predict():
"""Predict the bounding boxes of the input image.
Returns:
The image with bounding boxes.
"""
# Decode the image
data = request.get_json()
img = _decode(data['image'])
# increase brightness of image
# img = cv2.convertScaleAbs(img, alpha=1.5, beta=0)
# Predict the bounding boxes
result = model(img, agnostic_nms=True, iou=0.2, conf=0.40)[0]
box_annotator = sv.BoxAnnotator(
text_thickness=2,
thickness=2,
text_scale=1
)
detections = sv.Detections.from_yolov8(result)
labels = [
f"{model.model.names[class_id]} -> {conf:.2f}" for _, _, conf, class_id, _ in detections
]
frame = box_annotator.annotate(
scene=img, detections=detections, labels=labels)
# Encode the image
img_base64 = _encode(frame)
typeA = typeB = typeC = 0
conf_threshold = 0.5
for box in result.boxes:
if box.cls == 0 and box.conf > conf_threshold:
typeA += 1
elif box.cls == 1 and box.conf > conf_threshold:
typeB += 1
elif box.cls == 2 and box.conf > conf_threshold:
typeC += 1
context = {
"image": img_base64,
"data": [
{
"label": "Type A",
"count": typeA
},
{
"label": "Type B",
"count": typeB
},
{
"label": "Type C",
"count": typeC
},
{
"label": "Unsure",
"count": abs(typeA + typeB + typeC - len(result.boxes))
}
]
}
return context
if __name__ == "__main__":
app.run(debug=False, host="10.100.40.135", port=3545)