Abso1ute666's picture
Create app.py
b2fb34e
raw
history blame
1.86 kB
from PIL import Image
import matplotlib.pyplot as plt
import gradio as gr
from transformers import pipeline
from transformers import AutoModelForImageClassification, AutoImageProcessor
image_processor = AutoImageProcessor.from_pretrained("./Mymodel/")
model = AutoModelForImageClassification.from_pretrained("./Mymodel/")
def predict(my_image):
image = Image.fromarray(image.astype('uint8'))
pipe = pipeline("image-classification",
model=model,
feature_extractor=image_processor)
pred = pipe(image)
plt.imshow(image)
plt.title(pred[0]['label'].replace('_', ' ').title())
plt.axis(False)
plt.show()
print(f"Predicted the above image as a {pred[0]['label'].replace('_', ' ').title()} with {pred[0]['score']*100:.2f}% confidence")
run = True
while run:
inp = input('Is the prediction correct?')
if inp.lower() == 'yes':
print(f"""
{food_info[pred[0]['label'].replace('_', ' ').title()]['Description']}
Info: {food_info[pred[0]['label'].replace('_', ' ').title()]['Calories and Health Info']}""")
run = False
elif inp.lower() == 'no':
print(f"""
The image could be a {pred[1]['label'].replace('_', ' ').title()}, with a {pred[1]['score']*100:.2f}% confidence,
The image could be a {pred[2]['label'].replace('_', ' ').title()}, with a {pred[2]['score']*100:.2f}% confidence,
The image could be a {pred[3]['label'].replace('_', ' ').title()}, with a {pred[3]['score']*100:.2f}% confidence,
Or the image could be a {pred[4]['label'].replace('_', ' ').title()}, with a {pred[4]['score']*100:.2f}% confidence,
""")
run = False
else:
print('Please respond as yes or no')
iface = gr.Interface(fn=predict, inputs="image", outputs="image")
iface.launch()