File size: 2,890 Bytes
01034f9
 
 
 
 
 
 
 
 
898609b
01034f9
 
 
 
d235be4
 
01034f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d235be4
 
 
 
 
 
 
 
 
 
 
 
 
 
01034f9
 
d235be4
 
 
01034f9
 
 
 
 
 
 
 
 
d235be4
 
 
 
 
01034f9
 
 
 
 
 
 
 
 
 
 
 
 
 
d235be4
a6f40f9
d235be4
01034f9
 
 
 
 
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
import gradio as gr
import json
import requests
from PIL import Image
import os

# --- Constants for the API ---
API_URL = "https://predict.ultralytics.com"
# It's recommended to use Hugging Face secrets for API keys
API_KEY = os.environ.get("ULTRALYTICS_API_KEY") 
MODEL_ID = "https://hub.ultralytics.com/models/RsLHnWMhiBPqy3iFZAgr"

def classify_image(image):
    """
    Takes an image, sends it to the Ultralytics API, and returns the
    classification name and confidence score.
    """
    # Convert the Gradio image (numpy array) to a file-like object
    image_pil = Image.fromarray(image)
    image_path = "temp_image.jpg"
    image_pil.save(image_path)

    headers = {"x-api-key": API_KEY}
    data = {
        "model": MODEL_ID,
        "imgsz": 640,
        "conf": 0.25,
        "iou": 0.45
    }

    try:
        with open(image_path, "rb") as f:
            response = requests.post(API_URL, headers=headers, data=data, files={"file": f})
        
        # Check for a successful response
        response.raise_for_status()

        # Parse the JSON response
        result_data = response.json()
        
        # Extract the relevant information
        # The structure is data -> images -> [0] -> results -> [0]
        if result_data.get("images") and result_data["images"][0].get("results"):
            top_result = result_data["images"][0]["results"][0]
            name = top_result.get("name")
            confidence = top_result.get("confidence")
            
            # Return the extracted values for the two output components
            return name, f"{confidence:.2f}"
        else:
            return "No classification found", "N/A"

    except requests.exceptions.RequestException as e:
        return f"API Error: {e}", ""
    except (KeyError, IndexError):
        return "Could not parse API response.", ""
    finally:
        # Clean up the temporary image file
        if os.path.exists(image_path):
            os.remove(image_path)

# --- Gradio Interface ---

# Define the input and output components
image_input = gr.Image(type="numpy", label="Upload an Image or Use Webcam")

# Define separate outputs for name and confidence
name_output = gr.Label(label="Classification")
confidence_output = gr.Textbox(label="Confidence Score")


# List of example images
example_images = [
    ["images/img1.jpg"],
    ["images/img2.jpg"],
    ["images/img3.jpg"],
    ["images/img4.jpg"],
    ["images/img5.jpg"],
]

# Create the Gradio interface
iface = gr.Interface(
    fn=classify_image,
    inputs=image_input,
    outputs=[name_output, confidence_output],
    title="Tile Classification: Proof of Concept",
    description="Upload a picture or use your camera to classify an image using a pre-trained model. The model's prediction and confidence score will be displayed.",
    examples=example_images
)

# Launch the application
iface.launch()