File size: 1,780 Bytes
b26e93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import gradio as gr
import spaces
import torch
import os
from PIL import Image
import subprocess
import tempfile

zero = torch.Tensor([0]).cuda()
print(zero.device) # <-- 'cpu' 🤔

CHECKPOINT="./D-FINE/weight/dfine-n.pth"

@spaces.GPU
def inference(image):
    temp_input = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
    image.save(temp_input.name)
    temp_input.close()

    subprocess.run([
        "python", "D-FINE/tools/inference/torch_inf.py",
        "-c", "D-FINE/configs/dfine/custom/dfine_hgnetv2_n_custom.yml",  
        "-r", CHECKPOINT,
        "--input", temp_input.name,
        "--device", "cuda:0"
    ], check=True)

    output_path = temp_input.name  
    output_image = Image.open(output_path)

    return output_image

def get_default_image_paths(folder_path):
    image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff')
    image_paths = [[os.path.join(folder_path, file)] for file in os.listdir(folder_path) 
                   if file.lower().endswith(image_extensions)]
    return image_paths

default_images = get_default_image_paths(Path("examples/"))

# Create Gradio Interface with title and description
iface = gr.Interface(
    fn=inference,
    inputs=[
        gr.Image(label="Upload Image", type="pil"),  # File input as PIL Image
    ],
    outputs=gr.Image(type="pil", label="Output (Image)"),  # Show output as an image
    examples=default_images,
    cache_examples=False,
    title="Strawberry Disease Detection DFINE S",
    description="This application detects diseases in strawberries using a trained D-FINE N model. " \
    "Upload an image use your webcam for analysis."
)

iface.launch()


# demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
# demo.launch()