Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1,84 +1,73 @@ | |
| 1 | 
             
            import gradio as gr
         | 
| 2 | 
             
            import cv2
         | 
| 3 | 
            -
            import  | 
| 4 | 
             
            import random
         | 
|  | |
| 5 | 
             
            from services.video_service import get_random_video_frame
         | 
| 6 | 
             
            from services.overlay_service import overlay_boxes
         | 
| 7 | 
            -
            from services.detection_service import detect_objects
         | 
| 8 | 
            -
            from services.thermal_service import detect_thermal_anomalies
         | 
| 9 |  | 
| 10 | 
             
            # Constants
         | 
| 11 | 
            -
            TEMP_IMAGE_PATH = " | 
|  | |
| 12 |  | 
| 13 | 
            -
            #  | 
| 14 | 
            -
             | 
| 15 | 
            -
            frame_rate = 1  # default frame rate in seconds
         | 
| 16 |  | 
| 17 | 
            -
            #  | 
| 18 | 
            -
             | 
| 19 | 
            -
                 | 
| 20 | 
            -
                 | 
| 21 | 
            -
             | 
|  | |
| 22 |  | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
                detections = detect_objects(TEMP_IMAGE_PATH)
         | 
| 26 | 
            -
                thermal_detections = detect_thermal_anomalies(TEMP_IMAGE_PATH)
         | 
| 27 | 
            -
             | 
| 28 | 
            -
                # Merge all detections
         | 
| 29 | 
            -
                combined_detections = detections + thermal_detections
         | 
| 30 | 
            -
             | 
| 31 | 
            -
                # Overlay the detections
         | 
| 32 | 
            -
                annotated_frame = overlay_boxes(frame.copy(), combined_detections)
         | 
| 33 | 
            -
             | 
| 34 | 
            -
                # Update status text
         | 
| 35 | 
            -
                status_text = f"Active Anomaly: {anomaly_type} | Objects Detected: {len(detections)} | Thermal Alerts: {len(thermal_detections)}"
         | 
| 36 | 
            -
             | 
| 37 | 
            -
                return annotated_frame, status_text
         | 
| 38 | 
            -
             | 
| 39 | 
            -
            def loop_monitor():
         | 
| 40 | 
            -
                global paused
         | 
| 41 | 
             
                while True:
         | 
| 42 | 
            -
                    if not paused:
         | 
| 43 | 
            -
                         | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 48 |  | 
| 49 | 
            -
            # Pause/Resume control
         | 
| 50 | 
             
            def toggle_pause():
         | 
| 51 | 
            -
                 | 
| 52 | 
            -
                paused  | 
| 53 | 
            -
                return "Resume" if paused else "Pause"
         | 
| 54 |  | 
| 55 | 
            -
            # Frame Rate control
         | 
| 56 | 
            -
            def update_frame_rate(new_rate):
         | 
| 57 | 
            -
                global frame_rate
         | 
| 58 | 
            -
                frame_rate = new_rate
         | 
| 59 | 
            -
                return f"Frame Rate set to {new_rate} sec"
         | 
| 60 | 
            -
             | 
| 61 | 
            -
            # Build Gradio Interface
         | 
| 62 | 
             
            def build_interface():
         | 
| 63 | 
             
                with gr.Blocks() as app:
         | 
|  | |
|  | |
| 64 | 
             
                    with gr.Row():
         | 
| 65 | 
            -
                        video_output = gr.Image(label=" | 
| 66 | 
            -
                        metrics_output = gr.Textbox(label="Live Metrics" | 
| 67 | 
            -
                    with gr.Row():
         | 
| 68 | 
            -
                        pause_button = gr.Button("Pause")
         | 
| 69 | 
            -
                        frame_rate_slider = gr.Slider(minimum=0.5, maximum=5, value=1, step=0.5, label="Frame Update Interval (seconds)")
         | 
| 70 |  | 
| 71 | 
            -
                     | 
| 72 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 73 |  | 
| 74 | 
            -
                    # Pause/Resume button logic
         | 
| 75 | 
             
                    pause_button.click(toggle_pause, outputs=pause_button)
         | 
| 76 |  | 
| 77 | 
            -
                    # Frame Rate Slider logic
         | 
| 78 | 
            -
                    frame_rate_slider.change(update_frame_rate, inputs=frame_rate_slider, outputs=None)
         | 
| 79 | 
            -
             | 
| 80 | 
             
                return app
         | 
| 81 |  | 
| 82 | 
            -
             | 
| 83 | 
            -
            demo = build_interface()
         | 
| 84 | 
            -
            demo.launch()
         | 
|  | |
| 1 | 
             
            import gradio as gr
         | 
| 2 | 
             
            import cv2
         | 
| 3 | 
            +
            import os
         | 
| 4 | 
             
            import random
         | 
| 5 | 
            +
            import time
         | 
| 6 | 
             
            from services.video_service import get_random_video_frame
         | 
| 7 | 
             
            from services.overlay_service import overlay_boxes
         | 
|  | |
|  | |
| 8 |  | 
| 9 | 
             
            # Constants
         | 
| 10 | 
            +
            TEMP_IMAGE_PATH = "temp_frame.jpg"
         | 
| 11 | 
            +
            VIDEOS_FOLDER = "videos"
         | 
| 12 |  | 
| 13 | 
            +
            # App states
         | 
| 14 | 
            +
            app_state = {"paused": False}
         | 
|  | |
| 15 |  | 
| 16 | 
            +
            # Live metrics
         | 
| 17 | 
            +
            live_metrics = {
         | 
| 18 | 
            +
                "Frames Processed": 0,
         | 
| 19 | 
            +
                "Current FPS": 1,
         | 
| 20 | 
            +
                "Detections": 0,
         | 
| 21 | 
            +
            }
         | 
| 22 |  | 
| 23 | 
            +
            def monitor_feed(frame_rate=1):
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 24 | 
             
                while True:
         | 
| 25 | 
            +
                    if not app_state["paused"]:
         | 
| 26 | 
            +
                        frame, label = get_random_video_frame()
         | 
| 27 | 
            +
                        if frame is not None:
         | 
| 28 | 
            +
                            frame_with_overlay = overlay_boxes(frame, label)
         | 
| 29 | 
            +
                            cv2.imwrite(TEMP_IMAGE_PATH, frame_with_overlay)
         | 
| 30 | 
            +
                            live_metrics["Frames Processed"] += 1
         | 
| 31 | 
            +
                            live_metrics["Current FPS"] = frame_rate
         | 
| 32 | 
            +
                        else:
         | 
| 33 | 
            +
                            continue  # Skip if frame is None
         | 
| 34 | 
            +
                    time.sleep(1.0 / frame_rate)
         | 
| 35 | 
            +
                    yield TEMP_IMAGE_PATH, format_metrics()
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            def format_metrics():
         | 
| 38 | 
            +
                return (f"Frames Processed: {live_metrics['Frames Processed']}\n"
         | 
| 39 | 
            +
                        f"Current FPS: {live_metrics['Current FPS']}\n"
         | 
| 40 | 
            +
                        f"Detections: {live_metrics['Detections']}")
         | 
| 41 |  | 
|  | |
| 42 | 
             
            def toggle_pause():
         | 
| 43 | 
            +
                app_state["paused"] = not app_state["paused"]
         | 
| 44 | 
            +
                return "鈻讹笍 Resume" if app_state["paused"] else "鈴革笍 Pause"
         | 
|  | |
| 45 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 46 | 
             
            def build_interface():
         | 
| 47 | 
             
                with gr.Blocks() as app:
         | 
| 48 | 
            +
                    gr.Markdown("# Solar Surveillance System \ud83c\udf0c")
         | 
| 49 | 
            +
             | 
| 50 | 
             
                    with gr.Row():
         | 
| 51 | 
            +
                        video_output = gr.Image(label="Live Feed", elem_id="video-stream")
         | 
| 52 | 
            +
                        metrics_output = gr.Textbox(label="Live Metrics")
         | 
|  | |
|  | |
|  | |
| 53 |  | 
| 54 | 
            +
                    with gr.Row():
         | 
| 55 | 
            +
                        with gr.Column():
         | 
| 56 | 
            +
                            pause_button = gr.Button("鈴革笍 Pause")
         | 
| 57 | 
            +
                            frame_slider = gr.Slider(1, 10, value=1, step=1, label="Frame Rate (FPS)")
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                    runner = gr.Interface(
         | 
| 60 | 
            +
                        fn=monitor_feed,
         | 
| 61 | 
            +
                        inputs=frame_slider,
         | 
| 62 | 
            +
                        outputs=[video_output, metrics_output],
         | 
| 63 | 
            +
                        live=True,
         | 
| 64 | 
            +
                        concurrency_limit=1
         | 
| 65 | 
            +
                    )
         | 
| 66 |  | 
|  | |
| 67 | 
             
                    pause_button.click(toggle_pause, outputs=pause_button)
         | 
| 68 |  | 
|  | |
|  | |
|  | |
| 69 | 
             
                return app
         | 
| 70 |  | 
| 71 | 
            +
            if __name__ == "__main__":
         | 
| 72 | 
            +
                demo = build_interface()
         | 
| 73 | 
            +
                demo.launch()
         | 
