Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,41 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
# ---
|
| 9 |
-
|
| 10 |
-
GROQ_API_KEY = os.getenv("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")
|
| 11 |
-
GROQ_API_URL = "https://api.groq.com/v1/inference/yolov8"
|
| 12 |
|
| 13 |
# --- Streamlit UI ---
|
| 14 |
-
st.title("📸 Object Counter App")
|
| 15 |
-
st.write("Upload an image and count the objects using YOLOv8 + Groq API!")
|
| 16 |
|
| 17 |
uploaded_file = st.file_uploader("Upload an Image", type=['jpg', 'jpeg', 'png'])
|
| 18 |
|
| 19 |
if uploaded_file is not None:
|
| 20 |
-
image = Image.open(uploaded_file)
|
| 21 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 22 |
|
| 23 |
-
# Convert image to base64 for API
|
| 24 |
-
buffered = io.BytesIO()
|
| 25 |
-
image.save(buffered, format="JPEG")
|
| 26 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 27 |
-
|
| 28 |
st.write("🔎 Detecting objects...")
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 33 |
-
"Content-Type": "application/json"
|
| 34 |
-
}
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"inputs": {
|
| 39 |
-
"image": img_str
|
| 40 |
-
}
|
| 41 |
-
}
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
result = response.json()
|
| 47 |
-
detected_objects = result.get("detections", [])
|
| 48 |
-
count = len(detected_objects)
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
st.write(f"Detected items: {object_names}")
|
| 55 |
|
| 56 |
-
|
| 57 |
-
st.error(f"Error: {response.status_code} - {response.text}")
|
| 58 |
|
| 59 |
-
st.write("Made with ❤️ using YOLOv8 +
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
|
|
|
| 6 |
|
| 7 |
+
# --- Load YOLOv8 Model ---
|
| 8 |
+
model = YOLO('yolov8n.pt') # Nano model, fast and small
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# --- Streamlit UI ---
|
| 11 |
+
st.title("📸 Object Counter App (YOLOv8)")
|
|
|
|
| 12 |
|
| 13 |
uploaded_file = st.file_uploader("Upload an Image", type=['jpg', 'jpeg', 'png'])
|
| 14 |
|
| 15 |
if uploaded_file is not None:
|
| 16 |
+
image = Image.open(uploaded_file).convert('RGB')
|
| 17 |
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
st.write("🔎 Detecting objects...")
|
| 20 |
|
| 21 |
+
# Convert PIL image to NumPy array
|
| 22 |
+
img_array = np.array(image)
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Inference
|
| 25 |
+
results = model.predict(img_array)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Get number of detected objects
|
| 28 |
+
num_objects = len(results[0].boxes)
|
| 29 |
|
| 30 |
+
st.success(f"✅ Total objects detected: {num_objects}")
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# List detected object classes
|
| 33 |
+
class_names = [model.names[int(cls)] for cls in results[0].boxes.cls]
|
| 34 |
+
st.write(f"Detected Items: {class_names}")
|
| 35 |
|
| 36 |
+
# Draw boxes on image
|
| 37 |
+
result_img = results[0].plot()
|
|
|
|
| 38 |
|
| 39 |
+
st.image(result_img, caption='Detected Objects', use_column_width=True)
|
|
|
|
| 40 |
|
| 41 |
+
st.write("Made with ❤️ using YOLOv8 + Streamlit")
|