-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fruits_Vegetables_Classification.py
91 lines (75 loc) · 3.32 KB
/
Fruits_Vegetables_Classification.py
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
import streamlit as st
from PIL import Image
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
from tensorflow.keras.models import load_model
import requests
from bs4 import BeautifulSoup
import os
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
model = load_model('FV.h5')
labels = {0: 'apple', 1: 'banana', 2: 'beetroot', 3: 'bell pepper', 4: 'cabbage', 5: 'capsicum', 6: 'carrot',
7: 'cauliflower', 8: 'chilli pepper', 9: 'corn', 10: 'cucumber', 11: 'eggplant', 12: 'garlic', 13: 'ginger',
14: 'grapes', 15: 'jalepeno', 16: 'kiwi', 17: 'lemon', 18: 'lettuce',
19: 'mango', 20: 'onion', 21: 'orange', 22: 'paprika', 23: 'pear', 24: 'peas', 25: 'pineapple',
26: 'pomegranate', 27: 'potato', 28: 'raddish', 29: 'soy beans', 30: 'spinach', 31: 'sweetcorn',
32: 'sweetpotato', 33: 'tomato', 34: 'turnip', 35: 'watermelon'}
fruits = ['Apple', 'Banana', 'Bello Pepper', 'Chilli Pepper', 'Grapes', 'Jalepeno', 'Kiwi', 'Lemon', 'Mango', 'Orange',
'Paprika', 'Pear', 'Pineapple', 'Pomegranate', 'Watermelon']
vegetables = ['Beetroot', 'Cabbage', 'Capsicum', 'Carrot', 'Cauliflower', 'Corn', 'Cucumber', 'Eggplant', 'Ginger',
'Lettuce', 'Onion', 'Peas', 'Potato', 'Raddish', 'Soy Beans', 'Spinach', 'Sweetcorn', 'Sweetpotato',
'Tomato', 'Turnip']
def fetch_calories(prediction):
try:
url = 'https://www.google.com/search?&q=calories in ' + prediction
req = requests.get(url).text
scrap = BeautifulSoup(req, 'html.parser')
calories = scrap.find("div", class_="BNeawe iBp4i AP7Wnd").text
return calories
except Exception as e:
st.error("Can't fetch the Calories")
print(e)
return None
def processed_img(img_path):
img = load_img(img_path, target_size=(224, 224, 3))
img = img_to_array(img)
img = img / 255
img = np.expand_dims(img, [0])
answer = model.predict(img)
y_class = np.argmax(answer, axis=-1)
print(y_class)
y = " ".join(str(x) for x in y_class)
y = int(y)
res = labels[y]
print(res)
return res.capitalize()
def display_results(result, cal):
if result in vegetables:
st.info('**Category: Vegetables**')
else:
st.info('**Category: Fruit**')
st.success("**Predicted: " + result + '**')
if cal:
st.warning('**' + cal + ' (100 grams)**')
def run():
st.title("Fruits 🍎 - Vegetable 🥔 Classification")
img_file = st.file_uploader("Choose an Image", type=["jpg", "png"])
if img_file is not None:
img = Image.open(img_file).resize((250, 250))
st.image(img, use_column_width=False)
# Create the upload_images directory if it doesn't exist
upload_images_dir = './upload_images/'
os.makedirs(upload_images_dir, exist_ok=True)
save_image_path = os.path.join(upload_images_dir, img_file.name)
with open(save_image_path, "wb") as f:
f.write(img_file.getbuffer())
if st.button("Predict"):
try:
result = processed_img(save_image_path)
display_results(result, fetch_calories(result))
except Exception as e:
st.error("Error during prediction.")
print(e)
if __name__ == "__main__":
run()