Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 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,7 +29,7 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
| 27 |
print(f"✅ Using device: {device}")
|
| 28 |
|
| 29 |
# ==========================
|
| 30 |
-
# Load Model
|
| 31 |
# ==========================
|
| 32 |
selected_model = MODEL_PATH if os.path.isfile(MODEL_PATH) else FALLBACK_MODEL
|
| 33 |
model = YOLO(selected_model)
|
|
@@ -35,9 +37,9 @@ model = YOLO(selected_model)
|
|
| 35 |
# ==========================
|
| 36 |
# Video Processing with Optimizations
|
| 37 |
# ==========================
|
| 38 |
-
def process_video(
|
| 39 |
try:
|
| 40 |
-
video = cv2.VideoCapture(
|
| 41 |
if not video.isOpened():
|
| 42 |
raise ValueError("Could not open video file.")
|
| 43 |
|
|
@@ -119,23 +121,40 @@ def generate_pdf_report(violations, score):
|
|
| 119 |
return pdf_url
|
| 120 |
|
| 121 |
# ==========================
|
| 122 |
-
#
|
| 123 |
# ==========================
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
-
|
| 141 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import cv2
|
|
|
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
import time
|
| 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 |
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 |
# ==========================
|
| 38 |
# Video Processing with Optimizations
|
| 39 |
# ==========================
|
| 40 |
+
def process_video(video_file, frame_skip=5, max_frames=100):
|
| 41 |
try:
|
| 42 |
+
video = cv2.VideoCapture(video_file)
|
| 43 |
if not video.isOpened():
|
| 44 |
raise ValueError("Could not open video file.")
|
| 45 |
|
|
|
|
| 121 |
return pdf_url
|
| 122 |
|
| 123 |
# ==========================
|
| 124 |
+
# Endpoint for Hugging Face Model Inference
|
| 125 |
# ==========================
|
| 126 |
+
@app.route('/process_video', methods=['POST'])
|
| 127 |
+
def process_video_endpoint():
|
| 128 |
+
try:
|
| 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 |
+
# Save the uploaded video temporarily
|
| 138 |
+
video_path = os.path.join("temp_video", video_file.filename)
|
| 139 |
+
video_file.save(video_path)
|
| 140 |
|
| 141 |
+
# Process the video using the model
|
| 142 |
+
violations, score, pdf_url = process_video(video_path)
|
| 143 |
+
|
| 144 |
+
if not violations:
|
| 145 |
+
return jsonify({'error': 'Error processing video'}), 500
|
| 146 |
+
|
| 147 |
+
# Return the violations, safety score, and PDF report URL
|
| 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 |
+
# Run the Flask app
|
| 160 |
+
app.run(host="0.0.0.0", port=5000)
|