-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
83 lines (63 loc) · 2.34 KB
/
app.py
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
import os
import cv2
import sys
from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory
from werkzeug.utils import secure_filename
from PIL import Image
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
APP_ROOT = "D:/Projects/Turtlehead"
# app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route("/")
def hello():
return render_template('index.html')
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file:
filename = 'static/img/userimg/' + file.filename
file.save(filename)
process_image(filename)
return render_template('index1.html', imgsrc='img/userimg/' + file.filename)
def process_image(path):
# Load turtlehead
turt = Image.open("turtle-head.png")
# Load image to be modified
img = Image.open(path)
# Resize image to desired width, maintaining aspect ratio
width, height = img.size
new_width = 680
new_height = new_width * height / width
scale_image(img, new_width, new_height)
img.save(path)
cv_img = cv2.imread(path) # Load image for use with OpenCV
faces = detect_faces(cv_img)
# For each face found, render turtle-head on top of it.
for (x, y, w, h) in faces:
turt.thumbnail((w * 1.5, h * 1.5))
img.paste(turt, (x,y), mask=turt)
img.save(path)
def scale_image(image, width, height):
image.thumbnail((width,height), Image.ANTIALIAS)
# Takes in an image, and returns a list of coordinates and size of detected faces in photo.
def detect_faces(img):
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
img,
scaleFactor=1.1,
minNeighbors=2,
minSize=(30,30),
)
return faces