melindakhosasih
add price and examples
c970970
raw history blame
No virus
1.75 kB
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 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)
predicted = results.pandas().xyxy[0]["name"]
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"]
# 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)
if __name__ == "__main__":
demo.launch()