forked from waslleysouza/face_recognition_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
51 lines (35 loc) · 1.26 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
import cv2
import os
import json
from PIL import Image
def get_content_type(file):
return file.content_type.split("/")
def create_directories(directory_list):
for path in directory_list:
create_directory(path)
def create_directory(path):
if not os.path.exists(path):
os.makedirs(path)
print("Directory created: {!r}".format(path))
def resize_image(image, width):
if image.size[1] >= width:
wpercent = (width / float(image.size[0]))
hsize = int((float(image.size[1]) * float(wpercent)))
image = image.resize((width, hsize), Image.ANTIALIAS)
def get_rotate_code(rotate):
rotate_code = cv2.ROTATE_90_CLOCKWISE
if rotate == 180:
rotate_code = cv2.ROTATE_180
elif rotate == 270:
rotate_code = cv2.ROTATE_90_COUNTERCLOCKWISE
return rotate_code
def load_json_file(file_path):
with open(file_path, 'r') as f:
return json.load(f)
def rotate_cv2_image(cv2_image, rotate):
if rotate is not None:
return cv2.rotate(cv2_image, rotateCode=get_rotate_code(int(rotate)))
def save_cv2_image(directory, image_name, cv2_image):
image_path = os.path.join(directory, image_name)
cv2.imwrite(image_path, cv2_image)
print("File created: {!r}".format(image_path))