forked from seblful/label-studio-yolov8-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_det.py
103 lines (85 loc) · 3.4 KB
/
model_det.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
import os
import random
import requests
import socket
from PIL import Image
from io import BytesIO
from ultralytics import YOLO
from label_studio_ml.model import LabelStudioMLBase
from label_studio_ml.utils import get_single_tag_keys, get_local_path
# hostname = socket.gethostname()
# LS_URL = socket.gethostbyname(hostname)
# print("Hostname: ", hostname)
# print("IP Address: ", ip_address)
LS_URL = "http://192.168.100.3:8080"
LS_API_TOKEN = "201a66049e8438c587125d8163d2d60538f067d5"
# Initialize class inhereted from LabelStudioMLBase
class YOLOv8Model(LabelStudioMLBase):
def __init__(self, **kwargs):
# Call base class constructor
super(YOLOv8Model, self).__init__(**kwargs)
# Initialize self variables
self.from_name, self.to_name, self.value, self.classes = get_single_tag_keys(
self.parsed_label_config, 'RectangleLabels', 'Image')
self.labels = ['capsules', 'tablets']
# Load model
self.model = YOLO("best_det.pt")
# Function to predict
def predict(self, tasks, **kwargs):
"""
Returns the list of predictions based on input list of tasks for 1 image
"""
task = tasks[0]
# Getting URL of the image
image_url = task['data'][self.value]
full_url = LS_URL + image_url
print("FULL URL: ", full_url)
# Header to get request
header = {
"Authorization": "Token " + LS_API_TOKEN}
# Getting URL and loading image
image = Image.open(BytesIO(requests.get(
full_url, headers=header).content))
# Height and width of image
original_width, original_height = image.size
# Creating list for predictions and variable for scores
predictions = []
score = 0
i = 0
# Getting prediction using model
results = self.model.predict(image)
# Getting mask segments, boxes from model prediction
for result in results:
for i, prediction in enumerate(result.boxes):
xyxy = prediction.xyxy[0].tolist()
predictions.append({
"id": str(i),
"from_name": self.from_name,
"to_name": self.to_name,
"type": "rectanglelabels",
"score": prediction.conf.item(),
"original_width": original_width,
"original_height": original_height,
"image_rotation": 0,
"value": {
"rotation": 0,
"x": xyxy[0] / original_width * 100,
"y": xyxy[1] / original_height * 100,
"width": (xyxy[2] - xyxy[0]) / original_width * 100,
"height": (xyxy[3] - xyxy[1]) / original_height * 100,
"rectanglelabels": [self.labels[int(prediction.cls.item())]]
}})
score += prediction.conf.item()
print(f"Prediction Score is {score:.3f}.")
# Dict with final dicts with predictions
final_prediction = [{
"result": predictions,
"score": score / (i + 1),
"model_version": "v8n"
}]
return final_prediction
def fit(self, completions, workdir=None, **kwargs):
"""
Dummy function to train model
"""
return {'random': random.randint(1, 10)}