Spaces:
Sleeping
Sleeping
TheKnight115
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,121 +1,86 @@
|
|
1 |
import streamlit as st
|
2 |
-
from processor import process_frame
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
import tempfile
|
|
|
6 |
import cv2
|
7 |
-
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer, WebRtcMode, ClientSettings
|
8 |
-
import av
|
9 |
-
import asyncio
|
10 |
|
11 |
-
#
|
12 |
-
st.set_page_config(page_title="
|
13 |
|
14 |
st.title("π¦ Traffic Violation Detection App")
|
15 |
|
16 |
-
# Sidebar
|
17 |
-
st.sidebar.
|
18 |
-
option = st.sidebar.radio("Select the processing type:", ("Image", "Video", "Live Camera"))
|
19 |
|
20 |
if option == "Image":
|
21 |
-
st.header("πΌοΈ Image
|
22 |
-
uploaded_file = st.file_uploader("
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_image:
|
27 |
-
temp_image.write(uploaded_file.read())
|
28 |
-
temp_image_path = temp_image.name
|
29 |
-
|
30 |
-
# Display the uploaded image
|
31 |
-
st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)
|
32 |
-
|
33 |
-
# Process the image
|
34 |
if st.button("Process Image"):
|
35 |
with st.spinner("Processing..."):
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
# Download button
|
50 |
-
with open(tmp_path, "rb") as file:
|
51 |
-
btn = st.download_button(
|
52 |
-
label="π₯ Download Processed Image",
|
53 |
-
data=file,
|
54 |
-
file_name="processed_image.jpg",
|
55 |
-
mime="image/jpeg"
|
56 |
-
)
|
57 |
-
else:
|
58 |
-
st.error("Failed to process the image.")
|
59 |
|
60 |
elif option == "Video":
|
61 |
-
st.header("π₯ Video
|
62 |
video_files = [f for f in os.listdir("videos") if f.endswith(('.mp4', '.avi', '.mov'))]
|
63 |
-
|
64 |
if not video_files:
|
65 |
-
st.warning("No
|
66 |
else:
|
67 |
-
selected_video = st.selectbox("
|
68 |
video_path = os.path.join("videos", selected_video)
|
69 |
-
|
70 |
st.video(video_path)
|
71 |
-
|
72 |
if st.button("Process Video"):
|
73 |
-
with st.spinner("Processing..."):
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
)
|
88 |
-
else:
|
89 |
-
st.error("Failed to process the video.")
|
90 |
|
91 |
elif option == "Live Camera":
|
92 |
-
st.header("π· Live Camera
|
93 |
-
st.warning("Live camera processing is in progress. Please allow camera access.")
|
94 |
|
95 |
-
|
96 |
-
WEBRTC_CLIENT_SETTINGS = ClientSettings(
|
97 |
-
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
|
98 |
-
media_stream_constraints={"video": True, "audio": False},
|
99 |
-
)
|
100 |
|
101 |
class VideoTransformer(VideoTransformerBase):
|
102 |
def __init__(self):
|
103 |
-
self.font_path = "fonts/alfont_com_arial-1.ttf"
|
104 |
|
105 |
def transform(self, frame):
|
106 |
img = frame.to_ndarray(format="bgr24")
|
107 |
processed_img = process_frame(img, self.font_path)
|
108 |
-
|
109 |
-
return processed_img
|
110 |
-
return img
|
111 |
|
112 |
webrtc_ctx = webrtc_streamer(
|
113 |
key="live-camera",
|
114 |
-
|
115 |
-
client_settings=WEBRTC_CLIENT_SETTINGS,
|
116 |
video_transformer_factory=VideoTransformer,
|
117 |
-
|
|
|
118 |
)
|
119 |
|
120 |
-
st.info("Live
|
121 |
-
|
|
|
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 config
|
10 |
+
st.set_page_config(page_title="Traffic Violation Detection", layout="wide")
|
11 |
|
12 |
st.title("π¦ Traffic Violation Detection App")
|
13 |
|
14 |
+
# Sidebar options
|
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)
|
22 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
if st.button("Process Image"):
|
24 |
with st.spinner("Processing..."):
|
25 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
|
26 |
+
image.save(tmp.name)
|
27 |
+
frame = cv2.imread(tmp.name)
|
28 |
+
processed = process_frame(frame, "fonts/alfont_com_arial-1.ttf")
|
29 |
+
st.image(processed, caption='Processed Image.', use_column_width=True)
|
30 |
+
# Download button
|
31 |
+
_, img_encoded = cv2.imencode('.jpg', processed)
|
32 |
+
st.download_button(
|
33 |
+
label="Download Image",
|
34 |
+
data=img_encoded.tobytes(),
|
35 |
+
file_name="processed_image.jpg",
|
36 |
+
mime="image/jpeg"
|
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 = 'output_violation.mp4' # Adjust as needed
|
51 |
+
# Implement video processing logic here
|
52 |
+
# For brevity, assume process_video returns the path
|
53 |
+
# You can extend processor.py with a process_video function
|
54 |
+
st.success("Video processed!")
|
55 |
+
st.video(processed_path)
|
56 |
+
with open(processed_path, "rb") as file:
|
57 |
+
st.download_button(
|
58 |
+
label="Download Video",
|
59 |
+
data=file,
|
60 |
+
file_name="processed_video.mp4",
|
61 |
+
mime="video/mp4"
|
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):
|
70 |
def __init__(self):
|
71 |
+
self.font_path = "fonts/alfont_com_arial-1.ttf"
|
72 |
|
73 |
def transform(self, frame):
|
74 |
img = frame.to_ndarray(format="bgr24")
|
75 |
processed_img = process_frame(img, self.font_path)
|
76 |
+
return processed_img
|
|
|
|
|
77 |
|
78 |
webrtc_ctx = webrtc_streamer(
|
79 |
key="live-camera",
|
80 |
+
rtc_configuration=RTC_CONFIGURATION,
|
|
|
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.")
|
|