Thiết kế hệ thống cơ khí

Một phần của tài liệu Nghiên cứu, thiết kế hệ thống nhận diện khuôn mặt sử dụng trí tuệ nhân tạo (Trang 58)

Khung của hệ thống được thiết kế trên phần mềm SolidWorks. Bao gồm một chân đỡ, bộ phận liên kết và màn hình. Chân đỡ và bộ phận liên kết là các thanh inox. Chân đỡ bao gồm một ống hình trụ và một mặt đế được gắn chặt với mặt đất. Kích thước của chân đỡ được thiết kế là: 1500 x 100 (mm)

Bộ phận liên kết được tích hợp trong chân đỡ và có thể thay đổi chiều dài dao động từ: 100 – 500 (mm).

Màn hình tích hợp camera được gắn với bộ phận liên kết. Kích thước của màn hình là 9"

45

46

CHƯƠNG 4 KẾT LUẬN VÀ ĐỊNH HƯỚNG PHÁT TRIỂN 4.1 Kết quả

Kết quả nhận diện thực tế được nhóm sinh viên ghi lại video và chụp ảnh kết quả. Kết quả đánh giá được thực hiện trên 2 phương pháp: nhận diện ảnh và nhận diện khuôn mặt trong thời gian thực.

Phương pháp nhận diện ảnh sẽ được áp dụng vào 2 mô hình VGG-16 và ResNet-50. Với 2 mô hình sử dụng thuật toán SVM và FaceNet sẽ đánh giá bằng phương pháp nhận diện khuôn mặt trong thời gian thực.

4.1.1Kết quả kiểm tra khi nhận diện ảnh Mô hình VGG-16 Mô hình VGG-16

Mô hình ResNet-50

Hình 49: Kết quả kiểm tra với mô hình VGG-16

47

4.1.2Kết quả kiểm tra khi nhận diện thực tế

Các trường hợp kiểm tra ở 2 địa điểm trong nhà và ngoài trời dựa trên những điều kiện sau:

- Đầy đủ ánh sáng (điều kiện lý tưởng) - Điều kiện ngược sáng

Riêng với địa điểm trong nhà, một trường hợp nữa được kiểm tra là phòng bị thiếu ánh sáng. Người thực hiện kiểm tra là 2 sinh viên đã có ảnh trong bộ dữ liệu và 1 người lạ mặt. Dưới đây là kết quả kiểm tra:

Đối với trường hợp ngoài trời độ rọi ≈ 650 lux Đối với trường hợp trong nhà độ rọi ≈ 400 lux

48

Trường hợp đủ ánh sáng ngoài trời

ĐKKT

PPND

1 người Nhiều người Người đeo kính, đội mũ

SVM

FaceNet

49

Trường hợp ngược sáng ngoài trời

ĐKKT

PPND

1 người Nhiều người Người đeo kính, đội mũ

SVM

FaceNet

50

Trường hợp đủ ánh sáng trong nhà

ĐKKT

PPND

1 người Nhiều người Người đeo kính, đội mũ

SVM

FaceNet

51

Trường hợp ngược sáng trong nhà

ĐKKT

PPND

1 người Nhiều người Người đeo kính, đội mũ

SVM

FaceNet

52

Trường hợp phòng thiếu sáng

ĐKKT

PPND

1 người Nhiều người Người đeo kính, đội mũ

SVM

FaceNet

53

4.2 Đánh giá

4.2.1Đối với thuật toán SVM

Trong trường hợp đầy đủ ánh sáng tại cả 2 địa điểm kiểm tra mô hình nhận diện đều cho kết quả chính xác và nhận diện tốt.

Tuy nhiên với trường hợp ánh sáng bị ngược thuật toán SVM xảy ra tình trạng nhận sai đối tượng nhận diện hoặc không thể nhận diện được khuôn mặt trong hình.

Đối với trường hợp phòng bị thiếu ánh sáng, thuật toán SVM vẫn cho ra kết quả nhận diện chính xác trong một số khung hình. Tuy nhiên độ ổn định là không cao.

4.2.2Đối với mô hình FaceNet

Mô hình FaceNet đã khắc phục rất tốt nhược điểm của thuật toán SVM trong trường hợp ngược sáng. Kết quả nhận diện cho ra độ chính xác tương đối cao và ổn định. Với trường hợp phòng bị thiếu ánh sáng mô hình FaceNet vẫn cho thấy điểm nổi trội hơn về độ chính xác.

54

Tuy nhiên trong những khung hình khuôn mặt nhận được lượng ánh sáng quá ít sẽ xảy ra trường hợp không thể phát hiện dẫn đến bỏ lỡ khuôn mặt.

Một nhược điểm nữa của mô hình FaceNet là thời gian phản hồi sẽ chậm hơn 10-15% so với thuật toán SVM trong cùng hoàn cảnh. Nhược điểm này có thể khắc phục bằng cách nâng cấp phần cứng của thiết bị nhận diện.

4.3 Kết luận và định hướng phát triển

4.3.1Kết luận

Từ những kết quả thu được có thể thấy thuật toán SVM cho kết quả tốt và độ chính xác cao ở những trường hợp lý tưởng và dữ liệu khuôn mặt có sẵn đã được huấn luyện. Cùng với đó thời gian phản hồi và training của thuật toán SVM đều rất tốt cũng như không đòi hỏi cấu hình phần cứng phức tạp.

Tuy nhiên trong những hoàn cảnh không lý tưởng thuật toán SVM không còn đáp ứng được bài toán nhận diện đặt ra.

Điều tương tự cũng xảy ra với 2 mô hình VGG và ResNet khi nhận diện đối tượng không có trong bộ dữ liệu. Tuy nhiên mô hình ResNet-50 tỏ ra vượt trội hơn so với VGG-16 khi tăng dữ liệu huấn luyện lên.

Từ đó ta có thể thấy mô hình FaceNet là tối ưu nhất trong những phương pháp được đánh giá trong bài báo cáo. Nó giải quyết những nhược điểm của các phương pháp đi trước như: số lượng dữ liệu có thể huấn luyện hạn chế, nhận diện những đối tượng không có trong bộ dữ liệu, nhận diện trong những hoàn cảnh không lý tưởng, v.v... Tuy nhiên nhược điểm của mô hình này là thời gian phản hồi chậm và đòi hỏi cao cấu hình phần cứng của thiết bị nhận diện để có thể hoạt động.

4.3.2Định hướng phát triển

Qua đề tài nghiên cứu này, nhóm sinh viên thực hiện hy vọng có thể đóng góp một hướng tiếp cận mới cho bài toán nhận diện khuôn mặt. Bên cạnh đó hứa hẹn mở ra những mô hình tương tự phục vụ nhiều mục đích nghiên cứu cũng như ứng dụng trong cuộc sống hiện đại có thể kể đến như:

55 - Hệ thống nhận diện tội phạm

- Hệ thống giao tiếp thông minh giữa con người và máy tính - Xác minh sinh trắc học khuôn mặt bằng camera 3D

56

TÀI LIỆU THAM KHẢO

[1] Reimers, Nils; Gurevych, Iryna (2019-08-27). "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks".

[2] Ailon, Nir; Hoffer, Elad (2014-12-20). "Deep metric learning using Triplet network".

[3] Hermans, Alexander; Beyer, Lucas; Leibe, Bastian (2017-03-22). "In Defense of the Triplet Loss for Person Re-Identification".

[4] Sergios Theodoridis and Konstantinos Koutroumbas "Pattern Recognition", 4th Edition, Academic Press, 2009.

[5] Zhou, Mo; Niu, Zhenxing; Wang, Le; Gao, Zhanning; Zhang, Qilin; Hua, Gang (2020-04-03). "Ladder Loss for Coherent Visual-Semantic Embedding".

[6] Nello Cristianini and John Shawe-Taylor. “An Introduction to Support Vector Machines and other kernel-based learning methods”. Cambridge University Press, 2000.

[7] Vojislav Kecman: "Learning and Soft Computing - Support Vector Machines, Neural Networks, Fuzzy Logic Systems", The MIT Press, Cambridge, MA, 2001. [8] Bernhard Schölkopf, Christopher J.C. Burges, and Alexander J. Smola (editors). "Advances in Kernel Methods: Support Vector Learning". MIT Press, Cambridge, MA, 1999.

[9] Ingo Steinwart and Andreas Christmann. “Support Vector Machines”. Springer- Verlag, New York, 2008.

[10] Vladimir Vapnik, S.Kotz "Estimation of Dependences Based on Empirical Data" Springer, 2006.

[11] Ovidiu Ivanciuc, "Applications of Support Vector Machines in Chemistry", In: Reviews in Computational Chemistry, Volume 23, 2007.

57

[12] Catanzaro, Sundaram, Keutzer, "Fast Support Vector Machine Training and Classification on Graphics Processors", In: International Conference on Machine Learning, 2008.

[13] Chechik, G.; Sharma, V.; Shalit, U.; Bengio, S. (2010). "Large Scale Online Learning of Image Similarity Through Ranking".

[14] Dmitriy Fradkin and Ilya Muchnik "Support Vector Machines for Classification" in J. Abello and G. Carmode (Eds) "Discrete Methods in Epidemiology", DIMACS Series in Discrete Mathematics and Theoretical Computer Science, volume 70, pp. 13–20, 2006.

[15] Kristin P. Bennett and Colin Campbell, "Support Vector Machines: Hype or Hallelujah?", SIGKDD Explorations, 2,2, 2000.

[16] Kumar Chellapilla; Sid Puri; Patrice Simard (2006). "High Performance Convolutional Neural Networks for Document Processing".

[17] He, Kaiming; Zhang, Xiangyu; Ren, Shaoqing; Sun, Jian (2016). "Deep Residual Learning for Image Recognition".

[18] Venkatesan, Ragav; Li, Baoxin (2017-10-23). “Convolutional Neural Networks in Visual Computing”.

[19] Romanuke, Vadim (2017). "Appropriate number and allocation of ReLUs in convolutional neural networks".

[20] Richard, Zhang (2019-04-25). "Making Convolutional Networks Shift-Invariant Again".

[21] Bonsor, K. (September 4, 2001). "How Facial Recognition Systems Work". [22] Harley Geiger (December 6, 2011). "Facial Recognition and Privacy".

58

PHỤ LỤC Code

Mô hình VGG

from keras.applications import vgg16 # VGG16 anh target 224x224

img_rows = 224 img_cols = 224 #load model vgg16

model = vgg16.VGG16(weights = 'imagenet', include_top = False,

input_shape = (img_rows, img_cols, 3)) for (i,layer) in enumerate(model.layers):

print(str(i) + " "+ layer.__class__.__name__, layer.trainable) from keras.applications import vgg16

# su dung 4 layers cuối for layer in model.layers: layer.trainable = False

for (i,layer) in enumerate(model.layers):

print(str(i) + " "+ layer.__class__.__name__, layer.trainable) def addTopModel(bottom_model, num_classes, D=256):

top_model = bottom_model.output

top_model = Flatten(name = "flatten")(top_model) top_model = Dense(D, activation = "relu")(top_model)

59

top_model = Dense(D, activation = "relu")(top_model) top_model = Dense(D, activation = "relu")(top_model) top_model = Dropout(0.3)(top_model)

top_model = Dense(num_classes, activation = "sigmoid")(top_model) return top_model

from tensorflow.keras.models import Sequential from keras.models import Sequential

from keras.layers import Dense, Dropout, Activation, Flatten

from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D from keras.layers import LayerNormalization

from keras.models import Model num_classes =52 # so class

FC_Head = addTopModel(model, num_classes)

modelnew = Model(inputs=model.input, outputs=FC_Head) print(modelnew.summary())

#load data va pp data agument

from keras.preprocessing.image import ImageDataGenerator train_data_dir = '/content/drive/MyDrive/XLA/DuLieu1/Train' validation_data_dir = '/content/drive/MyDrive/XLA/DuLieu1/Val' train_datagen = ImageDataGenerator(

rescale=1./255, rotation_range=20,

60 width_shift_range=0., height_shift_range=0.1, horizontal_flip=True, fill_mode='nearest') validation_datagen = ImageDataGenerator(rescale=1./255) # lợ chọn thông số theo phần cứng máy train

train_batchsize = 32 val_batchsize = 16 img_rows=224 img_cols=224 train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_rows, img_cols), batch_size=train_batchsize, class_mode='categorical') validation_generator = validation_datagen.flow_from_directory( validation_data_dir, target_size=(img_rows, img_cols), batch_size=val_batchsize, class_mode='categorical', shuffle=False)

# train va luu lai weight

61

from keras.callbacks import ModelCheckpoint, EarlyStopping checkpoint = ModelCheckpoint("weight52class.h5",

monitor="val_loss", mode="min",

save_best_only = True, verbose=1)

earlystop = EarlyStopping(monitor = 'val_loss', min_delta = 0,

patience = 3, verbose = 1,

restore_best_weights = True) callbacks = [earlystop, checkpoint]

modelnew.compile(loss = 'categorical_crossentropy', optimizer = RMSprop(lr = 0.0001), metrics = ['accuracy']) nb_train_samples = 256 nb_validation_samples = 64 epochs = 256 batch_size = 16 history = modelnew.fit_generator( train_generator,

steps_per_epoch = nb_train_samples // batch_size, epochs = epochs,

62

callbacks = callbacks,

validation_data = validation_generator,

validation_steps = nb_validation_samples // batch_size)

modelnew.save("weigh52class.h5") #kiem tra model

classifier_model=tf.keras.models.load_model('weigh52class.h5') test_images_path=path+'/Test_Images/'

dnnFaceDetector=dlib.cnn_face_detection_model_v1("mmod_human_face_detecto r.dat")

os.mkdir(path+'/Predictions')

for img_name in os.listdir('Test_Images/'): if img_name=='crop_img.jpg':

continue

# Load Image

img=cv2.imread(path+'/Test_Images/'+img_name) gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # phat hien khuon mat

rects=dnnFaceDetector(gray,1) left,top,right,bottom=0,0,0,0 for (i,rect) in enumerate(rects): # Extract Each Face

63

top=rect.rect.top() #y1 right=rect.rect.right() #x2 bottom=rect.rect.bottom() #y2 width=right-left

height=bottom-top

img_crop=img[top:top+height,left:left+width]

cv2.imwrite(path+'/Test_Images/crop_img.jpg',img_crop)

crop_img=load_img(path+'/Test_Images/crop_img.jpg',target_size=(224,224)) crop_img=img_to_array(crop_img)

crop_img=np.expand_dims(crop_img,axis=0) crop_img=preprocess_input(crop_img) img_encode=vgg_face(crop_img) embed=K.eval(img_encode)

person=classifier_model.predict(embed) name=person_rep[np.argmax(person)]

os.remove(path+'/Test_Images/crop_img.jpg')

cv2.rectangle(img,(left,top),(right,bottom),(0,255,0), 2) img=cv2.putText(img,name,(left,top-

10),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,255),2,cv2.LINE_AA)

img=cv2.putText(img,str(np.max(person)),(right,bottom+10),cv2.FONT_HERSHEY _SIMPLEX,0.5,(0,0,255),1,cv2.LINE_AA)

# Save images with bounding box,name and accuracy cv2.imwrite(path+'/Predictions/'+img_name,img)

64 plot(img) Mô hình Resnet50 import os print(os.listdir("../input")) print(os.listdir("../input/keras-pretrained-models/")) data_dir = '../input/data54class/Data54new' vgg16weight = '../input/keras-pretrained- models/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' resnet50weight = '../input/keras-pretrained- models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' from keras.preprocessing.image import ImageDataGenerator from keras.models import Model, Sequential

from keras.layers import Conv2D, MaxPooling2D, BatchNormalization, GlobalAveragePooling2D

from keras.layers import Activation, Dropout, Flatten, Dense from keras.optimizers import RMSprop, SGD

from keras import backend as K import keras

import matplotlib.pyplot as plt

img_width, img_height = 200, 200

train_data_dir = os.path.join(data_dir, 'Train') validation_data_dir = os.path.join(data_dir, 'Val')

65 test_data_dir= os.path.join(data_dir,'Test') nb_train_samples = 64 #93 nb_validation_samples = 25 epochs = 250 batch_size = 16 numclasses =52 train_datagen = ImageDataGenerator( rescale=1. / 255,

rotation_range=10, # randomly rotate images in the range (degrees, 0 to 180) zoom_range = 0.1, # Randomly zoom image

width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)

height_shift_range=0.1, # randomly shift images vertically (fraction of total height) vertical_flip=False, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') validation_generator = test_datagen.flow_from_directory(

66 validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') if K.image_data_format() == 'channels_first': input_shape = (3, img_width, img_height) else:

input_shape = (img_width, img_height, 3

def vgg16CNNtl(input_shape, outclass, sigma='sigmoid'):

base_model = None

base_model = keras.applications.VGG16(weights=None, include_top=False, input_shape=input_shape) base_model.load_weights(vgg16weight) top_model = Sequential() top_model.add(Flatten(input_shape=base_model.output_shape[1:])) for i in range(2): top_model.add(Dense(4096, activation='relu')) top_model.add(Dropout(0.5)) top_model.add(Dense(outclass, activation=sigma))

67 model = None model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) return model

def resnet50tl(input_shape, outclass, sigma='sigmoid'): base_model = None base_model = keras.applications.resnet50.ResNet50(weights=None, include_top=False, input_shape=input_shape) base_model.load_weights(resnet50weight) top_model = Sequential() top_model.add(Flatten(input_shape=base_model.output_shape[1:])) for i in range(2): top_model.add(Dense(4096, activation='relu')) top_model.add(Dropout(0.5)) top_model.add(Dense(outclass, activation=sigma)) model = None model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) return model

model = resnet50tl(input_shape, numclasses, 'softmax') lr = 1e-5

68

optimizer = RMSprop(lr=lr, decay=decay) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) history = model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size) saveweight = 'ce1803_weight.h5' model.save_weights(saveweight) test_data_dir= os.path.join(data_dir,'Test') labels = (‘label.txt’) 'Trieu_Le_Dinh','ronaldo'] test_imgs = ['Nicole_Gale_Anderson/aug_0_5525.png'] for test in test_imgs:

test_img = os.path.join(validation_data_dir, test)

img = image.load_img(test_img, target_size=(img_width, img_height)) x = image.img_to_array(img)

x = np.expand_dims(x, axis=0) x /= 255.

69

result = np.squeeze(classes)

result_indices = np.argmax(result)

img = cv2.imread(test_img, cv2.IMREAD_COLOR) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.axis('off')

plt.title("{}, {:.2f}%".format(labels[result_indices], result[result_indices]*100)) plt.imshow(img)

Facenet

Main

from __future__ import absolute_import from __future__ import division

from __future__ import print_function

import PyQt5.QtWidgets as QtWidgets

from PyQt5.QtCore import QThread, Qt, pyqtSignal, pyqtSlot

from PyQt5.QtWidgets import QFormLayout, QLineEdit, QWidget, QApplication, QMainWindow

from PyQt5.QtWidgets import QPushButton, QPlainTextEdit, QHBoxLayout, QVBoxLayout

from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtGui import QPixmap

from PyQt5.QtWidgets import QLabel from other2 import Ui_Otherwindow import sys

import cv2

70

import os

from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * from os.path import expanduser

from sklearn.model_selection import GridSearchCV from sklearn.preprocessing import LabelEncoder from sklearn.svm import SVC

import tensorflow as tf import argparse import facenet import os import sys import math import pickle import align.detect_face import numpy as np import cv2 import collections import time import datetime import imutils

from datetime import datetime from sklearn.svm import SVC

# from colab import chrome as col class Thread(QThread):

changePixmap = pyqtSignal(QImage) def run(self):

71

#code nhan dien

parser = argparse.ArgumentParser()

parser.add_argument('--path', help='Path of the video you want to test on.', default=0)

args = parser.parse_args() # Cai dat cac tham so can thiet MINSIZE = 20 THRESHOLD = [0.6, 0.7, 0.7] FACTOR = 0.709 IMAGE_SIZE = 182 INPUT_IMAGE_SIZE = 160 CLASSIFIER_PATH = r'D:\Project\Code\Nhandienkhuonmat\Models\facemodel.pkl' VIDEO_PATH = args.path FACENET_MODEL_PATH = r'D:\Project\Code\Nhandienkhuonmat\Models\20180402-114759.pb' srcpath =r'D:\Project\Code\Nhandienkhuonmat\src\align'

# Load model da train de nhan dien khuon mat - thuc chat la classifier with open(CLASSIFIER_PATH, 'rb') as file:

model, class_names = pickle.load(file)

print("Custom Classifier, Successfully loaded") with tf.Graph().as_default():

# Cai dat GPU neu co

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))

with sess.as_default():

# Load model MTCNN phat hien khuon mat print('Loading feature extraction model')

facenet.load_model(FACENET_MODEL_PATH) # Lay tensor input va output

72 images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1]

# Cai dat cac mang con

pnet, rnet, onet = align.detect_face.create_mtcnn(sess, r"D:\Project\Code\Nhandienkhuonmat\src\align")

people_detected = set()

person_detected = collections.Counter() # Lay hinh anh tu file video

cap = cv2.VideoCapture(0) #cap = cv2.VideoCapture(r'D:\Project\Code\FaceRecognition\Video\Thieusang.mp4') #cap = cv2.VideoCapture("rtsp://admin:Abc12345@192.168.0.100:554/Streaming/channel s/1/", cv2.CAP_FFMPEG) while (cap.isOpened()): # Doc tung frame

ret, frame = cap.read() #dung cam 2D

frame = imutils.resize(frame, width=800) # Dung cam IP

'''

scale_percent = 55 # percent of original size width = int(frame.shape[1] * scale_percent / 100) height = int(frame.shape[0] * scale_percent / 100) dim = (width, height)

frame = cv2.resize(frame, dim) '''

73

# Phat hien khuon mat, tra ve vi tri trong bounding_boxes

bounding_boxes, _ = align.detect_face.detect_face(frame, MINSIZE, pnet, rnet, onet, THRESHOLD, FACTOR)

faces_found = bounding_boxes.shape[0] try:

# Neu co it nhat 1 khuon mat trong frame if faces_found > 0: det = bounding_boxes[:, 0:4] bb = np.zeros((faces_found, 4), dtype=np.int32) for i in range(faces_found): bb[i][0] = det[i][0] bb[i][1] = det[i][1] bb[i][2] = det[i][2] bb[i][3] = det[i][3]

# Cat phan khuon mat tim duoc

cropped = frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :] scaled = cv2.resize(cropped, (INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE),

interpolation=cv2.INTER_CUBIC) scaled = facenet.prewhiten(scaled)

scaled_reshape = scaled.reshape(-1, INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, 3)

feed_dict = {images_placeholder: scaled_reshape, phase_train_placeholder: False}

emb_array = sess.run(embeddings, feed_dict=feed_dict) # Dua vao model de classifier

predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[

74

# Lay ra ten va ty le % cua class co ty le cao nhat best_name = class_names[best_class_indices[0]] #print("name: {}, Probability: {}".format(best_name, best_class_probabilities))

# Ve khung mau xanh quanh khuon mat

cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (255, 0, 0), 2)

text_x = bb[i][0] text_y = bb[i][3] + 20

# Neu ty le nhan dang > 0.5 thi hidsaen thi ten if best_class_probabilities > 0.1:

name = class_names[best_class_indices[0]] else:

# Con neu <=0.5 thi hien thi Unknown name = "Unknown”

#Viet text len tren frame

cv2.putText(frame, name, (text_x, text_y), cv2.FONT_HERSHEY_COMPLEX_SMALL,

1, (255, 255, 255), thickness=1, lineType=2) except:

pass

# Hien thi frame len man hinh

rgbframe = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = rgbframe.shape

bytesPerline = ch*w

convertToQtFormat = QImage(rgbframe.data, w, h, bytesPerline, QImage.Format_RGB888)

p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio) self.changePixmap.emit(p)

cap.release()

75 def __init__(self): super(mainWin, self).__init__() self.initUI() # self.web = colab.chrome_webbrowser() def openWindow(self): self.window = QtWidgets.QMainWindow() self.ui = Ui_Otherwindow() self.ui.setupUi(self.window) self.window.show()

self.editorOutput.insertPlainText("Thêm người mới\n") @pyqtSlot(QImage)

def setImage(self, image):

self.label2.setPixmap(QPixmap.fromImage(image)) #Button 1: web

'''

def button_web(self):

self.web.connect_chrome()

self.editorOutput.insertPlainText("Open Colab On Webbrowser!\n")''' def buttonstartend(self): th = Thread(self) th.changePixmap.connect(self.setImage) th.start() self.editorOutput.insertPlainText("Open camera!\n") def buttontraininglocal(self):

self.editorOutput.insertPlainText("Đang trích xuất khuôn mặt\n") os.system("python src/align_dataset_mtcnn.py Dataset/FaceData/raw Dataset/FaceData/processed --image_size 160 --margin 32 --random_order -- gpu_memory_fraction 0.25")

def buttonexportimages(self):

76

os.system("python src/classifier.py TRAIN Dataset/FaceData/processed Models/20180402-114759.pb Models/facemodel.pkl --batch_size 600") def quit(self):

exit(0)

def initUI(self):

self.title = "FACE RECOGNITION" self.setGeometry(0, 0, 1520, 980) self.setWindowTitle(self.title) #Main Label

self.label1 = QtWidgets.QLabel(self) self.label1.resize(1000, 200)

self.label1.setText("<h1>HỆ THỐNG NHẬN DIỆN KHUÔN MẶT<h1>") self.label1.setStyleSheet("color:blue;") self.label1.setFont(QFont('Times', 15)) self.label1.move(370, 50) self.label4 = QtWidgets.QLabel(self) self.label4.resize(1000, 200) self.label4.setText("<h1>ĐỒ ÁN TỐT NGHIỆP<h1>") self.label4.setStyleSheet("color:blue;") self.label4.setFont(QFont('Times', 16)) self.label4.move(500, 5) self.label5 = QtWidgets.QLabel(self) self.label5.resize(1000, 200)

self.label5.setText("<h1>GVHD: TS. NGUYỄN VĂN TRƯỜNG<h1>") self.label5.setStyleSheet("color: rgb(46,141,80);")

self.label5.setFont(QFont('Times', 8)) self.label5.move(90, 450)

77

self.label6.resize(1000, 200)

self.label6.setText("<h1>SINH VIÊN THỰC HIỆN:<h1>") self.label6.setStyleSheet("color: rgb(46,141,80);")

self.label6.setFont(QFont('Times', 8)) self.label6.move(90, 500)

self.label7 = QtWidgets.QLabel(self) self.label7.resize(1000, 200)

self.label7.setText("<h1>NGUYỄN QUỐC HƯNG<h1>") self.label7.setStyleSheet("color:rgb(46,141,80);") self.label7.setFont(QFont('Times', 7)) self.label7.move(150, 550) self.label8 = QtWidgets.QLabel(self) self.label8.resize(1000, 200) self.label8.setText("<h1>NGUYỄN QUANG SÁNG<h1>") self.label8.setStyleSheet("color:rgb(46,141,80);") self.label8.setFont(QFont('Times', 7)) self.label8.move(150, 600) self.label3 = QtWidgets.QLabel(self) self.pixmap = QPixmap('LOGOHAUI.jpg') self.pixmap = self.pixmap.scaledToWidth(500/3) self.pixmap = self.pixmap.scaledToHeight(500/3) self.label3.setPixmap(self.pixmap) self.label3.resize(500/3, 500/3) self.label3.move(20, 20)

78 self.label9 = QtWidgets.QLabel(self) self.pixmap = QPixmap('Logo_irobot.jpg') self.pixmap = self.pixmap.scaledToWidth(500 / 3) self.pixmap = self.pixmap.scaledToHeight(500 / 3) self.label9.setPixmap(self.pixmap) self.label9.resize(500 / 3, 500 / 3) self.label9.move(1330, 20) #label3.setScaledContents(True) #self.setCentralWidget(label3)

#Real time part

self.label2 = QtWidgets.QLabel(self)

self.label2.setText("NHẤN START ĐỂ MỞ CAMERA") self.label2.setFont(QFont('Times', 15)) self.label2.move(680, 200) self.label2.resize(640, 480) th = Thread(self) th.changePixmap.connect(self.setImage) # th.start() #Terminal Notification self.editorOutput = QPlainTextEdit(self) self.editorOutput.setFont(QFont('Times', 10)) self.editorOutput.insertPlainText("CHÀO MỪNG BẠN ĐẾN VỚI HỆ THỐNG!!!\n") self.editorOutput.move(90, 230) self.editorOutput.resize(350, 200) self.editorOutput.setReadOnly(True)

79

#Button training colab

# self.buttonweb = QtWidgets.QPushButton(self) # self.buttonweb.setText("Training Colab") # self.buttonweb.move(750, 600)

# self.buttonweb.clicked.connect(self.button_web)

#Button training Local

self.buttontrain_local = QtWidgets.QPushButton(self) self.buttontrain_local.setText("Trích xuất\nkhuôn mặt") self.buttontrain_local.resize(100, 100)

self.buttontrain_local.move(400, 800)

self.buttontrain_local.setFont(QFont('Times', 15))

self.buttontrain_local.setStyleSheet("background-color : yellow; color: black") self.buttontrain_local.clicked.connect(self.buttontraininglocal) #Button start/end self.buttonstd = QtWidgets.QPushButton(self) self.buttonstd.setText("Start") self.buttonstd.resize(100, 100) self.buttonstd.move(800, 800) self.buttonstd.setFont(QFont('Times', 15))

self.buttonstd.setStyleSheet("background-color : green; color: white") self.buttonstd.clicked.connect(self.buttonstartend)

#Button export image

self.buttonexport = QtWidgets.QPushButton(self) self.buttonexport.setText("Training")

self.buttonexport.resize(100, 100) self.buttonexport.move(600, 800)

80

self.buttonexport.setStyleSheet("background-color : blue; color: white") self.buttonexport.clicked.connect(self.buttonexportimages)

#Button choosing dataset

self.buttondata = QtWidgets.QPushButton(self) self.buttondata.setText("Thêm mới")

self.buttondata.resize(100, 100) self.buttondata.move(200, 800)

self.buttondata.setFont(QFont('Times', 15))

self.buttondata.setStyleSheet("background-color : gray; color: white") self.buttondata.clicked.connect(self.openWindow)

#Button quit cam

self.buttondata = QtWidgets.QPushButton(self) self.buttondata.setText("Quit")

self.buttondata.resize(100, 100)

Một phần của tài liệu Nghiên cứu, thiết kế hệ thống nhận diện khuôn mặt sử dụng trí tuệ nhân tạo (Trang 58)