File size: 1,573 Bytes
5794246
2fc4677
 
174f932
3a733d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5794246
2fc4677
 
5794246
 
2fc4677
 
 
 
 
 
 
 
 
 
5794246
2fc4677
 
3a733d3
 
 
2fc4677
3a733d3
2fc4677
 
5794246
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
import gradio as gr
from ultralytics import YOLO
from PIL import Image
import timm
from cods.classif.data import ClassificationDataset
from cods.classif.models import ClassificationModel
from cods.classif.cp import ClassificationConformalizer


def classif(img):
    model_name = "resnet34"
    pretrained_resnet_34 = timm.create_model(model_name, pretrained=True)
    classifier = ClassificationModel(model=pretrained_resnet_34, model_name=model_name)

    val_dataset = ClassificationDataset(...)  # path to imagenet validation set

    val_preds = classifier.build_predictions(
        val_dataset,
        dataset_name="imagenet",
        split_name="cal",
        batch_size=512,
        shuffle=False,
    )

    cc = ClassificationConformalizer(method="lac", preprocess="softmax")
    cc.lbd = 0.9
    conf_cls = cc.conformalize(val_preds)
    return str(conf_cls)


# Load a pretrained YOLOv8n model
model = YOLO("yolov8n.pt")


def main_function(lbd, img):
    results = model(img)  # predict on an image
    r = results[0]
    im_bgr = r.plot()  # BGR-order numpy array
    im_rgb = Image.fromarray(im_bgr[..., ::-1])  # RGB-order PIL image
    new_img = im_rgb
    # res = results[0].save(filename="output.jpg")  # save the image
    # # load image
    # new_img = Image.open("output.jpg")
    return new_img


iface = gr.Interface(
    fn=classif,  # main_function,
    inputs=gr.Image(type="pil"),  # ["slider", gr.Image(type="pil")],
    outputs=gr.Textbox(),  # Image(type="pil"),
    examples=[
        "bus.jpg",  # [0, "bus.jpg"],
    ],
)
iface.launch()