Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from EnYa32/ObjectDetection: direct link, hf CLI and curl.
- Browser
- Download file 4.11 kB
-
https://huggingface.co/spaces/EnYa32/ObjectDetection/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/EnYa32/ObjectDetection/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/EnYa32/ObjectDetection/resolve/main/src/streamlit_app.py
4.11 kB
| import json | |
| import os | |
| import streamlit as st | |
| import cv2 | |
| import numpy as np | |
| from PIL import Image | |
| import cvlib as cv | |
| from cvlib.object_detection import draw_bbox | |
| st.set_page_config(page_title="Vehicle Counter", layout="wide") | |
| st.title("🚗 Vehicle Counter (JSON Results)") | |
| # ---------- Helpers ---------- | |
| def load_json(path: str): | |
| with open(path, "r", encoding="utf-8") as f: | |
| return json.load(f) | |
| def show_detected_image(path: str): | |
| img = cv2.imread(path) | |
| if img is None: | |
| st.warning(f"Could not read image: {path}") | |
| return | |
| st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), caption="Detected Image", use_container_width=True) | |
| def detect_and_save(uploaded_file, out_json="src/results.json", out_img="src/detected.jpg", conf_th=0.25): | |
| pil_img = Image.open(uploaded_file).convert("RGB") | |
| rgb = np.array(pil_img) | |
| bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR) | |
| bbox, labels, conf = cv.detect_common_objects(bgr, confidence=conf_th, model="yolov3-tiny") | |
| output = draw_bbox(bgr, bbox, labels, conf) | |
| results = { | |
| "cars": labels.count("car"), | |
| "trucks": labels.count("truck") | |
| } | |
| cv2.imwrite(out_img, output) | |
| with open(out_json, "w", encoding="utf-8") as f: | |
| json.dump(results, f, indent=2) | |
| return results, out_img, out_json | |
| # ---------- Layout ---------- | |
| st.subheader("1) Load existing results.json (from your project folder)") | |
| # Erwartet, dass results.json im Repo-Root liegt | |
| json_path = "src/results.json" | |
| img_path = "src/detected.jpg" | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| if os.path.exists(json_path): | |
| data = load_json(json_path) | |
| st.success("✅ Loaded results.json") | |
| st.metric("Cars", int(data.get("cars", 0))) | |
| st.metric("Trucks", int(data.get("trucks", 0))) | |
| st.code(json.dumps(data, indent=2), language="json") | |
| with open(json_path, "rb") as f: | |
| st.download_button("⬇️ Download results.json", f, file_name="results.json") | |
| else: | |
| st.warning("results.json not found in the app folder.") | |
| st.write("➡️ Upload a JSON below or use the detection section to create it.") | |
| with col2: | |
| if os.path.exists(img_path): | |
| show_detected_image(img_path) | |
| with open(img_path, "rb") as f: | |
| st.download_button("⬇️ Download detected.jpg", f, file_name="detected.jpg") | |
| else: | |
| st.info("detected.jpg not found (optional).") | |
| st.divider() | |
| st.subheader("2) Upload a results.json (optional)") | |
| uploaded_json = st.file_uploader("Upload results.json", type=["json"], key="json_uploader") | |
| if uploaded_json is not None: | |
| try: | |
| uploaded_data = json.load(uploaded_json) | |
| st.success("✅ JSON uploaded and parsed") | |
| st.metric("Cars", int(uploaded_data.get("cars", 0))) | |
| st.metric("Trucks", int(uploaded_data.get("trucks", 0))) | |
| st.code(json.dumps(uploaded_data, indent=2), language="json") | |
| except Exception as e: | |
| st.error(f"Could not read JSON: {e}") | |
| st.divider() | |
| st.subheader("3) (Optional) Upload an image and regenerate results.json + detected.jpg") | |
| st.caption("If you upload an image, the app will run detection and overwrite results.json and detected.jpg in the app runtime.") | |
| uploaded_img = st.file_uploader("Upload image (jpg/png/webp)", type=["jpg", "jpeg", "png", "webp"], key="img_uploader") | |
| conf_th = st.slider("Confidence threshold", 0.10, 0.90, 0.25, 0.05) | |
| if uploaded_img is not None: | |
| with st.spinner("Detecting objects and saving results..."): | |
| results, out_img, out_json = detect_and_save( | |
| uploaded_img, out_json="results.json", out_img="detected.jpg", conf_th=conf_th | |
| ) | |
| st.success("✅ Saved new detected.jpg and results.json") | |
| st.metric("Cars", results["cars"]) | |
| st.metric("Trucks", results["trucks"]) | |
| show_detected_image(out_img) | |
| with open(out_json, "rb") as f: | |
| st.download_button("⬇️ Download new results.json", f, file_name="results.json") | |
| with open(out_img, "rb") as f: | |
| st.download_button("⬇️ Download new detected.jpg", f, file_name="detected.jpg") |