-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
83 lines (70 loc) · 2.87 KB
/
api.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
75
76
77
78
79
80
81
82
83
import face_recognition
from PIL import Image, ImageTk
import os
import cv2 as cv
import time
from numpy import inexact
BASE_DIR = os.getcwd()
dir_path = os.path.join(BASE_DIR, 'test_image')
basedir = os.path.abspath(os.path.dirname(__file__))
def crop_image(dir_image):
image = face_recognition.load_image_file(dir_image)
face_locations = face_recognition.face_locations(image)
# print(face_locations)
# print("Tìm được {} khuôn mặt trong bức ảnh.".format(len(face_locations)))
index = 0
lst_image_detection = []
for face_location in face_locations:
# in tung anh và vi tri
top, right, bottom, left = face_location
# print("Khuôn mặt nằm ở vị trí pixel Top: {}, Right: {}, Bottom: {}, Left: {}".format(
# top, right, bottom, left))
# luu anh
path_folder = basedir+'/image_detection'
if not os.path.exists(path_folder):
os.mkdir(path_folder)
name_image = ''
location_1 = 0
try:
location_1 = dir_image.rindex("\\")+1
name_image = dir_image[location_1:]
location_2 = name_image.rindex('.')
name_image = name_image[:location_2]
except:
location_1 = dir_image.rindex("/")+1
name_image = dir_image[location_1:]
location_2 = name_image.rindex('.')
name_image = name_image[:location_2]
face_image = image[top:bottom, left:right]
name_image_detection = '{}{}{}.png'.format('D:/Baitapcacmon/Laptrinhhethong/face_detection/image_detection/',name_image+'_pic', index)
dict_info = {}
dict_info['file_path'] = name_image_detection
dict_info['note'] = 'Ảnh khuôn mặt thứ ' + str(index+1) + ' cắt từ ' + dir_image[location_1:]
lst_image_detection.append(dict_info)
Image.fromarray(face_image).save('{}{}{}.png'.format('D:/Baitapcacmon/Laptrinhhethong/face_detection/image_detection/',name_image+'_pic', index))
index+=1
# truy cập ảnh
# face_image = image[top:bottom, left:right]
# pil_image = Image.fromarray(face_image)
# pil_image.show()
return lst_image_detection
def read_file_from_folder(dir_path):
files = os.listdir(dir_path)
for file in files:
img_path = os.path.join(dir_path, file)
image = cv.imread(img_path, cv.IMREAD_GRAYSCALE)
cv.imshow('image', image)
cv.waitKey(3000)
cv.destroyAllWindows()
return True
def get_list_file_path_from_folder(dir_path):
files = os.listdir(dir_path)
list_file_path = []
for file in files:
img_path = os.path.join(dir_path, file)
list_file_path.append(img_path)
return list_file_path
# read_file_from_folder(dir_path)
# list_file_path = get_list_file_path_from_folder(dir_path)
# for file_path in list_file_path:
# crop_image(file_path)