Surya152002 commited on
Commit
5f84ddc
·
verified ·
1 Parent(s): 9d7bb64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -1,16 +1,18 @@
1
- import cv2
2
  import torch
 
3
  import streamlit as st
4
  from matplotlib import pyplot as plt
5
  import numpy as np
 
 
6
 
7
  # Function to detect circular objects in the image
8
- def detect_circles(image_path):
9
  # Load pre-trained YOLOv5 model
10
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # using a smaller model for faster inference
11
 
12
- # Read the image
13
- img = cv2.imread(image_path)
14
 
15
  # Run the YOLOv5 model on the image
16
  results = model(img)
@@ -32,10 +34,11 @@ image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
32
 
33
  if image_file is not None:
34
  # Open the uploaded image using PIL (Pillow)
35
- st.image(image_file, caption="Uploaded Image", use_column_width=True)
 
36
 
37
  # Process the image
38
- circles_count, results = detect_circles(image_file)
39
 
40
  # Display result
41
  st.write(f"Number of circular objects detected: {circles_count}")
 
 
1
  import torch
2
+ import cv2
3
  import streamlit as st
4
  from matplotlib import pyplot as plt
5
  import numpy as np
6
+ from PIL import Image
7
+ from io import BytesIO
8
 
9
  # Function to detect circular objects in the image
10
+ def detect_circles(image):
11
  # Load pre-trained YOLOv5 model
12
  model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # using a smaller model for faster inference
13
 
14
+ # Convert PIL image to NumPy array
15
+ img = np.array(image)
16
 
17
  # Run the YOLOv5 model on the image
18
  results = model(img)
 
34
 
35
  if image_file is not None:
36
  # Open the uploaded image using PIL (Pillow)
37
+ image = Image.open(image_file)
38
+ st.image(image, caption="Uploaded Image", use_column_width=True)
39
 
40
  # Process the image
41
+ circles_count, results = detect_circles(image)
42
 
43
  # Display result
44
  st.write(f"Number of circular objects detected: {circles_count}")