-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess.py
59 lines (54 loc) · 1.57 KB
/
preprocess.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
"""
Pre-process raw Trump pics, by detecting a face, stripping it,
and resizing it (keeping the aspect ratio) and then saving it
on disk, under the `/data` directory.
"""
import os
from concurrent import futures
import threading
import cv2, imutils
import numpy as np
import face_recognition
from PIL import Image
rawdir = "raw"
#
images = []
#
# Resize all pictures to the same dims, because dlib throws a bitchfit if they are
# different! Go for a simple size (512x512) for face detection
# (if that's too small, then we don't really want it anyway)
#
def voodoo(fname):
image = face_recognition.load_image_file(fname)
coords = face_recognition.face_locations(image)
#
# left, top right, bottom
#
i = 0
for idx in coords:
print(idx)
top, right, bottom, left = idx
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
size = 224, 224
pil_image.thumbnail(size)
file = os.path.basename(fname) + "_" + str(i)
file = os.path.join("trump_faces", file)
print(file)
pil_image.save(file, "jpeg")
i += 1
#
# Load the batch in RAM (if this brakes your puny puter then don't use a batch)
# I have 6 cores and 12 threads, so do the math yo!
#
jobs = []
with futures.ThreadPoolExecutor(max_workers=12) as ex:
for filename in os.listdir(rawdir):
if filename.endswith(".jpg"):
img_file = os.path.join(rawdir, filename)
jobs.append(ex.submit(voodoo, img_file))
images = []
i = 0
for f in jobs:
f.result()
#