Abs6187 commited on
Commit
d9291e1
·
verified ·
1 Parent(s): d0ba7e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -28
app.py CHANGED
@@ -1,41 +1,50 @@
1
  import streamlit as st
2
- import kagglehub
3
- import tensorflow as tf
4
  from PIL import Image
5
- import numpy as np
6
 
7
- # Download model from Kaggle Hub
8
- model_path = kagglehub.model_download("sohiebalwedyan/seatbelt-detection/tensorFlow2/seatbelt-detection")
9
- print("Path to model files:", model_path)
10
-
11
- # Load the TensorFlow model
12
- model = tf.saved_model.load(model_path)
13
 
 
14
  def predict(image):
15
- # Preprocess image
16
- img = image.resize((224, 224))
17
- img_array = np.array(img) / 255.0
18
- img_array = np.expand_dims(img_array, axis=0)
19
-
20
- # Run inference
21
- predictions = model(tf.convert_to_tensor(img_array, dtype=tf.float32))
22
-
23
- # Process predictions (modify based on actual model output)
24
- # This is a placeholder and may need adjustment based on model specifics
25
- st.write("Raw predictions:", predictions)
26
-
27
- return image # Placeholder return
28
 
29
- # Streamlit UI
30
- st.title("Seatbelt Detection")
31
- st.markdown("Upload an image for seatbelt detection.")
32
 
 
33
  uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
34
 
35
  if uploaded_image is not None:
 
36
  image = Image.open(uploaded_image)
 
 
37
  st.image(image, caption="Uploaded Image", use_column_width=True)
38
-
 
39
  st.subheader("Prediction Results:")
40
- result = predict(image)
41
- # Note: You may need to modify visualization based on model output
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from ultralytics import YOLO
 
3
  from PIL import Image
4
+ import os
5
 
6
+ # Load the trained YOLOv8 model
7
+ model = YOLO("yolov8n.pt")
 
 
 
 
8
 
9
+ # Define the prediction function
10
  def predict(image):
11
+ results = model(image) # Run YOLOv8 model on the uploaded image
12
+ results_img = results[0].plot() # Get image with bounding boxes
13
+ return Image.fromarray(results_img)
14
+
15
+ # Get example images from the images folder
16
+ def get_example_images():
17
+ examples = []
18
+ image_folder = "images"
19
+ for filename in os.listdir(image_folder):
20
+ if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
21
+ examples.append(os.path.join(image_folder, filename))
22
+ return examples
 
23
 
24
+ # Streamlit UI for Helmet Detection with YOLO
25
+ st.title("Helmet Detection with YOLOv8")
26
+ st.markdown("Upload an image to detect helmets.")
27
 
28
+ # Allow the user to upload an image
29
  uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
30
 
31
  if uploaded_image is not None:
32
+ # Open the uploaded image using PIL
33
  image = Image.open(uploaded_image)
34
+
35
+ # Display the uploaded image
36
  st.image(image, caption="Uploaded Image", use_column_width=True)
37
+
38
+ # Run the model prediction
39
  st.subheader("Prediction Results:")
40
+ result_image = predict(image)
41
+
42
+ # Display the result image with bounding boxes
43
+ st.image(result_image, caption="Detected Image", use_column_width=True)
44
+
45
+ # Optionally, show example images from the folder
46
+ if st.checkbox('Show example images'):
47
+ example_images = get_example_images()
48
+ for example_image in example_images:
49
+ img = Image.open(example_image)
50
+ st.image(img, caption=os.path.basename(example_image), use_column_width=True)