Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import cv2
|
|
|
|
| 3 |
import torch
|
| 4 |
-
import
|
| 5 |
-
from flask import Flask, request, jsonify
|
| 6 |
from ultralytics import YOLO
|
| 7 |
-
|
| 8 |
-
# Flask app initialization
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
|
| 11 |
# ==========================
|
| 12 |
# Configuration
|
|
@@ -29,7 +27,7 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
| 29 |
print(f"✅ Using device: {device}")
|
| 30 |
|
| 31 |
# ==========================
|
| 32 |
-
# Load Model
|
| 33 |
# ==========================
|
| 34 |
selected_model = MODEL_PATH if os.path.isfile(MODEL_PATH) else FALLBACK_MODEL
|
| 35 |
model = YOLO(selected_model)
|
|
@@ -37,9 +35,9 @@ model = YOLO(selected_model)
|
|
| 37 |
# ==========================
|
| 38 |
# Video Processing with Optimizations
|
| 39 |
# ==========================
|
| 40 |
-
def process_video(
|
| 41 |
try:
|
| 42 |
-
video = cv2.VideoCapture(
|
| 43 |
if not video.isOpened():
|
| 44 |
raise ValueError("Could not open video file.")
|
| 45 |
|
|
@@ -121,40 +119,23 @@ def generate_pdf_report(violations, score):
|
|
| 121 |
return pdf_url
|
| 122 |
|
| 123 |
# ==========================
|
| 124 |
-
#
|
| 125 |
# ==========================
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
# Get the video file from the request
|
| 130 |
-
if 'video' not in request.files:
|
| 131 |
-
return jsonify({'error': 'No video file provided'}), 400
|
| 132 |
-
|
| 133 |
-
video_file = request.files['video']
|
| 134 |
-
if not video_file:
|
| 135 |
-
return jsonify({'error': 'No video file provided'}), 400
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
video_file.save(video_path)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
return jsonify({
|
| 149 |
-
'violations': violations,
|
| 150 |
-
'score': score,
|
| 151 |
-
'pdf_report_url': pdf_url
|
| 152 |
-
})
|
| 153 |
-
|
| 154 |
-
except Exception as e:
|
| 155 |
-
print(f"❌ Error: {e}")
|
| 156 |
-
return jsonify({'error': str(e)}), 500
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
| 159 |
-
|
| 160 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import cv2
|
| 3 |
+
import gradio as gr
|
| 4 |
import torch
|
| 5 |
+
import numpy as np
|
|
|
|
| 6 |
from ultralytics import YOLO
|
| 7 |
+
import time
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ==========================
|
| 10 |
# Configuration
|
|
|
|
| 27 |
print(f"✅ Using device: {device}")
|
| 28 |
|
| 29 |
# ==========================
|
| 30 |
+
# Load Model (Use YOLOv8n for Faster Inference)
|
| 31 |
# ==========================
|
| 32 |
selected_model = MODEL_PATH if os.path.isfile(MODEL_PATH) else FALLBACK_MODEL
|
| 33 |
model = YOLO(selected_model)
|
|
|
|
| 35 |
# ==========================
|
| 36 |
# Video Processing with Optimizations
|
| 37 |
# ==========================
|
| 38 |
+
def process_video(video_path, frame_skip=5, max_frames=100):
|
| 39 |
try:
|
| 40 |
+
video = cv2.VideoCapture(video_path)
|
| 41 |
if not video.isOpened():
|
| 42 |
raise ValueError("Could not open video file.")
|
| 43 |
|
|
|
|
| 119 |
return pdf_url
|
| 120 |
|
| 121 |
# ==========================
|
| 122 |
+
# Gradio Interface
|
| 123 |
# ==========================
|
| 124 |
+
def gradio_interface(video_file):
|
| 125 |
+
if not video_file:
|
| 126 |
+
return "Please upload a video file.", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
violations, score, pdf_url = process_video(video_file)
|
| 129 |
+
return violations, f"Safety Score: {score}%", pdf_url
|
|
|
|
| 130 |
|
| 131 |
+
interface = gr.Interface(
|
| 132 |
+
fn=gradio_interface,
|
| 133 |
+
inputs=gr.Video(label="Upload Site Video"),
|
| 134 |
+
outputs=[gr.JSON(label="Detected Safety Violations"), gr.Textbox(label="Compliance Score"), gr.Textbox(label="PDF Report URL")],
|
| 135 |
+
title="Worksite Safety Violation Analyzer",
|
| 136 |
+
description="Upload short site videos to detect safety violations (e.g., no helmet, no harness, unsafe posture)."
|
| 137 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
+
print("🚀 Launching Safety Analyzer App...")
|
| 141 |
+
interface.launch()
|