Spaces:
Running
Running
TheKnight115
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,14 @@
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
-
from processor import process_frame
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
import tempfile
|
6 |
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer, RTCConfiguration
|
7 |
import cv2
|
8 |
|
9 |
-
# Set page
|
10 |
st.set_page_config(page_title="Traffic Violation Detection", layout="wide")
|
11 |
|
12 |
st.title("π¦ Traffic Violation Detection App")
|
@@ -15,7 +17,7 @@ st.title("π¦ Traffic Violation Detection App")
|
|
15 |
option = st.sidebar.radio("Select Option:", ("Image", "Video", "Live Camera"))
|
16 |
|
17 |
if option == "Image":
|
18 |
-
st.header("πΌοΈ Upload Image")
|
19 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
20 |
if uploaded_file:
|
21 |
image = Image.open(uploaded_file)
|
@@ -25,45 +27,50 @@ if option == "Image":
|
|
25 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
|
26 |
image.save(tmp.name)
|
27 |
frame = cv2.imread(tmp.name)
|
28 |
-
processed =
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
elif option == "Video":
|
40 |
-
st.header("π₯ Select Video")
|
41 |
video_files = [f for f in os.listdir("videos") if f.endswith(('.mp4', '.avi', '.mov'))]
|
42 |
if not video_files:
|
43 |
st.warning("No videos found in the 'videos/' folder.")
|
44 |
else:
|
45 |
-
selected_video = st.selectbox("Choose a video:", video_files)
|
46 |
video_path = os.path.join("videos", selected_video)
|
47 |
st.video(video_path)
|
48 |
if st.button("Process Video"):
|
49 |
with st.spinner("Processing Video..."):
|
50 |
-
processed_path =
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
)
|
63 |
|
64 |
elif option == "Live Camera":
|
65 |
st.header("π· Live Camera Feed")
|
|
|
66 |
|
|
|
67 |
RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
|
68 |
|
69 |
class VideoTransformer(VideoTransformerBase):
|
@@ -81,6 +88,4 @@ elif option == "Live Camera":
|
|
81 |
video_transformer_factory=VideoTransformer,
|
82 |
media_stream_constraints={"video": True, "audio": False},
|
83 |
async_transform=True
|
84 |
-
)
|
85 |
-
|
86 |
-
st.info("Live processing is active. Detected violations will be annotated on the video feed.")
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
import streamlit as st
|
4 |
+
from processor import process_image, process_video, process_frame
|
5 |
import os
|
6 |
from PIL import Image
|
7 |
import tempfile
|
8 |
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer, RTCConfiguration
|
9 |
import cv2
|
10 |
|
11 |
+
# Set page configuration
|
12 |
st.set_page_config(page_title="Traffic Violation Detection", layout="wide")
|
13 |
|
14 |
st.title("π¦ Traffic Violation Detection App")
|
|
|
17 |
option = st.sidebar.radio("Select Option:", ("Image", "Video", "Live Camera"))
|
18 |
|
19 |
if option == "Image":
|
20 |
+
st.header("πΌοΈ Upload and Process Image")
|
21 |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
22 |
if uploaded_file:
|
23 |
image = Image.open(uploaded_file)
|
|
|
27 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
|
28 |
image.save(tmp.name)
|
29 |
frame = cv2.imread(tmp.name)
|
30 |
+
processed = process_image(tmp.name, "fonts/alfont_com_arial-1.ttf")
|
31 |
+
if processed is not None:
|
32 |
+
st.image(processed, caption='Processed Image.', use_column_width=True)
|
33 |
+
# Save processed image to temporary file for download
|
34 |
+
_, img_encoded = cv2.imencode('.jpg', processed)
|
35 |
+
st.download_button(
|
36 |
+
label="π₯ Download Image",
|
37 |
+
data=img_encoded.tobytes(),
|
38 |
+
file_name="processed_image.jpg",
|
39 |
+
mime="image/jpeg"
|
40 |
+
)
|
41 |
+
else:
|
42 |
+
st.error("Failed to process the image.")
|
43 |
|
44 |
elif option == "Video":
|
45 |
+
st.header("π₯ Select and Process Video")
|
46 |
video_files = [f for f in os.listdir("videos") if f.endswith(('.mp4', '.avi', '.mov'))]
|
47 |
if not video_files:
|
48 |
st.warning("No videos found in the 'videos/' folder.")
|
49 |
else:
|
50 |
+
selected_video = st.selectbox("Choose a video to process:", video_files)
|
51 |
video_path = os.path.join("videos", selected_video)
|
52 |
st.video(video_path)
|
53 |
if st.button("Process Video"):
|
54 |
with st.spinner("Processing Video..."):
|
55 |
+
processed_path = process_video(video_path, "fonts/alfont_com_arial-1.ttf")
|
56 |
+
if processed_path and os.path.exists(processed_path):
|
57 |
+
st.success("Video processed successfully!")
|
58 |
+
st.video(processed_path)
|
59 |
+
with open(processed_path, "rb") as file:
|
60 |
+
st.download_button(
|
61 |
+
label="π₯ Download Processed Video",
|
62 |
+
data=file,
|
63 |
+
file_name="processed_video.mp4",
|
64 |
+
mime="video/mp4"
|
65 |
+
)
|
66 |
+
else:
|
67 |
+
st.error("Failed to process the video.")
|
68 |
|
69 |
elif option == "Live Camera":
|
70 |
st.header("π· Live Camera Feed")
|
71 |
+
st.info("Live processing is active. Detected violations will be annotated on the video feed.")
|
72 |
|
73 |
+
# RTC Configuration for streamlit-webrtc
|
74 |
RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
|
75 |
|
76 |
class VideoTransformer(VideoTransformerBase):
|
|
|
88 |
video_transformer_factory=VideoTransformer,
|
89 |
media_stream_constraints={"video": True, "audio": False},
|
90 |
async_transform=True
|
91 |
+
)
|
|
|
|