Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import requests
|
| 4 |
+
import io
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
# --- CONFIGURATION ---
|
| 8 |
+
# Insert your Groq API key
|
| 9 |
+
GROQ_API_KEY = "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"
|
| 10 |
+
GROQ_API_URL = "https://api.groq.com/v1/inference/yolov8"
|
| 11 |
+
|
| 12 |
+
# --- Streamlit UI ---
|
| 13 |
+
st.title("📸 Object Counter App")
|
| 14 |
+
st.write("Upload an image and count the objects using YOLOv8 + Groq API!")
|
| 15 |
+
|
| 16 |
+
uploaded_file = st.file_uploader("Upload an Image", type=['jpg', 'jpeg', 'png'])
|
| 17 |
+
|
| 18 |
+
if uploaded_file is not None:
|
| 19 |
+
image = Image.open(uploaded_file)
|
| 20 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 21 |
+
|
| 22 |
+
# Convert image to base64 for API
|
| 23 |
+
buffered = io.BytesIO()
|
| 24 |
+
image.save(buffered, format="JPEG")
|
| 25 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 26 |
+
|
| 27 |
+
st.write("🔎 Detecting objects...")
|
| 28 |
+
|
| 29 |
+
# --- Groq API Request ---
|
| 30 |
+
headers = {
|
| 31 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 32 |
+
"Content-Type": "application/json"
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
payload = {
|
| 36 |
+
"model": "yolov8n", # YOLOv8 Nano version (fast & small)
|
| 37 |
+
"inputs": {
|
| 38 |
+
"image": img_str
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
response = requests.post(GROQ_API_URL, json=payload, headers=headers)
|
| 43 |
+
|
| 44 |
+
if response.status_code == 200:
|
| 45 |
+
result = response.json()
|
| 46 |
+
detected_objects = result.get("detections", [])
|
| 47 |
+
count = len(detected_objects)
|
| 48 |
+
|
| 49 |
+
st.success(f"✅ Total objects detected: {count}")
|
| 50 |
+
|
| 51 |
+
# Show each detected object's class
|
| 52 |
+
object_names = [obj['class'] for obj in detected_objects]
|
| 53 |
+
st.write(f"Detected items: {object_names}")
|
| 54 |
+
|
| 55 |
+
# Optional: show bounding boxes if API supports (not visualized here)
|
| 56 |
+
|
| 57 |
+
else:
|
| 58 |
+
st.error(f"Error: {response.status_code} - {response.text}")
|
| 59 |
+
|
| 60 |
+
---
|
| 61 |
+
|
| 62 |
+
st.write("Made with ❤️ using YOLOv8 + Groq + Streamlit")
|