- 本次就使用PaddleInference將PaddleHub上的兩個(gè)模型串聯(lián)起來,并部署在CPU桌面平臺(tái)上,搭建一個(gè)簡單的人臉識(shí)別系統(tǒng)
先展示一下效果
- 因?yàn)椴幌肼赌標(biāo)允褂脭z像頭拍攝手機(jī)播放的視頻來大概演示一下效果
- 請自行忽略這個(gè)極簡GUI,
準(zhǔn)確說是沒有GUI - 測試平臺(tái)為Windows10,CPU為I5-6500
- 可以看到效果還是不錯(cuò)的
部署方案
- PaddleHub本身其實(shí)包含一個(gè)可直接調(diào)用的預(yù)測端口,但是使用起來不夠靈活
- 所以本次使用的大概方案是:
- 將兩個(gè)模型導(dǎo)出為推理模型
- 使用PaddleInference重寫預(yù)測端口,優(yōu)化預(yù)測效率
- 串聯(lián)兩個(gè)模型,實(shí)現(xiàn)人臉識(shí)別
- 使用預(yù)測結(jié)果實(shí)現(xiàn)一些小功能
導(dǎo)出推理模型
- 對于人臉檢測模型這類PaddleHub的預(yù)置模型來講,導(dǎo)出推理模型是非常簡單的,通過直接調(diào)用模型的save_inference_model函數(shù)即可一鍵導(dǎo)出推理模型
- 對于人臉驗(yàn)證模型這類經(jīng)過Finetune的模型來講,導(dǎo)出推理模型需要先搭建配置模型,然后加載訓(xùn)練結(jié)束的模型參數(shù),然后再調(diào)用save_inference_model函數(shù)即可導(dǎo)出推理模型
In [ ]
# 安裝新版PaddleHub
!pip install paddlehub==1.8.1
In [ ]
# 人臉檢測模型導(dǎo)出
import paddlehub as hub
# 加載模型
face_detector = hub.Module(name="pyramidbox_lite_mobile")
# 導(dǎo)出推理模型
face_detector.save_inference_model(
dirname='inference/face_detection',
model_filename='__model__',
params_filename='__params__')
In [ ]
# 人臉驗(yàn)證模型導(dǎo)出
import paddlehub as hub
from paddlehub.dataset.base_cv_dataset import BaseCVDataset
# 新建臨時(shí)train.txt
with open('data/train.txt', 'w', encoding='UTF-8') as f:
for x in range(100):
f.write('test 0\\n')
import paddlehub as hub
from paddlehub.dataset.base_cv_dataset import BaseCVDataset
# 自定義數(shù)據(jù)集
class FaceDataset(BaseCVDataset):
def __init__(self):
# 數(shù)據(jù)集存放位置
self.dataset_dir = "/home/aistudio/data/"
super(FaceDataset, self).__init__(
base_path=self.dataset_dir,
train_list_file="train.txt",
label_list=['0','1']
)
dataset = FaceDataset()
# 使用mobilenet_v3_large_imagenet_ssld預(yù)訓(xùn)練模型進(jìn)行finetune
module = hub.Module(name="mobilenet_v3_large_imagenet_ssld")
# 數(shù)據(jù)讀取器
data_reader = hub.reader.ImageClassificationReader(
image_width=module.get_expected_image_width(),
image_height=module.get_expected_image_height(),
images_mean=module.get_pretrained_images_mean(),
images_std=module.get_pretrained_images_std(),
dataset=dataset)
# 優(yōu)化器配置
strategy = hub.AdamWeightDecayStrategy(
learning_rate=1e-3,
lr_scheduler="linear_decay",
warmup_proportion=0.1,
weight_decay=0.0001,
optimizer_name="adam")
# 總體配置
config = hub.RunConfig(
use_cuda=False,
num_epoch=10,
checkpoint_dir="mobilenet_v3",
batch_size=100,
eval_interval=100,
strategy=strategy)
# 任務(wù)構(gòu)建
input_dict, output_dict, program = module.context(trainable=True)
img = input_dict["image"]
feature_map = output_dict["feature_map"]
feed_list = [img.name]
task = hub.ImageClassifierTask(
data_reader=data_reader,
feed_list=feed_list,
feature=feature_map,
num_classes=dataset.num_labels,
config=config)
# 加載best_model
task.init_if_load_best_model()
# 導(dǎo)出推理模型
task.save_inference_model(
dirname='inference/face_verification',
model_filename='__model__',
params_filename='__params__')
使用推理模型進(jìn)行預(yù)測
- 使用推理模型部署一般分為如下5步:
- 讀取數(shù)據(jù)
- 數(shù)據(jù)預(yù)處理
- 模型預(yù)測
- 結(jié)果后處理
- 結(jié)果展示
- 上述模型預(yù)測階段也分為5步:
- 配置推理選項(xiàng)
- 創(chuàng)建Predictor
- 準(zhǔn)備模型輸入
- 模型推理
- 獲取模型輸出
- 下面就來演示一下如何使用剛剛導(dǎo)出的推理模型完成人臉的檢測和驗(yàn)證
- 代碼上有詳細(xì)的注釋,更多更詳細(xì)的使用方法請參考[PaddleInference]
In [1]
# 檢測模型預(yù)測
%matplotlib inline
import cv2
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from paddle.fluid.core import AnalysisConfig, PaddleTensor
from paddle.fluid.core import create_paddle_predictor
# 數(shù)據(jù)預(yù)處理
def pre_det(org_im, shrink):
image = org_im.copy()
image_height, image_width, image_channel = image.shape
# 圖像縮放
if shrink != 1:
image_height, image_width = int(image_height * shrink), int(
image_width * shrink)
image = cv2.resize(image, (image_width, image_height),
cv2.INTER_NEAREST)
# HWC to CHW
if len(image.shape) == 3:
image = np.swapaxes(image, 1, 2)
image = np.swapaxes(image, 1, 0)
# 歸一化
mean = [104., 117., 123.]
scale = 0.007843
image = image.astype('float32')
image -= np.array(mean)[:, np.newaxis, np.newaxis].astype('float32')
image = image * scale
image = np.expand_dims(image, axis=0).astype('float32')
return image
# 數(shù)據(jù)后處理
# 輸入原始圖像,根據(jù)預(yù)測結(jié)果繪制人臉預(yù)測框,并裁剪人臉圖像
def post_det(img, output_datas):
img_h, img_w = img.shape[:2]
new_img = img.copy()
crops = []
for data in output_datas:
label, score, x1, y1, x2, y2 = data
if score>0.8:
x1, y1, x2, y2 = [int(_) for _ in [x1*img_w, y1*img_h, x2*img_w, y2*img_h]]
crop = img[max(0, y1-50):min(y2+50,img_h),max(0, x1-50):min(x2+50,img_w),:]
h, w = crop.shape[:2]
crop = cv2.resize(crop, (200, int(h/w*200))) if w>h else cv2.resize(crop, (int(w/h*200), 200))
row_nums = 200-crop.shape[0]
line_nums = 200-crop.shape[1]
if row_nums%2 ==0:
crop= np.pad(crop,((row_nums//2,row_nums//2),(0,0),(0,0)),'constant')
else:
crop= np.pad(crop,((row_nums//2,row_nums//2+1),(0,0),(0,0)),'constant')
if line_nums%2 ==0:
crop= np.pad(crop,((0,0),(line_nums//2,line_nums//2),(0,0)),'constant')
else:
crop= np.pad(crop,((0,0),(line_nums//2,line_nums//2+1),(0,0)),'constant')
crops.append(crop)
cv2.rectangle(new_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
return new_img, crops
# 創(chuàng)建預(yù)測器
def create_predictor(model_file, params_file):
# 創(chuàng)建配置
config = AnalysisConfig(model_file, params_file)
# 關(guān)閉GPU
config.disable_gpu()
# 開啟mkldnn加速intel平臺(tái)的CPU推理速度
config.enable_mkldnn()
# 關(guān)閉log顯示
config.disable_glog_info()
# 開啟ir優(yōu)化
config.switch_ir_optim(True)
# 使用feed和fetch的算子
config.switch_use_feed_fetch_ops(True)
# 根據(jù)配置創(chuàng)建預(yù)測器
predictor = create_paddle_predictor(config)
return predictor
# 模型預(yù)測
def predict_det(predictor, inputs):
# 轉(zhuǎn)換輸入數(shù)據(jù)為PaddleTensor
inputs = PaddleTensor(inputs.copy())
# 執(zhí)行前向計(jì)算
result = predictor.run([inputs])
# 轉(zhuǎn)換輸出數(shù)據(jù)為ndarray
output_data = result[0].as_ndarray()
return output_data
# 實(shí)例化檢測模型預(yù)測器
predictor = create_predictor('inference/face_detection/__model__', 'inference/face_detection/__params__')
# 讀取圖片
img = cv2.imread('img/test.jpg')
# 原始圖片展示
plt.imshow(img[:,:,::-1])
plt.show()
# 圖像預(yù)處理
img1 = pre_det(img, 0.5)
# 模型預(yù)測
output_data = predict_det(predictor, img1)
# 結(jié)果后處理
img, crops = post_det(img, output_data)
# 結(jié)果圖片展示
plt.imshow(img[:,:,::-1])
plt.show()
with 1 Axes>
with 1 Axes>
In [3]
# 驗(yàn)證模型預(yù)測
%matplotlib inline
import cv2
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from paddle.fluid.core import AnalysisConfig
from paddle.fluid.core import create_paddle_predictor
# 圖片拼接
def concatenate(true_img, crop):
new = np.concatenate([true_img,crop],1)
return new
# 數(shù)據(jù)預(yù)處理
def pre_val(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
# 圖像縮放
image = img.resize((224, 224), Image.LANCZOS)
# HWC to CHW
mean = np.array([0.485,0.456,0.406]).reshape(3, 1, 1)
std = np.array([0.229,0.224,0.225]).reshape(3, 1, 1)
image = np.array(image).astype('float32')
if len(image.shape) == 3:
image = np.swapaxes(image, 1, 2)
image = np.swapaxes(image, 1, 0)
# 歸一化
image /= 255
image -= mean
image /= std
image = image[[0, 1, 2], :, :]
image = np.expand_dims(image, axis=0).astype('float32')
return image
# 創(chuàng)建預(yù)測器
def create_predictor(model_file, params_file):
# 創(chuàng)建配置
config = AnalysisConfig(model_file, params_file)
# 關(guān)閉GPU
config.disable_gpu()
# 開啟mkldnn加速intel平臺(tái)的CPU推理速度
config.enable_mkldnn()
# 關(guān)閉log顯示
config.disable_glog_info()
# 開啟ir優(yōu)化
config.switch_ir_optim(True)
# 不使用feed和fetch的算子
config.switch_use_feed_fetch_ops(False)
# 根據(jù)配置創(chuàng)建預(yù)測器
predictor = create_paddle_predictor(config)
return predictor
# 模型預(yù)測
def predict_val(predictor, inputs):
# 獲取輸入向量名
input_names = predictor.get_input_names()
# 根據(jù)輸入向量名獲取輸入向量
input_tensor = predictor.get_input_tensor(input_names[0])
# 將輸入數(shù)據(jù)拷貝進(jìn)輸入向量
input_tensor.copy_from_cpu(inputs)
# 執(zhí)行前向計(jì)算
predictor.zero_copy_run()
# 獲取輸出向量名
output_names = predictor.get_output_names()
# 根據(jù)輸出向量名獲取輸出向量
output_tensor = predictor.get_output_tensor(output_names[0])
# 從輸出向量中拷貝輸出數(shù)據(jù)到輸出變量上
output_data = output_tensor.copy_to_cpu()
return output_data
# 實(shí)例化檢測模型預(yù)測器
predictor = create_predictor('inference/face_verification/__model__', 'inference/face_verification/__params__')
# 讀取圖片
img1 = cv2.imread('img/crop_0.jpg')
img2 = cv2.imread('img/crop_1.jpg')
# 圖像拼接
img_true = concatenate(img1, img1)
img_false = concatenate(img1, img2)
# 輸入圖片展示
plt.imshow(img_true[:,:,::-1])
plt.show()
plt.imshow(img_false[:,:,::-1])
plt.show()
# 數(shù)據(jù)預(yù)處理
img_true = pre_val(img_true)
img_false = pre_val(img_false)
# 數(shù)據(jù)拼接
imgs = np.concatenate([img_true, img_false], 0)
# 模型預(yù)測
output_data = predict_val(predictor, imgs)
# 結(jié)果后處理
results = np.argmax(output_data, 1)
for i, result in enumerate(results):
if result:
print('第%d個(gè)樣本匹配' % (i+1))
else:
print('第%d個(gè)樣本不匹配' % (i+1))
with 1 Axes>
with 1 Axes>
第1個(gè)樣本匹配
第2個(gè)樣本不匹配
完整程序
- 只需要將上面的兩個(gè)代碼稍微封裝,串聯(lián)起來,就能實(shí)現(xiàn)一個(gè)簡單的實(shí)時(shí)人臉識(shí)別系統(tǒng)
- 完整程序存放在face_recognition目錄下,目錄結(jié)構(gòu)如下:
- inference -- 存放推理模型
- preprocess.py -- 數(shù)據(jù)預(yù)處理
- postprocess.py -- 數(shù)據(jù)后處理
- inference.py -- 模型推理
- main.py -- 主程序
- 僅作為測試使用,未封裝GUI界面
- 使用下面的代碼即可啟動(dòng)程序
- 將按鈕窗口關(guān)閉,并使用Esc鍵退出視頻窗口,即可退出程序
In [ ]
# 請下載代碼并在有攝像頭的系統(tǒng)環(huán)境中執(zhí)行
!python face_recognition/main.py
程序流程
- 通過main.py來介紹一下大致的程序流程
- 具體細(xì)節(jié)請參考源碼
# 導(dǎo)入所需的包
import cv2, threading
import numpy as np
from inference import AnalysisModel
from preprocess import pre_det, pre_val
from postprocess import post_det
from tkinter import Tk, Button
# 按鈕點(diǎn)擊函數(shù),用于切換人臉
def change_face():
global change_flag
change_flag = True
# 主線程
def main():
global change_flag
# 開啟攝像頭
cap = cv2.VideoCapture(0)
# 初始化兩個(gè)模型
model_det = AnalysisModel('inference/face_detection/__model__',
'inference/face_detection/__params__',
True,
False)
model_val = AnalysisModel('inference/face_verification/__model__',
'inference/face_verification/__params__',
False,
True)
tmp = None
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
# 讀取當(dāng)前幀
sucess, img = cap.read()
# 檢測數(shù)據(jù)預(yù)處理
img_det = pre_det(img, 0.3)
# 檢測模型預(yù)測
result_det = model_det.predict_det(img_det)
# 檢測結(jié)果后處理
img, crops, bboxes = post_det(img, result_det)
# 如果當(dāng)前人臉信息不為空,則啟動(dòng)人臉驗(yàn)證
if type(tmp) is np.ndarray:
for crop, bbox in zip(crops, bboxes):
# 驗(yàn)證數(shù)據(jù)預(yù)處理
img_val = pre_val(tmp, crop)
x1, y1 = bbox[:2]
# 驗(yàn)證模型預(yù)測
result_val = model_val.predict_val(img_val)
# 驗(yàn)證結(jié)果后處理
if np.argmax(result_val[0]):
img = cv2.putText(img, 'Success', (x1, y1-4), font, 0.6, (0, 255, 0), 2)
else:
img = cv2.putText(img, 'Faild', (x1, y1-4), font, 0.6, (0, 0, 255), 2)
# 若更換人臉的標(biāo)識(shí)為真,則切換人臉信息
if (len(crops)>0) and change_flag:
tmp = crops[0]
crop = crops[0]
cv2.imshow('Face', crop)
change_flag=False
# 使用窗口顯示結(jié)果圖片
cv2.imshow('Face recognition', img)
k = cv2.waitKey(1)
if k == 27:
#通過esc鍵退出攝像
cv2.destroyAllWindows()
break
if __name__=='__main__':
global change_flag
change_flag = False
# 初始化按鈕界面
root = Tk()
root.title('Button')
button = Button(root, text ="點(diǎn)擊抓取人臉圖片", command = change_face)
button.pack()
# 初始化主線程
main_thread = threading.Thread(target=main)
# 啟動(dòng)主線程
main_thread.start()
# 啟動(dòng)按鈕界面線程
root.mainloop()
總結(jié)
- 這個(gè)人臉識(shí)別系統(tǒng)實(shí)測可用,效果也還能夠接受
- 如果項(xiàng)目有任何錯(cuò)誤的地方,歡迎大家在評論區(qū)中評論指正
關(guān)于作者
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報(bào)投訴
-
人臉識(shí)別技術(shù)
+關(guān)注
關(guān)注
0文章
127瀏覽量
14554 -
人臉識(shí)別
+關(guān)注
關(guān)注
76文章
4021瀏覽量
82369
發(fā)布評論請先 登錄
相關(guān)推薦
人臉識(shí)別技術(shù)的優(yōu)勢與劣勢
與其他生物識(shí)別技術(shù)(如指紋識(shí)別)相比,人臉識(shí)別是非接觸式的,這意味著用戶不需要與設(shè)備直接接觸,這在衛(wèi)生和便捷性方面是一
人臉識(shí)別技術(shù)的算法原理解析
基于人的面部特征,通過計(jì)算機(jī)算法來識(shí)別或驗(yàn)證個(gè)人身份。這項(xiàng)技術(shù)通常包括以下幾個(gè)步驟:人臉檢測、特征提取、特征比對和身份確認(rèn)。 2. 人臉檢測
人臉識(shí)別技術(shù)的應(yīng)用場景
領(lǐng)域,人臉識(shí)別技術(shù)被用來監(jiān)控和預(yù)防犯罪。通過在公共場所安裝攝像頭,結(jié)合人臉識(shí)別系統(tǒng),可以實(shí)時(shí)識(shí)別
新唐科技基于NuMaker-M55M1平臺(tái)的人臉識(shí)別系統(tǒng)
人臉識(shí)別技術(shù)已成為現(xiàn)代科技的重要基石,廣泛應(yīng)用于安全監(jiān)控、門禁系統(tǒng)以及用戶身份認(rèn)證等領(lǐng)域。新唐科技基于 NuMaker-M55M1 平臺(tái)的人臉
幾款性價(jià)比很不錯(cuò)的門禁人臉識(shí)別系統(tǒng)及其常見應(yīng)用
在當(dāng)今智能化時(shí)代,門禁系統(tǒng)作為安全管理的重要一環(huán),正逐步向更高效、更便捷的人臉識(shí)別技術(shù)轉(zhuǎn)型。其中,遠(yuǎn)景達(dá)物聯(lián)網(wǎng)推出的F3與F6門禁
![幾款性價(jià)比很不錯(cuò)的門禁<b class='flag-5'>人臉</b><b class='flag-5'>識(shí)別系統(tǒng)</b>及其常見應(yīng)用](https://file1.elecfans.com/web3/M00/04/BE/wKgZO2d3iFiAW8BmAADfO3unFxI673.png)
基于OpenCV的人臉識(shí)別系統(tǒng)設(shè)計(jì)
基于OpenCV的人臉識(shí)別系統(tǒng)是一個(gè)復(fù)雜但功能強(qiáng)大的系統(tǒng),廣泛應(yīng)用于安全監(jiān)控、人機(jī)交互、智能家居等多個(gè)領(lǐng)域。下面將詳細(xì)介紹基于OpenCV的
人臉識(shí)別技術(shù)的可行性在于矛盾具有什么性
人臉識(shí)別技術(shù)的可行性在于矛盾具有普遍性。 一、引言 人臉識(shí)別
人臉識(shí)別技術(shù)的優(yōu)缺點(diǎn)有哪些
人臉識(shí)別技術(shù)是一種基于人臉特征信息進(jìn)行身份識(shí)別的生物識(shí)別技術(shù)
人臉識(shí)別技術(shù)的原理介紹
人臉識(shí)別技術(shù)是一種基于人臉特征信息進(jìn)行身份識(shí)別的生物識(shí)別技術(shù)
如何設(shè)計(jì)人臉識(shí)別的神經(jīng)網(wǎng)絡(luò)
人臉識(shí)別技術(shù)是一種基于人臉特征信息進(jìn)行身份識(shí)別的技術(shù)
人臉識(shí)別模型訓(xùn)練流程
人臉識(shí)別模型訓(xùn)練流程是計(jì)算機(jī)視覺領(lǐng)域中的一項(xiàng)重要技術(shù)。本文將詳細(xì)介紹人臉識(shí)別模型的訓(xùn)練流程,包括
人臉識(shí)別模型訓(xùn)練是什么意思
人臉識(shí)別模型訓(xùn)練是指通過大量的人臉數(shù)據(jù),使用機(jī)器學(xué)習(xí)或深度學(xué)習(xí)算法,訓(xùn)練出一個(gè)能夠識(shí)別和分類
人臉檢測和人臉識(shí)別的區(qū)別是什么
人臉檢測和人臉識(shí)別是計(jì)算機(jī)視覺領(lǐng)域的兩個(gè)重要技術(shù),它們在許多應(yīng)用場景中都有廣泛的應(yīng)用,如安全監(jiān)控、身份驗(yàn)證、社交媒體等。盡管它們在某些方面有
人臉識(shí)別技術(shù)的原理是什么 人臉識(shí)別技術(shù)的特點(diǎn)有哪些
人臉識(shí)別技術(shù)的原理 人臉識(shí)別技術(shù)是一種通過計(jì)算機(jī)以圖
評論