PrashanthB461 commited on
Commit
9897313
·
verified ·
1 Parent(s): b2ff9da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -40
app.py CHANGED
@@ -1,12 +1,10 @@
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,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(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,40 +119,23 @@ def generate_pdf_report(violations, score):
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)
 
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()