This repository was archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyImageLibrary.py
81 lines (64 loc) · 2.13 KB
/
myImageLibrary.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
import numpy as np
import scipy.misc
import cv2
import glob
import os
import math
import pickle
import base64
def showimage(title,img):
cv2.imshow(title,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def extract_face(img,FACE_CASCADE,return_rectangles = False):
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
rectangles = FACE_CASCADE.detectMultiScale(img_gray, 1.3, 5)
faces = []
for (x,y,w,h) in rectangles:
cropped_face = img[y:y+h,x:x+w]
faces.append(cropped_face)
if return_rectangles:
return faces, rectangles
else:
return faces
def extract_faces_bulk(img_list,FACE_CASCADE):
return_list = []
for img in img_list:
try:
faces = extract_face(img,FACE_CASCADE)
if len(faces)>0:
return_list.append(faces[0])
except:
pass
return return_list
def crop_center(image,cropx,cropy):
w = image.shape[1]
h = image.shape[0]
startx = w//2-(cropx//2)
starty = h//2-(cropy//2)
return image[starty:starty+cropy, startx:startx+cropx]
def resize_crop(image,square_size):
w = image.shape[1]
h = image.shape[0]
min_dim = min(w,h)
max_square_image = crop_center(image, min_dim, min_dim)
result = cv2.resize(max_square_image,(square_size,square_size),0,0)
return result
def get_images(parent_directory):
results = glob.glob(os.path.join(parent_directory,'*.jpg'))+glob.glob(os.path.join(parent_directory,'*.jpeg'))+glob.glob(os.path.join(parent_directory,'*.png'))
images = []
for result in results:
images.append(cv2.imread(result))
return images
def preprocess(img,transpose=True,normalize=False):
#takes an image (face) of shape (x,x,3), transforms it to shape (1,3,x,x), pickles it and encodes to base64.
#also normalizes before encoding if normalize=True
#only transposes if tranpose=True
if transpose:
img = img.transpose(2,0,1)
if normalize:
img = np.around(img/255.0, decimals = 12)
img = np.expand_dims(img,axis=0)
img_pkl = pickle.dumps(img)
img_base64 = base64.b64encode(img_pkl)
return img_base64