-
Notifications
You must be signed in to change notification settings - Fork 5
/
Fruits_Vegetable_Classification.py
63 lines (57 loc) · 2.75 KB
/
Fruits_Vegetable_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
import streamlit as st
from PIL import Image
from keras.preprocessing.image import load_img,img_to_array
import numpy as np
from keras.models import load_model
import requests
from bs4 import BeautifulSoup
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 able to fetch the Calories")
print(e)
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 = answer.argmax(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 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)
save_image_path = './uploads/'+img_file.name
with open(save_image_path, "wb") as f:
f.write(img_file.getbuffer())
# if st.button("Predict"):
if img_file is not None:
result= processed_img(save_image_path)
print(result)
if result in vegetables:
st.info('**Category : Vegetables**')
else:
st.info('**Category : Fruit**')
st.success("**Predicted : "+result+'**')
cal = fetch_calories(result)
if cal:
st.warning('**'+cal+'(100 grams)**')
run()