对深度学习完全陌生的人无法了解发生了什么。我正在实现一个django应用程序来实时识别人脸。在两个不同的环境中安装了使用pip和conda的软件包,但没有获得任何成功。在这里,我使用MTCNN来检测人脸,然后我建立了识别人脸的模型。虽然没有django的情况很好,但他们没有与django合作。 更新-发现了另一件事-当单独测试它(没有django)并且使用Keras==2.2.5时,这个脚本甚至没有运行。我相信它与MTCNN有一些问题。 enter image description here
import cv2
进口泡菜 从PIL导入图像 从numpy导入加载 从numpy导入展开\u dims 从numpy进口asarray 从mtcnn.mtcnn导入mtcnn 从tensorflow.keras.models导入负载_模型
从sklearn.preprocessing导入标签编码器
类实时人脸检测:
def __init__(self):
self.stroke = 1
self.detector = MTCNN()
self.video_cap = cv2.VideoCapture(0)
self.color = (255, 0, 0)
print("Loading pre-trained Keras model for face recognition")
self.keras_model = load_model('facenet_keras.h5', compile=False)
print("Face recognition model loaded successfully...")
print("Loading pre-trained SVC model")
self.svc_model = pickle.load(open('FACENET_MODEL.sav', 'rb'))
print("Loading successful...")
self.emb_data = load('5-celebrity-faces-embeddings.npz')
def img_to_array(self, face_img_pixels, required_size=(160, 160)):
image = Image.fromarray(face_img_pixels)
image = image.resize(required_size)
return asarray(image)
# Get the face embedding for one face
def get_embedding(self, model, face_pixels):
face_pixels = face_pixels.astype('float32')
mean, std = face_pixels.mean(), face_pixels.std()
face_pixels = (face_pixels - mean) / std
samples = expand_dims(face_pixels, axis=0)
yhat = model.predict(samples)
return yhat[0]
def get_encoder(self):
trainy = self.emb_data['arr_1']
# Label encode targets
out_encoder = LabelEncoder()
out_encoder.fit(trainy)
return out_encoder
def find_faces(self):
out_encoder = self.get_encoder()
while(self.video_cap.isOpened()):
ret, frame = self.video_cap.read()
rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pixels = asarray(rgb_image)
all_faces = self.detector.detect_faces(pixels)
for face in all_faces:
x1, y1, width, height = face['box']
x1, y1 = abs(x1), abs(y1)
x2, y2 = x1 + width, y1 + height
face_arr = self.img_to_array(pixels[y1:y2, x1:x2])
face_emb = self.get_embedding(self.keras_model, face_arr)
samples = expand_dims(face_emb, axis=0)
yhat_class = self.svc_model.predict(samples)
yhat_prob = self.svc_model.predict_proba(samples)
class_index = yhat_class[0]
class_probability = yhat_prob[0, class_index] * 100
class_probability = round(class_probability)
if class_probability > 95.0:
predict_names = out_encoder.inverse_transform(yhat_class)
print('Predicted: %s (%f)' % (predict_names[0], class_probability))
else:
print("Low accuracy, probability = %f" % (class_probability))
目前没有回答
相关问题 更多 >
编程相关推荐