TheKnight115 commited on
Commit
f0afed0
Β·
verified Β·
1 Parent(s): ded2692

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -69
app.py CHANGED
@@ -11,81 +11,44 @@ import cv2
11
  # Set page configuration
12
  st.set_page_config(page_title="Traffic Violation Detection", layout="wide")
13
 
14
- st.title("🚦 Traffic Violation Detection App")
 
15
 
16
- # Sidebar options
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)
24
- st.image(image, caption='Uploaded Image.', use_column_width=True)
25
- if st.button("Process Image"):
26
- with st.spinner("Processing..."):
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, "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, "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):
77
- def __init__(self):
78
- self.font_path = "alfont_com_arial-1.ttf"
79
-
80
- def transform(self, frame):
81
- img = frame.to_ndarray(format="bgr24")
82
- processed_img = process_frame(img, self.font_path)
83
- return processed_img
84
-
85
- webrtc_ctx = webrtc_streamer(
86
- key="live-camera",
87
- rtc_configuration=RTC_CONFIGURATION,
88
- video_transformer_factory=VideoTransformer,
89
- media_stream_constraints={"video": True, "audio": False},
90
- async_transform=True
91
- )
 
11
  # Set page configuration
12
  st.set_page_config(page_title="Traffic Violation Detection", layout="wide")
13
 
14
+ # Streamlit app layout
15
+ st.title("Motorbike Violation Detection")
16
 
17
+ option = st.selectbox(
18
+ "Choose Input Type",
19
+ ("Image", "Video", "Live Camera")
20
+ )
21
 
22
  if option == "Image":
 
23
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
24
+ if uploaded_file is not None:
25
+ image = cv2.imread(uploaded_file)
26
+ process_image(image)
27
+ st.download_button("Download Result", data=image, file_name="result_image.jpg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  elif option == "Video":
30
+ # Dynamically load video files from 'videos/' folder
31
+ video_folder = 'videos/'
32
+ video_files = [f for f in os.listdir(video_folder) if f.endswith(('.mp4', '.avi'))]
33
+
34
+ if video_files:
35
+ video_file = st.selectbox("Select a video", video_files)
 
 
36
  if st.button("Process Video"):
37
+ process_video(os.path.join(video_folder, video_file))
38
+ st.download_button("Download Result", data=video_file, file_name="result_video.mp4")
39
+ else:
40
+ st.warning("No videos found in the 'videos/' folder.")
 
 
 
 
 
 
 
 
 
 
41
 
42
  elif option == "Live Camera":
43
+ st.write("Starting live camera...")
44
+ run = st.checkbox('Run Camera')
45
+ camera = cv2.VideoCapture(0)
46
+ FRAME_WINDOW = st.image([])
47
+
48
+ while run:
49
+ ret, frame = camera.read()
50
+ if ret:
51
+ process_frame(frame)
52
+ FRAME_WINDOW.image(frame)
53
+
54
+ camera.release()