-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathweb_app.py
More file actions
184 lines (147 loc) · 6.2 KB
/
Copy pathweb_app.py
File metadata and controls
184 lines (147 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
import os
import uuid
from pathlib import Path
from flask import Flask, render_template, request, send_file, jsonify, url_for
from werkzeug.utils import secure_filename
import threading
import time
from video_blur_core import VideoBlurrer
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 500 * 1024 * 1024 # 500MB max file size
app.config['UPLOAD_FOLDER'] = '/app/uploads'
app.config['OUTPUT_FOLDER'] = '/app/outputs'
# Secret key for session management
# WARNING: Set SECRET_KEY environment variable in production!
secret_key = os.environ.get('SECRET_KEY')
if not secret_key:
import sys
import secrets
# Generate a random key for development
secret_key = secrets.token_hex(32)
print("WARNING: No SECRET_KEY set. Using auto-generated key. Set SECRET_KEY environment variable in production!", file=sys.stderr)
app.config['SECRET_KEY'] = secret_key
ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov', 'mkv', 'flv', 'wmv'}
# Store job statuses in memory
# NOTE: This is lost on restart. For production, consider Redis or database
jobs = {}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def process_video_task(job_id, input_path, output_path, settings):
"""Background task to process video"""
try:
jobs[job_id]['status'] = 'processing'
jobs[job_id]['progress'] = 0
def progress_callback(progress, fps, message):
"""Progress callback receives 3 parameters from VideoBlurrer"""
jobs[job_id]['progress'] = int(progress)
jobs[job_id]['message'] = message
jobs[job_id]['fps'] = round(fps, 2) if fps > 0 else 0
blurrer = VideoBlurrer(
device=settings.get('device', 'auto'),
blur_strength=settings.get('blur_strength', 51),
blur_type=settings.get('blur_type', 'gaussian'),
confidence=settings.get('confidence', 0.15),
detect_faces=settings.get('detect_faces', True),
detect_license_plates=settings.get('detect_license_plates', True),
progress_callback=progress_callback,
pitch_shift=settings.get('pitch_shift', 0.0)
)
blurrer.process_video(input_path, output_path)
jobs[job_id]['status'] = 'completed'
jobs[job_id]['progress'] = 100
jobs[job_id]['output_file'] = os.path.basename(output_path)
except Exception as e:
jobs[job_id]['status'] = 'failed'
jobs[job_id]['error'] = str(e)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
if 'video' not in request.files:
return jsonify({'error': 'No video file provided'}), 400
file = request.files['video']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
if not allowed_file(file.filename):
return jsonify({'error': f'Invalid file type. Allowed: {", ".join(ALLOWED_EXTENSIONS)}'}), 400
# Create upload and output directories if they don't exist
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['OUTPUT_FOLDER'], exist_ok=True)
# Generate unique job ID
job_id = str(uuid.uuid4())
# Save uploaded file
filename = secure_filename(file.filename)
file_ext = filename.rsplit('.', 1)[1].lower()
input_filename = f"{job_id}_input.{file_ext}"
output_filename = f"{job_id}_output.{file_ext}"
input_path = os.path.join(app.config['UPLOAD_FOLDER'], input_filename)
output_path = os.path.join(app.config['OUTPUT_FOLDER'], output_filename)
file.save(input_path)
# Get settings from form
settings = {
'blur_strength': int(request.form.get('blur_strength', 51)),
'confidence': float(request.form.get('confidence', 0.15)),
'blur_type': request.form.get('blur_type', 'gaussian'),
'detect_faces': request.form.get('detect_faces', 'true').lower() == 'true',
'detect_license_plates': request.form.get('detect_license_plates', 'true').lower() == 'true',
'device': request.form.get('device', 'auto'),
'pitch_shift': float(request.form.get('pitch_shift', 0.0))
}
# Initialize job status
jobs[job_id] = {
'status': 'queued',
'progress': 0,
'input_file': filename,
'created_at': time.time()
}
# Start background processing
# Note: Using daemon threads for simplicity. For production, consider
# a task queue like Celery for better job management and graceful shutdown
thread = threading.Thread(
target=process_video_task,
args=(job_id, input_path, output_path, settings)
)
thread.daemon = True
thread.start()
return jsonify({
'job_id': job_id,
'message': 'Video upload successful. Processing started.'
})
@app.route('/status/<job_id>')
def get_status(job_id):
if job_id not in jobs:
return jsonify({'error': 'Job not found'}), 404
job = jobs[job_id]
response = {
'status': job['status'],
'progress': job['progress'],
'input_file': job['input_file']
}
if job['status'] == 'completed':
response['download_url'] = url_for('download_file', job_id=job_id)
elif job['status'] == 'failed':
response['error'] = job.get('error', 'Unknown error')
return jsonify(response)
@app.route('/download/<job_id>')
def download_file(job_id):
if job_id not in jobs:
return jsonify({'error': 'Job not found'}), 404
job = jobs[job_id]
if job['status'] != 'completed':
return jsonify({'error': 'Video processing not completed'}), 400
output_filename = job['output_file']
output_path = os.path.join(app.config['OUTPUT_FOLDER'], output_filename)
if not os.path.exists(output_path):
return jsonify({'error': 'Output file not found'}), 404
return send_file(
output_path,
as_attachment=True,
download_name=f"blurred_{job['input_file']}"
)
@app.route('/health')
def health():
return jsonify({'status': 'healthy'})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=False)