File size: 2,822 Bytes
3ac7250
 
c970970
3ac7250
 
 
 
c970970
3ac7250
 
 
 
 
 
 
 
 
c970970
 
b27d380
c970970
 
 
 
 
 
 
 
 
 
 
 
 
4809d91
 
 
 
c970970
 
 
 
b27d380
 
 
4809d91
c970970
3ac7250
4809d91
 
3ac7250
c970970
 
 
 
 
 
7987c06
 
c970970
 
4809d91
db8bdd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c970970
 
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
from  hubconf import custom
model = custom(path_or_model='best.pt')  # custom example
model.eval()
# model = create(name='yolov7', pretrained=True, channels=3, classes=80, autoshape=True)  # pretrained example

# Verify inference
import numpy as np
import torch
from PIL import Image
import gradio as gr

# imgs = [np.zeros((640, 480, 3))]
# imgs = 'inference/images/meal.jpg'

# results = model(imgs)  # batched inference
# results.print()
# results.save()
def total_price(predicted):
    price = 0
    for name, confidence in predicted:
        if name == "side dish":
            price += 10
        elif name == "purple rice" or name == "white rice" or name == "brown rice":
            price += 20
        elif name == "40dollars meal":
            price += 40
        elif name == "30dollars meal":
            price += 30
        elif name == "25dollars meal":
            price += 25

    return price

def predict(input_image):
    """
    Predict model output
    """
    # Disable gradient computation
    with torch.no_grad():
        results = model(input_image)

    results_pd = results.pandas().xyxy[0]
    name, confidence = results_pd["name"], results_pd["confidence"]
    predicted = list(zip(name, confidence))
    output_image = results.render()[0]
    price = total_price(predicted)

    # Return the output image and price
    return [output_image, price]

with gr.Blocks() as demo:
    # Title
    gr.HTML(
    """
    <h1 align="center">Group 29 - AI Cafeteria Price Evaluator</h1>
    """)
    examples = ["./examples/img_1.jpg", "./examples/img_2.jpg", "./examples/img_3.jpg",
                "./examples/img_4.jpg", "./examples/img_5.jpg", "./examples/img_6.jpg"]
    # gr.Interface(inputs=["image"],outputs=["image"],fn=lambda img:model(img).render()[0]).launch()
    gr.Interface(inputs=["image"], outputs=["image", "text"], fn=predict, examples=examples)

    gr.HTML(
    """
    <style>
        table.center {
            margin-left: auto; 
            margin-right: auto;
        }
        .center th{
            text-align: center;
        }
    </style>
    <h1 align="center">Price List</h1>
    <h3>
    <table class="center">
        <tr>
            <th style="text-align: center">Menu</th>
            <th style="text-align: center">Price</th>
        </tr>
        <tr>
            <td>White/Brown/Purple Rice</td>
            <td>20</td>
        </tr>
        <tr>
            <td>Side Dish</td>
            <td>10</td>
        </tr>
        <tr>
            <td>25 Dollar Meal</td>
            <td>25</td>
        </tr>
        <tr>
            <td>30 Dollar Meal</td>
            <td>30</td>
        </tr>
        <tr>
            <td>40 Dollar Meal</td>
            <td>40</td>
        </tr>
    </table>
    </h3>
    """)
    
if __name__ == "__main__":
    demo.launch()