forked from pooya-mohammadi/Face
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
62 lines (45 loc) · 1.39 KB
/
utils.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
import pickle
import numpy as np
import matplotlib.pyplot as plt
import cv2
from sklearn.preprocessing import Normalizer
# get encode
def get_encode(face_encoder, face, size):
face = normalize(face)
face = cv2.resize(face, size)
encode = face_encoder.predict(np.expand_dims(face, axis=0))[0]
return encode
def get_face(img, box):
x1, y1, width, height = box
x1, y1 = abs(x1), abs(y1)
x2, y2 = x1 + width, y1 + height
face = img[y1:y2, x1:x2]
return face, (x1, y1), (x2, y2)
l2_normalizer = Normalizer('l2')
def normalize(img):
mean, std = img.mean(), img.std()
return (img - mean) / std
def plt_show(cv_img):
img_rgb = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()
def load_pickle(path):
with open(path, 'rb') as f:
encoding_dict = pickle.load(f)
return encoding_dict
def save_pickle(path, obj):
with open(path, 'wb') as file:
pickle.dump(obj, file)
def read_vc(vc, func_to_call, break_print=':(', show=False, win_name='', break_key='q', **kwargs):
while vc.isOpened():
ret, frame = vc.read()
if not ret:
print(break_print)
break
res = func_to_call(frame, **kwargs)
if res is not None:
frame = res
if show:
cv2.imshow(win_name, frame)
if cv2.waitKey(1) & 0xff == ord(break_key):
break