File size: 1,353 Bytes
25c5985
 
 
 
 
 
 
9de7b12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from src.pipeline.prediction_pipeline import PredictionPipeline
import numpy as np
from PIL import Image

pipeline = PredictionPipeline()

def predict_single(image):
    
    if image is None:
        return None, "No image detected!", "No image detected!"
    img = Image.fromarray(image) if isinstance(image, np.ndarray) else image
    result = pipeline.predict(img)
    annotated_img = pipeline.annotate(img, result)
    return annotated_img, result["category"], result["freshness"]

with gr.Blocks() as demo:
    gr.Markdown("# Food Freshness Detection")

    with gr.Tab("Image Upload"):
        image = gr.Image(sources=["upload"], label="Upload an Image")
        out_img = gr.Image()
        cat = gr.Textbox(label="Category")
        fresh = gr.Textbox(label="Freshness")
        btn = gr.Button("Predict on Image")
        btn.click(predict_single, inputs=image, outputs=[out_img, cat, fresh])

        
    with gr.Tab("Live Webcam"):
        webcam = gr.Image(sources=["webcam"], label="Webcam")
        out_img = gr.Image()
        cat = gr.Textbox(label="Category")
        fresh = gr.Textbox(label="Freshness")
        btn = gr.Button("Predict")
        btn.click(predict_single, inputs=webcam, outputs=[out_img, cat, fresh])


    

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)