Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +95 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,97 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import cvlib as cv
|
| 8 |
+
from cvlib.object_detection import draw_bbox
|
| 9 |
+
|
| 10 |
+
st.set_page_config(page_title="Vehicle Counter", layout="wide")
|
| 11 |
+
st.title("🚗 Vehicle Counter (JSON Results)")
|
| 12 |
+
|
| 13 |
+
# ---------- Helpers ----------
|
| 14 |
+
def load_json(path: str):
|
| 15 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 16 |
+
return json.load(f)
|
| 17 |
+
|
| 18 |
+
def show_detected_image(path: str):
|
| 19 |
+
img = cv2.imread(path)
|
| 20 |
+
if img is None:
|
| 21 |
+
st.warning(f"Could not read image: {path}")
|
| 22 |
+
return
|
| 23 |
+
st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), caption="Detected Image", use_container_width=True)
|
| 24 |
+
|
| 25 |
+
def detect_and_save(uploaded_file, out_json="src/results.json", out_img="src/detected.jpg", conf_th=0.25):
|
| 26 |
+
pil_img = Image.open(uploaded_file).convert("RGB")
|
| 27 |
+
rgb = np.array(pil_img)
|
| 28 |
+
bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)
|
| 29 |
+
|
| 30 |
+
bbox, labels, conf = cv.detect_common_objects(bgr, confidence=conf_th, model="yolov3-tiny")
|
| 31 |
+
output = draw_bbox(bgr, bbox, labels, conf)
|
| 32 |
+
|
| 33 |
+
results = {
|
| 34 |
+
"cars": labels.count("car"),
|
| 35 |
+
"trucks": labels.count("truck")
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
cv2.imwrite(out_img, output)
|
| 39 |
+
with open(out_json, "w", encoding="utf-8") as f:
|
| 40 |
+
json.dump(results, f, indent=2)
|
| 41 |
+
|
| 42 |
+
return results, out_img, out_json
|
| 43 |
+
|
| 44 |
+
# ---------- Layout ----------
|
| 45 |
+
st.subheader("1) Load existing results.json (from your project folder)")
|
| 46 |
+
|
| 47 |
+
# Erwartet, dass results.json im Repo-Root liegt
|
| 48 |
+
json_path = "src/results.json"
|
| 49 |
+
img_path = "src/detected.jpg"
|
| 50 |
+
|
| 51 |
+
col1, col2 = st.columns(2)
|
| 52 |
+
|
| 53 |
+
with col1:
|
| 54 |
+
if os.path.exists(json_path):
|
| 55 |
+
data = load_json(json_path)
|
| 56 |
+
st.success("✅ Loaded results.json")
|
| 57 |
+
st.metric("Cars", int(data.get("cars", 0)))
|
| 58 |
+
st.metric("Trucks", int(data.get("trucks", 0)))
|
| 59 |
+
|
| 60 |
+
st.code(json.dumps(data, indent=2), language="json")
|
| 61 |
+
|
| 62 |
+
with open(json_path, "rb") as f:
|
| 63 |
+
st.download_button("⬇️ Download results.json", f, file_name="results.json")
|
| 64 |
+
else:
|
| 65 |
+
st.warning("results.json not found in the app folder.")
|
| 66 |
+
st.write("➡️ Upload a JSON below or use the detection section to create it.")
|
| 67 |
+
|
| 68 |
+
with col2:
|
| 69 |
+
if os.path.exists(img_path):
|
| 70 |
+
show_detected_image(img_path)
|
| 71 |
+
with open(img_path, "rb") as f:
|
| 72 |
+
st.download_button("⬇️ Download detected.jpg", f, file_name="detected.jpg")
|
| 73 |
+
else:
|
| 74 |
+
st.info("detected.jpg not found (optional).")
|
| 75 |
+
|
| 76 |
+
st.divider()
|
| 77 |
+
|
| 78 |
+
st.subheader("2) Upload a results.json (optional)")
|
| 79 |
+
uploaded_json = st.file_uploader("Upload results.json", type=["json"], key="json_uploader")
|
| 80 |
+
|
| 81 |
+
if uploaded_json is not None:
|
| 82 |
+
try:
|
| 83 |
+
uploaded_data = json.load(uploaded_json)
|
| 84 |
+
st.success("✅ JSON uploaded and parsed")
|
| 85 |
+
st.metric("Cars", int(uploaded_data.get("cars", 0)))
|
| 86 |
+
st.metric("Trucks", int(uploaded_data.get("trucks", 0)))
|
| 87 |
+
st.code(json.dumps(uploaded_data, indent=2), language="json")
|
| 88 |
+
except Exception as e:
|
| 89 |
+
st.error(f"Could not read JSON: {e}")
|
| 90 |
+
|
| 91 |
+
st.divider()
|
| 92 |
+
|
| 93 |
+
st.subheader("3) (Optional) Upload an image and regenerate results.json + detected.jpg")
|
| 94 |
+
st.caption("If you upload an image, the app will run detection and overwrite results.json and detected.jpg in the app runtime.")
|
| 95 |
|
| 96 |
+
uploaded_img = st.file_uploader("Upload image (jpg/png/webp)", type=["jpg", "jpeg", "png", "webp"], key="img_uploader")
|
| 97 |
+
conf_th = st.slider("Confidence thresh_
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|