File size: 3,462 Bytes
0590b95
f4861ec
 
 
0590b95
f4861ec
 
0590b95
f4861ec
 
0590b95
f4861ec
 
 
 
 
 
 
 
0590b95
f4861ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0590b95
f4861ec
 
 
 
 
 
 
 
 
0590b95
 
f4861ec
 
 
 
0590b95
 
 
 
f4861ec
 
0590b95
 
 
 
f4861ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0590b95
f4861ec
0590b95
f4861ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0590b95
 
f4861ec
 
 
 
 
0590b95
 
f4861ec
0590b95
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import gradio as gr
from weight_detector import WeightDetector
import tempfile
import os
from PIL import Image
import requests
from io import BytesIO

# Initialize detector
detector = WeightDetector()

def process_input(image_source: str, image_upload=None, image_url: str = "") -> tuple:
    """
    Process image from different sources (upload, webcam, or URL)
    Returns:
        tuple: (detected_weight, detection_metadata, annotated_image)
    """
    temp_img_path = None
    
    try:
        # Handle different input types
        if image_source == "upload" and image_upload is not None:
            img = image_upload
        elif image_source == "url" and image_url:
            response = requests.get(image_url)
            img = Image.open(BytesIO(response.content))
        else:
            return None, "No valid image provided", None
            
        # Save to temp file for processing
        with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
            temp_img_path = f.name
            img.save(f.name)
            
        # Detect weight
        weight, metadata, annotated_img = detector.detect_weight(temp_img_path)
        
        # Format result message
        if weight is not None:
            message = f"βœ… Detected weight: {weight}g"
            if len(metadata) > 1:
                message += f" (from {len(metadata)} possible values)"
        else:
            message = "❌ No weight value detected"
            
        return weight, message, annotated_img
        
    except Exception as e:
        return None, f"Error: {str(e)}", None
    finally:
        if temp_img_path and os.path.exists(temp_img_path):
            os.unlink(temp_img_path)

# Gradio interface
with gr.Blocks(title="Auto Weight Logger") as demo:
    gr.Markdown("""
    # Auto Weight Logger
    Capture or upload an image of a weight measurement to automatically detect and log the value.
    """)
    
    with gr.Row():
        with gr.Column():
            image_source = gr.Radio(
                ["upload", "url"],
                label="Image Source",
                value="upload"
            )
            
            image_upload = gr.Image(
                sources=["upload"],
                type="pil",
                label="Upload Image"
            )
            
            image_url = gr.Textbox(
                label="Image URL",
                visible=False
            )
            
            submit_btn = gr.Button("Detect Weight")
            
        with gr.Column():
            weight_value = gr.Number(
                label="Detected Weight (g)",
                interactive=False
            )
            
            result_message = gr.Textbox(
                label="Detection Result",
                interactive=False
            )
            
            annotated_image = gr.Image(
                label="Annotated Image",
                interactive=False
            )
    
    # Show/hide URL input based on selection
    def toggle_url_visibility(source):
        return gr.Textbox(visible=source == "url")
    
    image_source.change(
        toggle_url_visibility,
        inputs=image_source,
        outputs=image_url
    )
    
    # Process submission
    submit_btn.click(
        process_input,
        inputs=[image_source, image_upload, image_url],
        outputs=[weight_value, result_message, annotated_image]
    )

# For Hugging Face Spaces
demo.launch()