-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfacecut.py
More file actions
44 lines (35 loc) · 1.2 KB
/
Copy pathfacecut.py
File metadata and controls
44 lines (35 loc) · 1.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
# Author: Ellis Madagan
# Last update: 2/25/2018
from PIL import Image
import face_recognition
import time
import MySQLdb as mysql
import base64
import os
DB_HOST = ''
DB_USER = ''
DB_PASSWORD = ''
DB_NAME = ''
def process_image(encoded_file):
# Set up database connection
db = mysql.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASSWORD, db=DB_NAME)
conn = db.cursor()
location = 'Rowan University'
# Write temp image
with open('temp/temp.jpg', 'wb') as file:
file.write(base64.b64decode(encoded_file))
image = face_recognition.load_image_file('temp/temp.jpg')
face_locations = face_recognition.face_locations(image)
count = 1
for face_location in face_locations:
top,right,bottom,left = face_location
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image_path = 'html/faces/' + str(int(time.time())) + '-' + str(count) + '.png'
count += 1
pil_image.save(open(pil_image_path, 'wb'))
conn.execute('INSERT INTO faces(img_path, location) VALUES ("{0}", "{1}")'.format(pil_image_path, location))
# Remove temp file
os.remove('temp/temp.jpg')
db.commit()
db.close()