Beasto commited on
Commit
7d484d7
·
verified ·
1 Parent(s): 2b28052

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -2
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import streamlit as st
 
2
  from streamlit_option_menu import option_menu
3
  import tensorflow as tf
4
  import json
5
  from streamlit_lottie import st_lottie
6
- from PIL import Image
7
  from tensorflow.keras.models import load_model
8
  import numpy as np
9
  from tensorflow.keras.utils import custom_object_scope
@@ -23,6 +24,15 @@ def img_prep(paths):
23
  out.append(img)
24
  return out
25
 
 
 
 
 
 
 
 
 
 
26
  def model_out(img,model_path):
27
  img = ((np.asarray(img))/127.5)-1
28
  img = np.expand_dims(img,0)
@@ -30,7 +40,26 @@ def model_out(img,model_path):
30
  model = load_model(model_path)
31
  pred = ((model.predict(img)+1)*127.5)/255
32
  return pred[0]
33
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  def lottiemaker(path):
35
  with open(path) as f:
36
  return json.load(f)
@@ -188,6 +217,26 @@ elif selected == 'Projects':
188
  st.image(model_out(imgs[0],'Portfolio Projects/FloodAreaSegmentationUnetPix2Pix.h5'),use_column_width=False)
189
  if img_selected_option == 'img2':
190
  st.image(model_out(imgs[1],'Portfolio Projects/FloodAreaSegmentationUnetPix2Pix.h5'),use_column_width=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
 
192
  elif selected_option == 'Machine Learning Text classifier':
193
 
 
1
  import streamlit as st
2
+ from ultralytics import YOLO
3
  from streamlit_option_menu import option_menu
4
  import tensorflow as tf
5
  import json
6
  from streamlit_lottie import st_lottie
7
+ from PIL import Image,ImageFont,ImageDraw
8
  from tensorflow.keras.models import load_model
9
  import numpy as np
10
  from tensorflow.keras.utils import custom_object_scope
 
24
  out.append(img)
25
  return out
26
 
27
+ def img_prep_YOLO(paths,size):
28
+ out = []
29
+ for i in paths:
30
+ img = Image.open(i)
31
+ img = img.resize((size,size))
32
+ img = img.convert('RGB')
33
+ out.append(img)
34
+ return out
35
+
36
  def model_out(img,model_path):
37
  img = ((np.asarray(img))/127.5)-1
38
  img = np.expand_dims(img,0)
 
40
  model = load_model(model_path)
41
  pred = ((model.predict(img)+1)*127.5)/255
42
  return pred[0]
43
+
44
+ def yolo_out(model,img)
45
+ model = YOLO(model)
46
+ results = model(img)
47
+ for result in results:
48
+ cls = result.boxes.cls[0]
49
+ cls = arr[int(cls)]
50
+ lbl = result.boxes.conf[0]
51
+ boxes = result.boxes.xyxy[0]
52
+ draw = ImageDraw.Draw(img)
53
+ draw.rectangle([boxes[0], boxes[1], boxes[2], boxes[3]], outline="black", width=5)
54
+ text_position = (boxes[0]+boxes[2])/2, boxes[1]-10
55
+ draw.text(text_position, f'{cls} {lbl}', fill="red", font=font)
56
+
57
+ return img
58
+
59
+ font_size = 40
60
+ font = ImageFont.truetype("arial.ttf", size=font_size)
61
+ arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
62
+
63
  def lottiemaker(path):
64
  with open(path) as f:
65
  return json.load(f)
 
217
  st.image(model_out(imgs[0],'Portfolio Projects/FloodAreaSegmentationUnetPix2Pix.h5'),use_column_width=False)
218
  if img_selected_option == 'img2':
219
  st.image(model_out(imgs[1],'Portfolio Projects/FloodAreaSegmentationUnetPix2Pix.h5'),use_column_width=False)
220
+
221
+ if selected_option == 'YOLO object detection':
222
+ st.header('Description')
223
+ st.write('This is an YOLO mdoel which can detect different hand sign in an image')
224
+ st.header('Architecture')
225
+ st.write('The model uses YOLOv8 to do the detection')
226
+
227
+ imgs = img_prep(['YOLOB.jpg','YOLOC.jpg'],416)
228
+ col1,col2 = st.columns(2)
229
+ with col1:
230
+ st.image(imgs[0],use_column_width=False)
231
+ with col2:
232
+ st.image(imgs[1],use_column_width=False)
233
+
234
+ img_options = {'img1':1, 'img2':2}
235
+ img_selected_option = st.selectbox('Select an Image', img_options)
236
+ if img_selected_option == 'img1':
237
+ st.image(model_out(imgs[0],'Portfolio Projects/HandSignDetector.pt'),use_column_width=False)
238
+ if img_selected_option == 'img2':
239
+ st.image(model_out(imgs[1],'Portfolio Projects/HandSignDetector.pt'),use_column_width=False)
240
 
241
  elif selected_option == 'Machine Learning Text classifier':
242