File size: 1,327 Bytes
61ef567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import json
import sys

import cv2
from PIL import Image


DEFAULT_LABELS = [
    "melasma skin pigmentation",
    "ringworm skin infection ring shape",
    "eczema inflamed dry skin",
    "acne pimple pustule",
    "normal skin",
    "birthmark on skin",
    "shadow on skin",
    "insect bite on skin",
    "sunburned skin",
]


def parse_args(argv):
    if len(argv) < 2:
        print("Usage: python <script>.py <image_path> [label1] [label2] ...")
        return None

    image_path = Path(argv[1])
    if not image_path.is_file():
        print(f"Image not found: {image_path}")
        return None

    labels = argv[2:] if len(argv) > 2 else DEFAULT_LABELS
    return image_path, labels


def load_pil_image(image_path):
    img = cv2.imread(str(image_path))
    if img is None:
        raise ValueError(f"Could not load image: {image_path}")
    return Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))


def load_bgr_image(image_path):
    img = cv2.imread(str(image_path))
    if img is None:
        raise ValueError(f"Could not load image: {image_path}")
    return img


def print_results(model_name, image_path, results):
    print(f"\nModel: {model_name}")
    print(f"Image: {image_path}")
    print(json.dumps(results, indent=2))


def exit_with_usage():
    raise SystemExit(1)