-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacerecognize.py
130 lines (108 loc) · 4.19 KB
/
facerecognize.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File : facerecognize.py
@Time : 2019/9/30 17:30
@Author : cha0sCat
@Version : 1.0
@Contact : ***REMOVED***
@License : (C)Copyright 2017-2019, cha0sCat
@Desc : 单面孔人脸辨识
"""
from utils import timer
import numpy
import cv2
class FaceRecognize:
def __init__(self,
bin_path="models/20180408-102900.bin",
xml_path="models/20180408-102900.xml",
DEVICE="MYRIAD"):
from openvino.inference_engine import IENetwork, IECore
ie = IECore()
net = IENetwork(model=xml_path, weights=bin_path)
input_blob = next(iter(net.inputs))
output_blob = next(iter(net.outputs))
exec_net = ie.load_network(network=net, device_name=DEVICE)
n, c, network_input_h, network_input_w = net.inputs[input_blob].shape
self.input_blob = input_blob
self.output_blob = output_blob
self.exec_net = exec_net
self.network_input_h = network_input_h
self.network_input_w = network_input_w
def _preprocessImage(self, frame):
"""
将正常图片转换为适合神经网络的图片格式
:param frame: cv2读入的图片原图
:return:
"""
preprocessed_image = cv2.resize(frame, (self.network_input_w, self.network_input_h))
preprocessed_image = numpy.transpose(preprocessed_image)
preprocessed_image = numpy.reshape(preprocessed_image, (1, 3, self.network_input_w, self.network_input_h))
return preprocessed_image
def _runInference(self, image_to_classify):
"""
进行识别
:param image_to_classify: 经过_preprocessed_image处理的图像
:return: 识别结果
"""
results = self.exec_net.infer({self.input_blob: image_to_classify})
return results[self.output_blob].flatten()
def runImages(self, frame):
"""
对人脸进行辨识
:param frame: cv2的图像
:return: 人脸标志点
"""
image_to_classify = self._preprocessImage(frame)
nodes = self._runInference(image_to_classify)
return nodes
class cv2FaceRecognize:
def __init__(self,
bin_path="models/facenet-20180408-102900.bin",
xml_path="models/facenet-20180408-102900.xml",
network_input_h=160,
network_input_w=160):
net = cv2.dnn.readNet(xml_path, bin_path)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
self.net = net
self.network_input_h = network_input_h
self.network_input_w = network_input_w
def _preprocessImage(self, frame):
"""
将正常图片转换为适合神经网络的图片格式
:param frame: cv2读入的图片原图
:return:
"""
# blob = cv2.dnn.blobFromImage(frame, size=(self.network_input_w, self.network_input_h), ddepth=cv2.CV_8U)
blob = cv2.dnn.blobFromImage(frame, size=(self.network_input_w, self.network_input_h))
return blob
def _runInference(self, blob):
"""
进行识别
:param image_to_classify: 经过_preprocessed_image处理的图像
:return: 识别结果
"""
self.net.setInput(blob)
out = self.net.forward()
return out
@timer
def runImages(self, frame):
"""
对人脸进行辨识
:param frame: cv2的图像
:return: 人脸标志点
"""
blob = self._preprocessImage(frame)
nodes = self._runInference(blob)
return tuple(nodes[0])
if __name__ == '__main__':
#net = cv2FaceRecognize()
#face1 = net.runImages(cv2.imread("elvis.png"))[0]
#face2 = net.runImages(cv2.imread("valid_face.png"))[0]
net2 = FaceRecognize()
face1 = net2.runImages(cv2.imread("elvis.png"))
face2 = net2.runImages(cv2.imread("valid_face.png"))
# print(net2.runImages(cv2.imread("elvis.png")))
# print(len(net2.runImages(cv2.imread("elvis.png"))))
from utils import faceCompare
print(faceCompare(face1, face2))