TheKnight115 commited on
Commit
3d6c4b9
Β·
verified Β·
1 Parent(s): 4815258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -31
app.py CHANGED
@@ -11,43 +11,84 @@ import cv2
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','.mov'))]
33
- if video_files:
34
- video_file = st.selectbox("Select a video", video_files)
35
- if st.button("Process Video"):
36
- process_video(os.path.join(video_folder, video_file))
37
- st.download_button("Download Result", data=video_file, file_name="result_video.mp4")
38
- else:
39
  st.warning("No videos found in the 'videos/' folder.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  elif option == "Live Camera":
42
- st.write("Starting live camera...")
43
- run = st.checkbox('Run Camera')
44
- camera = cv2.VideoCapture(0)
45
- FRAME_WINDOW = st.image([])
46
-
47
- while run:
48
- ret, frame = camera.read()
49
- if ret:
50
- process_frame(frame)
51
- FRAME_WINDOW.image(frame)
52
-
53
- camera.release()
 
 
 
 
 
 
 
 
 
 
 
11
  # Set page configuration
12
  st.set_page_config(page_title="Traffic Violation Detection", layout="wide")
13
 
14
+ st.title("🚦 Motorbike Violation Detection App")
15
+
16
+
17
+ # Sidebar options
18
+ option = st.sidebar.radio("Select Option:", ("Image", "Video", "Live Camera"))
19
+
20
 
 
 
 
 
21
 
22
  if option == "Image":
23
+ st.header("πŸ–ΌοΈ Upload and Process Image")
24
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
25
+ if uploaded_file:
26
+ image = Image.open(uploaded_file)
27
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
28
+ if st.button("Process Image"):
29
+ with st.spinner("Processing..."):
30
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
31
+ image.save(tmp.name)
32
+ frame = cv2.imread(tmp.name)
33
+ processed = process_image(tmp.name, "alfont_com_arial-1.ttf")
34
+ if processed is not None:
35
+ st.image(processed, caption='Processed Image.', use_column_width=True)
36
+ # Save processed image to temporary file for download
37
+ _, img_encoded = cv2.imencode('.jpg', processed)
38
+ st.download_button(
39
+ label="πŸ“₯ Download Image",
40
+ data=img_encoded.tobytes(),
41
+ file_name="processed_image.jpg",
42
+ mime="image/jpeg"
43
+ )
44
+ else:
45
+ st.error("Failed to process the image.")
46
 
47
  elif option == "Video":
48
+ st.header("πŸŽ₯ Select and Process Video")
49
+ video_files = [f for f in os.listdir("videos/") if f.endswith(('.mp4', '.avi', '.mov'))]
50
+ if not video_files:
 
 
 
 
 
 
51
  st.warning("No videos found in the 'videos/' folder.")
52
+ else:
53
+ selected_video = st.selectbox("Choose a video to process:", video_files)
54
+ video_path = os.path.join("videos/", selected_video)
55
+ st.video(video_path)
56
+ if st.button("Process Video"):
57
+ with st.spinner("Processing Video..."):
58
+ processed_path = process_video(video_path, "alfont_com_arial-1.ttf")
59
+ if processed_path and os.path.exists(processed_path):
60
+ st.success("Video processed successfully!")
61
+ st.video(processed_path)
62
+ with open(processed_path, "rb") as file:
63
+ st.download_button(
64
+ label="πŸ“₯ Download Processed Video",
65
+ data=file,
66
+ file_name="processed_video.mp4",
67
+ mime="video/mp4"
68
+ )
69
+ else:
70
+ st.error("Failed to process the video.")
71
 
72
  elif option == "Live Camera":
73
+ st.header("πŸ“· Live Camera Feed")
74
+ st.info("Live processing is active. Detected violations will be annotated on the video feed.")
75
+
76
+ # RTC Configuration for streamlit-webrtc
77
+ RTC_CONFIGURATION = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
78
+
79
+ class VideoTransformer(VideoTransformerBase):
80
+ def __init__(self):
81
+ self.font_path = "alfont_com_arial-1.ttf"
82
+
83
+ def transform(self, frame):
84
+ img = frame.to_ndarray(format="bgr24")
85
+ processed_img = process_frame(img, self.font_path)
86
+ return processed_img
87
+
88
+ webrtc_ctx = webrtc_streamer(
89
+ key="live-camera",
90
+ rtc_configuration=RTC_CONFIGURATION,
91
+ video_transformer_factory=VideoTransformer,
92
+ media_stream_constraints={"video": True, "audio": False},
93
+ async_transform=True
94
+ )