Commit
·
e19b441
1
Parent(s):
67c09a2
Updated predict function.
Browse files
app.py
CHANGED
|
@@ -51,7 +51,6 @@ food_info = {
|
|
| 51 |
}
|
| 52 |
|
| 53 |
def predict(my_image):
|
| 54 |
-
|
| 55 |
image = Image.fromarray(my_image.astype('uint8'))
|
| 56 |
|
| 57 |
pipe = pipeline("image-classification",
|
|
@@ -60,33 +59,14 @@ def predict(my_image):
|
|
| 60 |
|
| 61 |
pred = pipe(image)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
print(f"Predicted the above image as a {pred[0]['label'].replace('_', ' ').title()} with {pred[0]['score']*100:.2f}% confidence")
|
| 69 |
-
|
| 70 |
-
run = True
|
| 71 |
-
while run:
|
| 72 |
-
|
| 73 |
-
inp = input('Is the prediction correct?')
|
| 74 |
-
if inp.lower() == 'yes':
|
| 75 |
-
print(f"""
|
| 76 |
-
{food_info[pred[0]['label'].replace('_', ' ').title()]['Description']}
|
| 77 |
-
|
| 78 |
-
Info: {food_info[pred[0]['label'].replace('_', ' ').title()]['Calories and Health Info']}""")
|
| 79 |
-
run = False
|
| 80 |
-
elif inp.lower() == 'no':
|
| 81 |
-
print(f"""
|
| 82 |
-
The image could be a {pred[1]['label'].replace('_', ' ').title()}, with a {pred[1]['score']*100:.2f}% confidence,
|
| 83 |
-
The image could be a {pred[2]['label'].replace('_', ' ').title()}, with a {pred[2]['score']*100:.2f}% confidence,
|
| 84 |
-
The image could be a {pred[3]['label'].replace('_', ' ').title()}, with a {pred[3]['score']*100:.2f}% confidence,
|
| 85 |
-
Or the image could be a {pred[4]['label'].replace('_', ' ').title()}, with a {pred[4]['score']*100:.2f}% confidence,
|
| 86 |
-
""")
|
| 87 |
-
run = False
|
| 88 |
-
else:
|
| 89 |
-
print('Please respond as yes or no')
|
| 90 |
|
| 91 |
-
iface = gr.Interface(fn=predict,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
iface.launch()
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
def predict(my_image):
|
|
|
|
| 54 |
image = Image.fromarray(my_image.astype('uint8'))
|
| 55 |
|
| 56 |
pipe = pipeline("image-classification",
|
|
|
|
| 59 |
|
| 60 |
pred = pipe(image)
|
| 61 |
|
| 62 |
+
res = {}
|
| 63 |
+
for i in pred:
|
| 64 |
+
res[i['label'].replace('_', ' ').title()] = round(i['score'])
|
| 65 |
+
return res, food_info[pred[0]['label'].replace('_', ' ').title()]['Description'],food_info[pred[0]['label'].replace('_', ' ').title()]['Calories and Health Info']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
iface = gr.Interface(fn=predict,
|
| 68 |
+
inputs='image',
|
| 69 |
+
outputs=[gr.Label(num_top_classes=5, label="Predictions"),
|
| 70 |
+
gr.Text(label='Description'),
|
| 71 |
+
gr.Text(label='Calories and Health Info')])
|
| 72 |
iface.launch()
|