TheKnight115 commited on
Commit
3a97db5
Β·
verified Β·
1 Parent(s): 1289698

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -84
app.py CHANGED
@@ -1,121 +1,86 @@
1
  import streamlit as st
2
- from processor import process_frame, process_image, process_video
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
- # Configure Streamlit page
12
- st.set_page_config(page_title="🚦 Traffic Violation Detection", layout="wide")
13
 
14
  st.title("🚦 Traffic Violation Detection App")
15
 
16
- # Sidebar for selection
17
- st.sidebar.title("Choose an Option")
18
- option = st.sidebar.radio("Select the processing type:", ("Image", "Video", "Live Camera"))
19
 
20
  if option == "Image":
21
- st.header("πŸ–ΌοΈ Image Processing")
22
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
23
-
24
- if uploaded_file is not None:
25
- # Save the uploaded image to a temporary file
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
- font_path = "fonts/alfont_com_arial-1.ttf" # Update the path as needed
37
- processed_image = process_image(temp_image_path, font_path)
38
- if processed_image is not None:
39
- # Convert the processed image to RGB
40
- processed_image_rgb = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
41
- st.image(processed_image_rgb, caption='Processed Image.', use_column_width=True)
42
-
43
- # Save processed image to a temporary file
44
- result_image = Image.fromarray(processed_image_rgb)
45
- with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
46
- result_image.save(tmp.name)
47
- tmp_path = tmp.name
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 Processing")
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 predefined videos found in the 'videos/' directory.")
66
  else:
67
- selected_video = st.selectbox("Select a video to process:", video_files)
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
- font_path = "fonts/alfont_com_arial-1.ttf" # Update the path as needed
75
- processed_video_path = process_video(video_path, font_path)
76
- if processed_video_path and os.path.exists(processed_video_path):
77
- st.success("Video processed successfully!")
78
- st.video(processed_video_path)
79
-
80
- # Provide download button
81
- with open(processed_video_path, "rb") as file:
82
- btn = st.download_button(
83
- label="πŸ“₯ Download Processed Video",
84
- data=file,
85
- file_name="processed_video.mp4",
86
- mime="video/mp4"
87
- )
88
- else:
89
- st.error("Failed to process the video.")
90
 
91
  elif option == "Live Camera":
92
- st.header("πŸ“· Live Camera Processing")
93
- st.warning("Live camera processing is in progress. Please allow camera access.")
94
 
95
- # Define settings for WebRTC
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" # Update the path as needed
104
 
105
  def transform(self, frame):
106
  img = frame.to_ndarray(format="bgr24")
107
  processed_img = process_frame(img, self.font_path)
108
- if processed_img is not None:
109
- return processed_img
110
- return img
111
 
112
  webrtc_ctx = webrtc_streamer(
113
  key="live-camera",
114
- mode=WebRtcMode.SENDRECV,
115
- client_settings=WEBRTC_CLIENT_SETTINGS,
116
  video_transformer_factory=VideoTransformer,
117
- async_transform=True,
 
118
  )
119
 
120
- st.info("Live camera feed is being processed. Detected violations will be annotated on the video stream.")
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.")