Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,14 @@
|
|
| 1 |
-
|
| 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)
|
|
@@ -38,44 +31,34 @@ def swap_faces(img, face1, face2):
|
|
| 38 |
|
| 39 |
return img
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 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"
|
| 69 |
|
| 70 |
swapped_img = swap_faces(img, faces[0], faces[1])
|
| 71 |
|
| 72 |
-
result_filename = '
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
app.run(debug=True)
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
import dlib
|
| 4 |
import numpy as np
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
UPLOAD_FOLDER = 'uploads'
|
| 8 |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'mp4', 'avi'}
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
detector = dlib.get_frontal_face_detector()
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
def detect_faces(image_path):
|
| 13 |
img = cv2.imread(image_path)
|
| 14 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 31 |
|
| 32 |
return img
|
| 33 |
|
| 34 |
+
def process_file(image):
|
| 35 |
+
if image is None:
|
| 36 |
+
return "Please upload an image."
|
| 37 |
+
|
| 38 |
+
filename = os.path.join(UPLOAD_FOLDER, 'uploaded_image.jpg')
|
| 39 |
+
cv2.imwrite(filename, image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
faces, img = detect_faces(filename)
|
| 42 |
if len(faces) < 2:
|
| 43 |
+
return "Need at least two faces to swap"
|
| 44 |
|
| 45 |
swapped_img = swap_faces(img, faces[0], faces[1])
|
| 46 |
|
| 47 |
+
result_filename = os.path.join(UPLOAD_FOLDER, 'result_image.jpg')
|
| 48 |
+
cv2.imwrite(result_filename, swapped_img)
|
| 49 |
+
return swapped_img
|
| 50 |
+
|
| 51 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
| 52 |
+
os.makedirs(UPLOAD_FOLDER)
|
| 53 |
|
| 54 |
+
iface = gr.Interface(
|
| 55 |
+
fn=process_file,
|
| 56 |
+
inputs=gr.Image(type="numpy", label="Upload an Image"),
|
| 57 |
+
outputs=gr.Image(type="numpy", label="Swapped Faces"),
|
| 58 |
+
title="Face Swap",
|
| 59 |
+
description="Upload an image with at least two faces, and this tool will swap the faces."
|
| 60 |
+
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
+
iface.launch()
|
| 64 |
+
|
|
|