Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from flask import Flask, render_template, request, send_from_directory
|
5 |
+
from werkzeug.utils import secure_filename
|
6 |
+
|
7 |
+
# Define the folder paths for saving input and output videos
|
8 |
+
UPLOAD_FOLDER = 'input_videos/'
|
9 |
+
OUTPUT_FOLDER = 'output_videos/'
|
10 |
+
|
11 |
+
# Allowed video extensions
|
12 |
+
ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov'}
|
13 |
+
|
14 |
+
# Initialize the Flask app
|
15 |
+
app = Flask(__name__)
|
16 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
17 |
+
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
|
18 |
+
|
19 |
+
|
20 |
+
def allowed_file(filename):
|
21 |
+
"""Check if the uploaded file is allowed based on its extension."""
|
22 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
23 |
+
|
24 |
+
|
25 |
+
def extract_and_compare(video1_path, video2_path, output_path):
|
26 |
+
"""Compare the two videos and save the output video with differences highlighted."""
|
27 |
+
cap1 = cv2.VideoCapture(video1_path)
|
28 |
+
cap2 = cv2.VideoCapture(video2_path)
|
29 |
+
|
30 |
+
fps = cap1.get(cv2.CAP_PROP_FPS)
|
31 |
+
width = int(cap1.get(cv2.CAP_PROP_FRAME_WIDTH))
|
32 |
+
height = int(cap1.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
33 |
+
|
34 |
+
fourcc = cv2.VideoWriter_fourcc(*'XVID')
|
35 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
36 |
+
|
37 |
+
while cap1.isOpened() and cap2.isOpened():
|
38 |
+
ret1, frame1 = cap1.read()
|
39 |
+
ret2, frame2 = cap2.read()
|
40 |
+
|
41 |
+
if not ret1 or not ret2:
|
42 |
+
break
|
43 |
+
|
44 |
+
frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
|
45 |
+
frame2_gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
|
46 |
+
|
47 |
+
diff = cv2.absdiff(frame1_gray, frame2_gray)
|
48 |
+
_, diff_thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)
|
49 |
+
|
50 |
+
contours, _ = cv2.findContours(diff_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
51 |
+
|
52 |
+
for contour in contours:
|
53 |
+
if cv2.contourArea(contour) > 500:
|
54 |
+
(x, y, w, h) = cv2.boundingRect(contour)
|
55 |
+
cv2.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
56 |
+
|
57 |
+
out.write(frame1)
|
58 |
+
|
59 |
+
cap1.release()
|
60 |
+
cap2.release()
|
61 |
+
out.release()
|
62 |
+
|
63 |
+
|
64 |
+
@app.route('/', methods=['GET', 'POST'])
|
65 |
+
def index():
|
66 |
+
if request.method == 'POST':
|
67 |
+
# Check if files are part of the request
|
68 |
+
if 'video1' not in request.files or 'video2' not in request.files:
|
69 |
+
return 'No files part'
|
70 |
+
|
71 |
+
video1 = request.files['video1']
|
72 |
+
video2 = request.files['video2']
|
73 |
+
|
74 |
+
# Check if the files are allowed
|
75 |
+
if video1 and allowed_file(video1.filename) and video2 and allowed_file(video2.filename):
|
76 |
+
# Secure filenames and save to input_videos folder
|
77 |
+
filename1 = secure_filename(video1.filename)
|
78 |
+
filename2 = secure_filename(video2.filename)
|
79 |
+
video1_path = os.path.join(app.config['UPLOAD_FOLDER'], filename1)
|
80 |
+
video2_path = os.path.join(app.config['UPLOAD_FOLDER'], filename2)
|
81 |
+
video1.save(video1_path)
|
82 |
+
video2.save(video2_path)
|
83 |
+
|
84 |
+
# Output path
|
85 |
+
output_path = os.path.join(app.config['OUTPUT_FOLDER'], 'output_with_highlights.avi')
|
86 |
+
|
87 |
+
# Compare the videos and generate the output
|
88 |
+
extract_and_compare(video1_path, video2_path, output_path)
|
89 |
+
|
90 |
+
# Return the output video
|
91 |
+
return send_from_directory(app.config['OUTPUT_FOLDER'], 'output_with_highlights.avi', as_attachment=True)
|
92 |
+
|
93 |
+
return render_template('index.html')
|
94 |
+
|
95 |
+
|
96 |
+
if __name__ == '__main__':
|
97 |
+
app.run(debug=True)
|