Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import streamlit as st
|
|
| 5 |
from PIL import Image
|
| 6 |
from collections import Counter
|
| 7 |
import os
|
| 8 |
-
import requests
|
| 9 |
|
| 10 |
# Streamlit app title
|
| 11 |
st.set_page_config(page_title="Object Detection App", page_icon="🖼️", layout="centered")
|
|
@@ -22,48 +21,23 @@ The application will display the image with bounding boxes around the objects.
|
|
| 22 |
st.markdown("### Step 1: Upload an Image for Object Detection")
|
| 23 |
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
| 24 |
|
| 25 |
-
# Function to download
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
print(f"Downloading {file_name}...")
|
| 30 |
-
# Disable the progress bar by using requests
|
| 31 |
-
response = requests.get(url, stream=True)
|
| 32 |
-
with open(file_path, 'wb') as f:
|
| 33 |
-
for chunk in response.iter_content(chunk_size=1024):
|
| 34 |
-
if chunk:
|
| 35 |
-
f.write(chunk)
|
| 36 |
-
return file_path
|
| 37 |
-
|
| 38 |
-
# Function to perform object detection with manual model file paths
|
| 39 |
-
def detect_common_objects_skip_progress(image, model='yolov3', skip_progress=False):
|
| 40 |
-
if skip_progress:
|
| 41 |
-
# Manually set paths for config and weights if progress bar should be disabled
|
| 42 |
-
model_config = 'path_to_config.cfg' # Provide the path to the .cfg file
|
| 43 |
-
model_weights = 'path_to_weights.weights' # Provide the path to the .weights file
|
| 44 |
-
else:
|
| 45 |
-
# Download the config and weights files if not already present
|
| 46 |
-
model_config = download_file_no_progress(
|
| 47 |
-
url="https://github.com/opencv/opencv_contrib/blob/master/modules/dnn/data/yolov3.cfg",
|
| 48 |
-
file_name="yolov3.cfg",
|
| 49 |
-
dest_dir="."
|
| 50 |
-
)
|
| 51 |
-
model_weights = download_file_no_progress(
|
| 52 |
-
url="https://github.com/opencv/opencv_contrib/blob/master/modules/dnn/data/yolov3.weights",
|
| 53 |
-
file_name="yolov3.weights",
|
| 54 |
-
dest_dir="."
|
| 55 |
-
)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
box, label, count = cv.detect_common_objects(image, model=model
|
|
|
|
|
|
|
| 59 |
return box, label, count
|
| 60 |
|
| 61 |
if uploaded_file is not None:
|
| 62 |
# Convert image file to OpenCV format
|
| 63 |
image = np.array(Image.open(uploaded_file))
|
| 64 |
|
| 65 |
-
# Perform object detection with
|
| 66 |
-
box, label, count = detect_common_objects_skip_progress(image
|
| 67 |
|
| 68 |
# Draw bounding boxes on the image
|
| 69 |
output_image = draw_bbox(image, box, label, count)
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from collections import Counter
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
# Streamlit app title
|
| 10 |
st.set_page_config(page_title="Object Detection App", page_icon="🖼️", layout="centered")
|
|
|
|
| 21 |
st.markdown("### Step 1: Upload an Image for Object Detection")
|
| 22 |
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
|
| 23 |
|
| 24 |
+
# Function to skip download and use local files for the model
|
| 25 |
+
def detect_common_objects_skip_progress(image, model='yolov3'):
|
| 26 |
+
model_config = 'yolov3.cfg' # Path to your downloaded .cfg file
|
| 27 |
+
model_weights = 'yolov3.weights' # Path to your downloaded .weights file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Perform object detection using the provided model files
|
| 30 |
+
box, label, count = cv.detect_common_objects(image, model=model,
|
| 31 |
+
model_config=model_config,
|
| 32 |
+
model_weights=model_weights)
|
| 33 |
return box, label, count
|
| 34 |
|
| 35 |
if uploaded_file is not None:
|
| 36 |
# Convert image file to OpenCV format
|
| 37 |
image = np.array(Image.open(uploaded_file))
|
| 38 |
|
| 39 |
+
# Perform object detection with manually provided paths
|
| 40 |
+
box, label, count = detect_common_objects_skip_progress(image)
|
| 41 |
|
| 42 |
# Draw bounding boxes on the image
|
| 43 |
output_image = draw_bbox(image, box, label, count)
|