Vishnu Raja commited on
Commit
58bd1b2
·
1 Parent(s): 8ba5e32

Add application file

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ import streamlit as st
3
+ import numpy as np
4
+ from PIL import Image
5
+ import urllib.request
6
+ from utils import *
7
+ import requests
8
+
9
+ labels = gen_labels()
10
+
11
+ html_temp = '''
12
+ <div style="padding-bottom: 20px; padding-top: 20px; padding-left: 5px; padding-right: 5px">
13
+ <center><h1>Garbage Segregation</h1></center>
14
+ </div>
15
+ '''
16
+
17
+ st.markdown(html_temp, unsafe_allow_html=True)
18
+
19
+ html_temp = '''
20
+ <div>
21
+ <h2></h2>
22
+ <center><h3>Please upload Waste Image to find its Category</h3></center>
23
+ </div>
24
+ '''
25
+
26
+ st.markdown(html_temp, unsafe_allow_html=True)
27
+
28
+ opt = st.selectbox("How do you want to upload the image for classification?\n", ('Please Select', 'Upload image via link', 'Upload image from device'))
29
+
30
+ image = None # Initialize image variable
31
+
32
+ if opt == 'Upload image from device':
33
+ file = st.file_uploader('Select', type=['jpg', 'png', 'jpeg'])
34
+ if file is not None:
35
+ image = Image.open(file).resize((256, 256), Image.LANCZOS)
36
+
37
+ elif opt == 'Upload image via link':
38
+ try:
39
+ img = st.text_input('Enter the Image Address')
40
+ image = Image.open(urllib.request.urlopen(img)).resize((256, 256), Image.LANCZOS)
41
+ except:
42
+ if st.button('Submit'):
43
+ show = st.error("Please Enter a valid Image Address!")
44
+ time.sleep(4)
45
+ show.empty()
46
+
47
+ try:
48
+ if image is not None:
49
+ st.image(image, width=256, caption='Uploaded Image')
50
+ if st.button('Predict'):
51
+ img = preprocess(image)
52
+
53
+ model = model_arc()
54
+
55
+ image_url = "https://drive.google.com/file/d/1jc_gp8qR8t6e8z8WOA_K-SqfurN7Z8vn/view?usp=sharing"
56
+ r = requests.get(image_url)
57
+
58
+ # Save the content of the response to a temporary file
59
+ with open("temp_weights.h5", "wb") as f:
60
+ f.write(r.content)
61
+
62
+ # Load the weights from the temporary file
63
+ model.load_weights("temp_weights.h5")
64
+
65
+ prediction = model.predict(img)
66
+ st.info('Hey! The uploaded image has been classified as "{} waste" '.format(labels[np.argmax(prediction)]))
67
+ except Exception as e:
68
+ st.info(str(e))