Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- 2.py +59 -0
- Dockerfile +19 -0
- requirements.txt +2 -0
2.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# import pkg_resources
|
| 8 |
+
|
| 9 |
+
# def generate_requirements(filename="requirements.txt"):
|
| 10 |
+
# """
|
| 11 |
+
# Generate a requirements.txt file containing installed pip packages and versions.
|
| 12 |
+
# """
|
| 13 |
+
# # Get all installed packages
|
| 14 |
+
# installed_packages = pkg_resources.working_set
|
| 15 |
+
# packages_list = sorted([f"{pkg.key}=={pkg.version}" for pkg in installed_packages])
|
| 16 |
+
|
| 17 |
+
# # Write to requirements.txt
|
| 18 |
+
# with open(filename, "w") as f:
|
| 19 |
+
# for package in packages_list:
|
| 20 |
+
# f.write(package + "\n")
|
| 21 |
+
|
| 22 |
+
# print(f"Requirements saved to {filename}")
|
| 23 |
+
|
| 24 |
+
# if __name__ == "__main__":
|
| 25 |
+
# generate_requirements()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Load YOLO model (COCO-pretrained)
|
| 29 |
+
model = YOLO('yolov8s.pt') # or 'yolov10s.pt'
|
| 30 |
+
|
| 31 |
+
st.title("YOLO Object Detection with Streamlit")
|
| 32 |
+
st.write("Upload an image and see object detection results in real-time!")
|
| 33 |
+
|
| 34 |
+
# Upload image
|
| 35 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 36 |
+
|
| 37 |
+
if uploaded_file is not None:
|
| 38 |
+
# Convert uploaded file to PIL image
|
| 39 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 40 |
+
st.image(image, caption="Uploaded Image", width='stretch')
|
| 41 |
+
|
| 42 |
+
# Temporary save to disk for YOLO inference
|
| 43 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp_file:
|
| 44 |
+
temp_path = tmp_file.name
|
| 45 |
+
image.save(temp_path)
|
| 46 |
+
|
| 47 |
+
# Run YOLO inference
|
| 48 |
+
results = model(temp_path)
|
| 49 |
+
|
| 50 |
+
# Show detection results in Streamlit
|
| 51 |
+
st.image(results[0].plot(), caption="Detected Objects", width='stretch')
|
| 52 |
+
|
| 53 |
+
# # Save results to output folder
|
| 54 |
+
# output_dir = "outputs"
|
| 55 |
+
# os.makedirs(output_dir, exist_ok=True)
|
| 56 |
+
# results.save(output_dir)
|
| 57 |
+
# st.write(f"Detection results saved in `{output_dir}/`")
|
| 58 |
+
# Clean up temporary file
|
| 59 |
+
os.remove(temp_path)
|
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Base image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy requirements and install dependencies
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir --upgrade pip
|
| 10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
+
|
| 12 |
+
# Copy project files
|
| 13 |
+
COPY . .
|
| 14 |
+
|
| 15 |
+
# Expose port
|
| 16 |
+
EXPOSE 8000
|
| 17 |
+
|
| 18 |
+
# Default command
|
| 19 |
+
CMD ["python", "2.py"]
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.50.0
|
| 2 |
+
ultralytics==8.3.220
|