Nada2001 commited on
Commit
047d106
·
1 Parent(s): d3a8b16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -15
app.py CHANGED
@@ -1,17 +1,121 @@
1
  import streamlit as st
2
  import torch
3
- import os
4
-
5
- def getResult(filename):
6
- model_name='best.pt'
7
- model = torch.hub.load( " ",'custom', source='local', path = model_name, force_reload = True)
8
- img = "/content/"+filename+""
9
- results = model(img)
10
- return results
11
-
12
- uploaded_file = st.file_uploader("Choose a image file")
13
- if uploaded_file is not None:
14
- image = uploaded_file.read()
15
- filename = uploaded_file.name
16
- resulted_img = getResult(filename)
17
- img = st.image(resulted_img, caption='Sunrise by the mountains', use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import torch
3
+ from detect import detect
4
+ from PIL import Image
5
+ from io import *
6
+ import glob
7
+ from datetime import datetime
8
+ import os
9
+ import wget
10
+ import time
11
+
12
+ ## CFG
13
+ cfg_model_path = "best.pt"
14
+
15
+ cfg_enable_url_download = True
16
+ if cfg_enable_url_download:
17
+ url = "https://archive.org/download/best_20230416/best.pt" #Configure this if you set cfg_enable_url_download to True
18
+ cfg_model_path = f"models/{url.split('/')[-1:][0]}" #config model path from url name
19
+ ## END OF CFG
20
+
21
+
22
+
23
+
24
+
25
+
26
+ def imageInput(device, src):
27
+
28
+ if src == 'Upload your own data.':
29
+ image_file = st.file_uploader("Upload An Image", type=['png', 'jpeg', 'jpg'])
30
+ col1, col2 = st.columns(2)
31
+ if image_file is not None:
32
+ img = Image.open(image_file)
33
+ with col1:
34
+ st.image(img, caption='Uploaded Image', use_column_width='always')
35
+ ts = datetime.timestamp(datetime.now())
36
+ imgpath = os.path.join('data/uploads', str(ts)+image_file.name)
37
+ outputpath = os.path.join('data/outputs', os.path.basename(imgpath))
38
+ with open(imgpath, mode="wb") as f:
39
+ f.write(image_file.getbuffer())
40
+
41
+ #call Model prediction--
42
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path='models/yoloTrained.pt', force_reload=True)
43
+ model.cuda() if device == 'cuda' else model.cpu()
44
+ pred = model(imgpath)
45
+ pred.render() # render bbox in image
46
+ for im in pred.ims:
47
+ im_base64 = Image.fromarray(im)
48
+ im_base64.save(outputpath)
49
+
50
+ #--Display predicton
51
+
52
+ img_ = Image.open(outputpath)
53
+ with col2:
54
+ st.image(img_, caption='Model Prediction(s)', use_column_width='always')
55
+
56
+ elif src == 'From test set.':
57
+ # Image selector slider
58
+ imgpath = glob.glob('data/images/*')
59
+ imgsel = st.slider('Select random images from test set.', min_value=1, max_value=len(imgpath), step=1)
60
+ image_file = imgpath[imgsel-1]
61
+ submit = st.button("Predict!")
62
+ col1, col2 = st.columns(2)
63
+ with col1:
64
+ img = Image.open(image_file)
65
+ st.image(img, caption='Selected Image', use_column_width='always')
66
+ with col2:
67
+ if image_file is not None and submit:
68
+ #call Model prediction--
69
+ model = torch.hub.load('ultralytics/yolov5', 'custom', path=cfg_model_path, force_reload=True)
70
+ pred = model(image_file)
71
+ pred.render() # render bbox in image
72
+ for im in pred.ims:
73
+ im_base64 = Image.fromarray(im)
74
+ im_base64.save(os.path.join('data/outputs', os.path.basename(image_file)))
75
+ #--Display predicton
76
+ img_ = Image.open(os.path.join('data/outputs', os.path.basename(image_file)))
77
+ st.image(img_, caption='Model Prediction(s)')
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+ def main():
86
+ # -- Sidebar
87
+ st.sidebar.title('⚙️Options')
88
+ datasrc = st.sidebar.radio("Select input source.", ['From test set.', 'Upload your own data.'])
89
+
90
+
91
+
92
+ option = st.sidebar.radio("Select input type.", ['Image', 'Video'])
93
+ if torch.cuda.is_available():
94
+ deviceoption = st.sidebar.radio("Select compute Device.", ['cpu', 'cuda'], disabled = False, index=1)
95
+ else:
96
+ deviceoption = st.sidebar.radio("Select compute Device.", ['cpu', 'cuda'], disabled = True, index=0)
97
+ # -- End of Sidebar
98
+
99
+ st.header('📦Obstacle Detection')
100
+ st.subheader('👈🏽 Select options left-haned menu bar.')
101
+ st.sidebar.markdown("https://github.com/thepbordin/Obstacle-Detection-for-Blind-people-Deployment")
102
+ if option == "Image":
103
+ imageInput(deviceoption, datasrc)
104
+ elif option == "Video":
105
+ videoInput(deviceoption, datasrc)
106
+
107
+
108
+
109
+ if __name__ == '__main__':
110
+
111
+ main()
112
+
113
+ # Downlaod Model from url.
114
+ @st.cache
115
+ def loadModel():
116
+ start_dl = time.time()
117
+ model_file = wget.download(url, out="models/")
118
+ finished_dl = time.time()
119
+ print(f"Model Downloaded, ETA:{finished_dl-start_dl}")
120
+ if cfg_enable_url_download:
121
+ loadModel()