Spaces:
Sleeping
Sleeping
victorisgeek
commited on
Commit
•
a545eac
1
Parent(s):
0a4c146
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, redirect, url_for, send_from_directory, render_template
|
2 |
+
from werkzeug.utils import secure_filename
|
3 |
+
import os
|
4 |
+
import cv2
|
5 |
+
import dlib
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
UPLOAD_FOLDER = 'uploads'
|
9 |
+
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'mp4', 'avi'}
|
10 |
+
|
11 |
+
app = Flask(__name__)
|
12 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
13 |
+
|
14 |
+
detector = dlib.get_frontal_face_detector()
|
15 |
+
|
16 |
+
def allowed_file(filename):
|
17 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
18 |
+
|
19 |
+
def detect_faces(image_path):
|
20 |
+
img = cv2.imread(image_path)
|
21 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
22 |
+
faces = detector(gray)
|
23 |
+
return faces, img
|
24 |
+
|
25 |
+
def extract_face(img, face):
|
26 |
+
x, y, w, h = face.left(), face.top(), face.width(), face.height()
|
27 |
+
return img[y:y+h, x:x+w]
|
28 |
+
|
29 |
+
def swap_faces(img, face1, face2):
|
30 |
+
face1_img = extract_face(img, face1)
|
31 |
+
face2_img = extract_face(img, face2)
|
32 |
+
|
33 |
+
face1_resized = cv2.resize(face1_img, (face2.width(), face2.height()))
|
34 |
+
face2_resized = cv2.resize(face2_img, (face1.width(), face1.height()))
|
35 |
+
|
36 |
+
img[face1.top():face1.top()+face1.height(), face1.left():face1.left()+face1.width()] = face2_resized
|
37 |
+
img[face2.top():face2.top()+face2.height(), face2.left():face2.left()+face2.width()] = face1_resized
|
38 |
+
|
39 |
+
return img
|
40 |
+
|
41 |
+
@app.route('/')
|
42 |
+
def index():
|
43 |
+
return render_template('upload.html')
|
44 |
+
|
45 |
+
@app.route('/upload', methods=['POST'])
|
46 |
+
def upload_file():
|
47 |
+
if 'file' not in request.files:
|
48 |
+
return redirect(request.url)
|
49 |
+
file = request.files['file']
|
50 |
+
if file.filename == '':
|
51 |
+
return redirect(request.url)
|
52 |
+
if file and allowed_file(file.filename):
|
53 |
+
filename = secure_filename(file.filename)
|
54 |
+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
55 |
+
return redirect(url_for('process_file', filename=filename))
|
56 |
+
return redirect(request.url)
|
57 |
+
|
58 |
+
@app.route('/uploads/<filename>')
|
59 |
+
def uploaded_file(filename):
|
60 |
+
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
61 |
+
|
62 |
+
@app.route('/process/<filename>')
|
63 |
+
def process_file(filename):
|
64 |
+
image_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
65 |
+
faces, img = detect_faces(image_path)
|
66 |
+
|
67 |
+
if len(faces) < 2:
|
68 |
+
return "Need at least two faces to swap", 400
|
69 |
+
|
70 |
+
swapped_img = swap_faces(img, faces[0], faces[1])
|
71 |
+
|
72 |
+
result_filename = 'result_' + filename
|
73 |
+
result_path = os.path.join(app.config['UPLOAD_FOLDER'], result_filename)
|
74 |
+
cv2.imwrite(result_path, swapped_img)
|
75 |
+
|
76 |
+
return redirect(url_for('uploaded_file', filename=result_filename))
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
80 |
+
os.makedirs(UPLOAD_FOLDER)
|
81 |
+
app.run(debug=True)
|