-
Notifications
You must be signed in to change notification settings - Fork 2
/
classifiers.py
74 lines (59 loc) · 2.1 KB
/
classifiers.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
import os
import folder_paths
from torchvision import transforms
from ultralytics import YOLO
model_path = folder_paths.models_dir
folder_paths.add_model_folder_path("ultralytics_classifiers", os.path.join(model_path, "ultralytics", "classifiers"))
class YOLOClassifierModelLoader:
# Download these models from https://docs.ultralytics.com/tasks/classify/
# Place them on models/ultralytics/classification folder
model_names = (
'yolov8n-cls.pt',
'yolov8s-cls.pt',
'yolov8m-cls.pt',
'yolov8l-cls.pt',
'yolov8x-cls.pt'
)
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model_name": (cls.model_names,)
}
}
RETURN_TYPES = ("YOLO_CLASSIFIER_MODEL",)
RETURN_NAMES = ("YOLO classifier model",)
FUNCTION = "load_model"
CATEGORY = "SuperMasterBlasterLaser/ComfyUI_YOLO_Classifiers"
def load_model(self, model_name):
model_path = folder_paths.get_full_path("ultralytics_classifiers", model_name)
model = YOLO(model_path)
return (model,)
class YOLOClassify:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"yolo_classifier_model": ("YOLO_CLASSIFIER_MODEL",),
"image_to_classify": ("IMAGE",)
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("Class name with highest score",)
FUNCTION = "classify"
CATEGORY = "SuperMasterBlasterLaser/ComfyUI_YOLO_Classifiers"
def classify(self, yolo_classifier_model, image_to_classify):
image_to_classify = image_to_classify.permute(0, 3, 1, 2)
image_to_classify = image_to_classify.half()
image_to_classify = image_to_classify.squeeze(0)
transform = transforms.Compose([
transforms.Resize(size=(224, 224))
])
img = transform(image_to_classify).unsqueeze(0)
result = yolo_classifier_model(img)
class_name = None
for r in result:
top1 = r.probs.top1
class_name = r.names[top1]
break
return (class_name,)