Commit
·
b2fb34e
1
Parent(s):
fbec9fe
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 6 |
+
|
| 7 |
+
image_processor = AutoImageProcessor.from_pretrained("./Mymodel/")
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained("./Mymodel/")
|
| 9 |
+
|
| 10 |
+
def predict(my_image):
|
| 11 |
+
|
| 12 |
+
image = Image.fromarray(image.astype('uint8'))
|
| 13 |
+
|
| 14 |
+
pipe = pipeline("image-classification",
|
| 15 |
+
model=model,
|
| 16 |
+
feature_extractor=image_processor)
|
| 17 |
+
|
| 18 |
+
pred = pipe(image)
|
| 19 |
+
|
| 20 |
+
plt.imshow(image)
|
| 21 |
+
plt.title(pred[0]['label'].replace('_', ' ').title())
|
| 22 |
+
plt.axis(False)
|
| 23 |
+
plt.show()
|
| 24 |
+
|
| 25 |
+
print(f"Predicted the above image as a {pred[0]['label'].replace('_', ' ').title()} with {pred[0]['score']*100:.2f}% confidence")
|
| 26 |
+
|
| 27 |
+
run = True
|
| 28 |
+
while run:
|
| 29 |
+
|
| 30 |
+
inp = input('Is the prediction correct?')
|
| 31 |
+
if inp.lower() == 'yes':
|
| 32 |
+
print(f"""
|
| 33 |
+
{food_info[pred[0]['label'].replace('_', ' ').title()]['Description']}
|
| 34 |
+
|
| 35 |
+
Info: {food_info[pred[0]['label'].replace('_', ' ').title()]['Calories and Health Info']}""")
|
| 36 |
+
run = False
|
| 37 |
+
elif inp.lower() == 'no':
|
| 38 |
+
print(f"""
|
| 39 |
+
The image could be a {pred[1]['label'].replace('_', ' ').title()}, with a {pred[1]['score']*100:.2f}% confidence,
|
| 40 |
+
The image could be a {pred[2]['label'].replace('_', ' ').title()}, with a {pred[2]['score']*100:.2f}% confidence,
|
| 41 |
+
The image could be a {pred[3]['label'].replace('_', ' ').title()}, with a {pred[3]['score']*100:.2f}% confidence,
|
| 42 |
+
Or the image could be a {pred[4]['label'].replace('_', ' ').title()}, with a {pred[4]['score']*100:.2f}% confidence,
|
| 43 |
+
""")
|
| 44 |
+
run = False
|
| 45 |
+
else:
|
| 46 |
+
print('Please respond as yes or no')
|
| 47 |
+
|
| 48 |
+
iface = gr.Interface(fn=predict, inputs="image", outputs="image")
|
| 49 |
+
iface.launch()
|