-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.py
179 lines (117 loc) · 5.75 KB
/
scan.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import streamlit as st
import pandas as pd
import numpy as np
from keras.models import load_model
from PIL import Image
import tensorflow as tf
import base64
from googletrans import Translator
def run():
translator = Translator()
def translate_text(text, dest_language):
language_codes = {
'English': 'en',
'Hindi': 'hi',
'Gujarati': 'gu',
'Marathi': 'mr'
}
return translator.translate(text, dest=language_codes[dest_language]).text
def translate_page(elements, target_language):
translated_elements = [translate_text(element, target_language) for element in elements]
return translated_elements
@st.cache_data
def load_data():
df = pd.read_excel('wateer.xlsx')
return df
df = load_data()
class_names_model1_model3 = df['Item'].tolist()
class_names_model2 = ['apple', 'banana', 'beetroot', 'bell pepper', 'cabbage', 'capsicum', 'carrot',
'cauliflower', 'chilli pepper', 'corn', 'cucumber', 'eggplant', 'garlic', 'ginger',
'grapes', 'jalepeno', 'kiwi', 'lemon', 'lettuce', 'mango', 'onion', 'orange', 'paprika',
'pear', 'peas', 'pineapple', 'pomegranate', 'potato', 'raddish', 'soy beans', 'spinach',
'sweetcorn', 'sweetpotato', 'tomato', 'turnip', 'watermelon']
@st.cache_resource
def load_models():
model1 = load_model('model1.h5')
model2 = load_model('FV_1.h5')
model3 = load_model('model3.h5')
return model1, model2, model3
model1, model2, model3 = load_models()
input_size_model1 = (224, 224)
input_size_model2 = (224, 224)
input_size_model3 = (150, 150)
def preprocess_image(image, target_size):
img = image.resize(target_size)
img = np.array(img)
img = img / 255.0
img = np.expand_dims(img, axis=0)
return img
def get_base64_of_bin_file(bin_file):
with open(bin_file, 'rb') as f:
data = f.read()
return base64.b64encode(data).decode()
gif_path = "green.gif"
base64_gif = get_base64_of_bin_file(gif_path)
page_bg = f"""
<style>
[data-testid="stAppViewContainer"] {{
background-image: url("data:image/gif;base64,{base64_gif}");
background-size: cover;
background-position: center;
}}
.black-text {{
color: black;
margin-left: 480px;
}}
.custom-heading {{
color: black;
margin-left: 480px;
font-size: 35px;
}}
</style>
"""
st.markdown(page_bg, unsafe_allow_html=True)
language = st.sidebar.selectbox(
'Select Language',
('English', 'Hindi', 'Gujarati', 'Marathi')
)
st.title(translate_text("Water Footprint Estimation from Image", language))
uploaded_file = st.file_uploader(translate_text("Upload an image", language), type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption=translate_text("Uploaded Image", language), use_column_width=True)
processed_image1 = preprocess_image(image, input_size_model1)
processed_image2 = preprocess_image(image, input_size_model2)
processed_image3 = preprocess_image(image, input_size_model3)
pred1 = model1.predict(processed_image1)
pred2 = model2.predict(processed_image2)
pred3 = model3.predict(processed_image3)
identified_item = None
model_used = None
if np.max(pred2) > 0.5:
identified_item = class_names_model2[np.argmax(pred2)]
model_used = "Model 2"
elif np.max(pred1) > 0.5:
identified_item = class_names_model1_model3[np.argmax(pred1)]
model_used = "Model 1"
elif np.max(pred3) > 0.5:
identified_item = class_names_model1_model3[np.argmax(pred3)]
model_used = "Model 3"
if identified_item:
st.success(f"{translate_text('Identified Item', language)}: {identified_item} ({translate_text('Identified by', language)} {model_used})")
identified_item_cleaned = identified_item.strip().lower()
df['Item_cleaned'] = df['Item'].str.strip().str.lower()
if identified_item_cleaned in df['Item_cleaned'].values:
water_footprint = df[df['Item_cleaned'] == identified_item_cleaned]['WaterFootprint'].values[0]
quantity = st.number_input(f"{translate_text('Enter quantity of', language)} {identified_item} ({translate_text('in kg', language)}):", min_value=0.0, value=1.0)
if st.button(translate_text("Calculate Water Footprint", language)):
image_file_path = 'wb.jpg'
st.image(image_file_path, caption=translate_text("Water Footprint", language), use_column_width=False, width=300)
total_water_footprint = water_footprint * quantity
st.write(f"The total water footprint for {quantity} kg of {identified_item} is {total_water_footprint:.2f} 1-liter bottles.")
else:
st.error(f"Item '{identified_item}' not found in the Excel sheet.")
else:
st.error("No model was able to identify the image. Please try a different image.")
else:
st.write("Please upload an image to begin.")