Python基于keras訓(xùn)練實(shí)現(xiàn)微笑識(shí)別的示例詳解
一、數(shù)據(jù)預(yù)處理
實(shí)驗(yàn)數(shù)據(jù)來自genki4k
提取含有完整人臉的圖片
def init_file(): ? ? num = 0 ? ? bar = tqdm(os.listdir(read_path)) ? ? for file_name in bar: ? ? ? ? bar.desc = "預(yù)處理圖片: " ? ? ? ? # a圖片的全路徑 ? ? ? ? img_path = (read_path + "/" + file_name) ? ? ? ? # 讀入的圖片的路徑中含非英文 ? ? ? ? img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED) ? ? ? ? # 獲取圖片的寬高 ? ? ? ? img_shape = img.shape ? ? ? ? img_height = img_shape[0] ? ? ? ? img_width = img_shape[1] ? ? ? ? # 用來存儲(chǔ)生成的單張人臉的路徑 ? ? ? ? # dlib檢測(cè) ? ? ? ? dets = detector(img, 1) ? ? ? ? for k, d in enumerate(dets): ? ? ? ? ? ? if len(dets) > 1: ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? num += 1 ? ? ? ? ? ? # 計(jì)算矩形大小 ? ? ? ? ? ? # (x,y), (寬度width, 高度height) ? ? ? ? ? ? # pos_start = tuple([d.left(), d.top()]) ? ? ? ? ? ? # pos_end = tuple([d.right(), d.bottom()]) ? ? ? ? ? ? # 計(jì)算矩形框大小 ? ? ? ? ? ? height = d.bottom() - d.top() ? ? ? ? ? ? width = d.right() - d.left() ? ? ? ? ? ? # 根據(jù)人臉大小生成空的圖像 ? ? ? ? ? ? img_blank = np.zeros((height, width, 3), np.uint8) ? ? ? ? ? ? for i in range(height): ? ? ? ? ? ? ? ? if d.top() + i >= img_height: ?# 防止越界 ? ? ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? ? ? for j in range(width): ? ? ? ? ? ? ? ? ? ? if d.left() + j >= img_width: ?# 防止越界 ? ? ? ? ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? ? ? ? ? img_blank[i][j] = img[d.top() + i][d.left() + j] ? ? ? ? ? ? img_blank = cv2.resize(img_blank, (200, 200), interpolation=cv2.INTER_CUBIC) ? ? ? ? ? ? # 保存圖片 ? ? ? ? ? ? cv2.imencode('.jpg', img_blank)[1].tofile(save_path + "/" + "file" + str(num) + ".jpg") ? ? logging.info("一共", len(os.listdir(read_path)), "個(gè)樣本") ? ? logging.info("有效樣本", num)
二、訓(xùn)練模型
創(chuàng)建模型
# 創(chuàng)建網(wǎng)絡(luò) def create_model(): model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(128, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Flatten()) model.add(layers.Dropout(0.5)) model.add(layers.Dense(512, activation='relu')) model.add(layers.Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc']) return model
訓(xùn)練模型
# 訓(xùn)練模型 def train_model(model): ? ? # 歸一化處理 ? ? train_datagen = ImageDataGenerator( ? ? ? ? rescale=1. / 255, ? ? ? ? rotation_range=40, ? ? ? ? width_shift_range=0.2, ? ? ? ? height_shift_range=0.2, ? ? ? ? shear_range=0.2, ? ? ? ? zoom_range=0.2, ? ? ? ? horizontal_flip=True, ) ? ? test_datagen = ImageDataGenerator(rescale=1. / 255) ? ? train_generator = train_datagen.flow_from_directory( ? ? ? ? # This is the target directory ? ? ? ? train_dir, ? ? ? ? # All images will be resized to 150x150 ? ? ? ? target_size=(150, 150), ? ? ? ? batch_size=32, ? ? ? ? # Since we use binary_crossentropy loss, we need binary labels ? ? ? ? class_mode='binary') ? ? validation_generator = test_datagen.flow_from_directory( ? ? ? ? validation_dir, ? ? ? ? target_size=(150, 150), ? ? ? ? batch_size=32, ? ? ? ? class_mode='binary') ? ? history = model.fit_generator( ? ? ? ? train_generator, ? ? ? ? steps_per_epoch=60, ? ? ? ? epochs=12, ? ? ? ? validation_data=validation_generator, ? ? ? ? validation_steps=30) ? ? # 保存模型 ? ? save_path = "../output/model" ? ? if not os.path.exists(save_path): ? ? ? ? os.makedirs(save_path) ? ? model.save(save_path + "/smileDetect.h5") ? ? return history
訓(xùn)練結(jié)果
準(zhǔn)確率
丟失率
訓(xùn)練過程
三、預(yù)測(cè)
通過讀取攝像頭內(nèi)容進(jìn)行預(yù)測(cè)
def rec(img): ? ? gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ? ? dets = detector(gray, 1) ? ? if dets is not None: ? ? ? ? for face in dets: ? ? ? ? ? ? left = face.left() ? ? ? ? ? ? top = face.top() ? ? ? ? ? ? right = face.right() ? ? ? ? ? ? bottom = face.bottom() ? ? ? ? ? ? cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2) ? ? ? ? ? ? img1 = cv2.resize(img[top:bottom, left:right], dsize=(150, 150)) ? ? ? ? ? ? img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB) ? ? ? ? ? ? img1 = np.array(img1) / 255. ? ? ? ? ? ? img_tensor = img1.reshape(-1, 150, 150, 3) ? ? ? ? ? ? prediction = model.predict(img_tensor) ? ? ? ? ? ? if prediction[0][0] > 0.5: ? ? ? ? ? ? ? ? result = 'unsmile' ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? result = 'smile' ? ? ? ? ? ? cv2.putText(img, result, (left, top), font, 2, (0, 255, 0), 2, cv2.LINE_AA) ? ? ? ? cv2.imshow('Video', img) while video.isOpened(): ? ? res, img_rd = video.read() ? ? if not res: ? ? ? ? break ? ? rec(img_rd) ? ? if cv2.waitKey(1) & 0xFF == ord('q'): ? ? ? ? break
效果
四、源代碼
pretreatment.py
import dlib ?# 人臉識(shí)別的庫dlib import numpy as np ?# 數(shù)據(jù)處理的庫numpy import cv2 ?# 圖像處理的庫OpenCv import os import shutil from tqdm import tqdm import logging # dlib預(yù)測(cè)器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('../resources/shape_predictor_68_face_landmarks.dat') # 原圖片路徑 read_path = "../resources/genki4k/files" # 提取人臉存儲(chǔ)路徑 save_path = "../output/genki4k/files" if not os.path.exists(save_path): ? ? os.makedirs(save_path) # 新的數(shù)據(jù)集 data_dir = '../resources/data' if not os.path.exists(data_dir): ? ? os.makedirs(data_dir) # 訓(xùn)練集 train_dir = data_dir + "/train" if not os.path.exists(train_dir): ? ? os.makedirs(train_dir) # 驗(yàn)證集 validation_dir = os.path.join(data_dir, 'validation') if not os.path.exists(validation_dir): ? ? os.makedirs(validation_dir) # 測(cè)試集 test_dir = os.path.join(data_dir, 'test') if not os.path.exists(test_dir): ? ? os.makedirs(test_dir) # 初始化訓(xùn)練數(shù)據(jù) def init_data(file_list): ? ? # 如果不存在文件夾則新建 ? ? for file_path in file_list: ? ? ? ? if not os.path.exists(file_path): ? ? ? ? ? ? os.makedirs(file_path) ? ? ? ? # 存在則清空里面所有數(shù)據(jù) ? ? ? ? else: ? ? ? ? ? ? for i in os.listdir(file_path): ? ? ? ? ? ? ? ? path = os.path.join(file_path, i) ? ? ? ? ? ? ? ? if os.path.isfile(path): ? ? ? ? ? ? ? ? ? ? os.remove(path) def init_file(): ? ? num = 0 ? ? bar = tqdm(os.listdir(read_path)) ? ? for file_name in bar: ? ? ? ? bar.desc = "預(yù)處理圖片: " ? ? ? ? # a圖片的全路徑 ? ? ? ? img_path = (read_path + "/" + file_name) ? ? ? ? # 讀入的圖片的路徑中含非英文 ? ? ? ? img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED) ? ? ? ? # 獲取圖片的寬高 ? ? ? ? img_shape = img.shape ? ? ? ? img_height = img_shape[0] ? ? ? ? img_width = img_shape[1] ? ? ? ? # 用來存儲(chǔ)生成的單張人臉的路徑 ? ? ? ? # dlib檢測(cè) ? ? ? ? dets = detector(img, 1) ? ? ? ? for k, d in enumerate(dets): ? ? ? ? ? ? if len(dets) > 1: ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? num += 1 ? ? ? ? ? ? # 計(jì)算矩形大小 ? ? ? ? ? ? # (x,y), (寬度width, 高度height) ? ? ? ? ? ? # pos_start = tuple([d.left(), d.top()]) ? ? ? ? ? ? # pos_end = tuple([d.right(), d.bottom()]) ? ? ? ? ? ? # 計(jì)算矩形框大小 ? ? ? ? ? ? height = d.bottom() - d.top() ? ? ? ? ? ? width = d.right() - d.left() ? ? ? ? ? ? # 根據(jù)人臉大小生成空的圖像 ? ? ? ? ? ? img_blank = np.zeros((height, width, 3), np.uint8) ? ? ? ? ? ? for i in range(height): ? ? ? ? ? ? ? ? if d.top() + i >= img_height: ?# 防止越界 ? ? ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? ? ? for j in range(width): ? ? ? ? ? ? ? ? ? ? if d.left() + j >= img_width: ?# 防止越界 ? ? ? ? ? ? ? ? ? ? ? ? continue ? ? ? ? ? ? ? ? ? ? img_blank[i][j] = img[d.top() + i][d.left() + j] ? ? ? ? ? ? img_blank = cv2.resize(img_blank, (200, 200), interpolation=cv2.INTER_CUBIC) ? ? ? ? ? ? # 保存圖片 ? ? ? ? ? ? cv2.imencode('.jpg', img_blank)[1].tofile(save_path + "/" + "file" + str(num) + ".jpg") ? ? logging.info("一共", len(os.listdir(read_path)), "個(gè)樣本") ? ? logging.info("有效樣本", num) # 劃分?jǐn)?shù)據(jù)集 def divide_data(file_path, message, begin, end): ? ? files = ['file{}.jpg'.format(i) for i in range(begin, end)] ? ? bar = tqdm(files) ? ? bar.desc = message ? ? for file in bar: ? ? ? ? src = os.path.join(save_path, file) ? ? ? ? dst = os.path.join(file_path, file) ? ? ? ? shutil.copyfile(src, dst) if __name__ == "__main__": ? ? init_file() ? ? positive_train_dir = os.path.join(train_dir, 'smile') ? ? negative_train_dir = os.path.join(train_dir, 'unSmile') ? ? positive_validation_dir = os.path.join(validation_dir, 'smile') ? ? negative_validation_dir = os.path.join(validation_dir, 'unSmile') ? ? positive_test_dir = os.path.join(test_dir, 'smile') ? ? negative_test_dir = os.path.join(test_dir, 'unSmile') ? ? file_list = [positive_train_dir, positive_validation_dir, positive_test_dir, ? ? ? ? ? ? ? ? ?negative_train_dir, negative_validation_dir, negative_test_dir] ? ? init_data(file_list) ? ? divide_data(positive_train_dir, "劃分訓(xùn)練集正樣本", 1, 1001) ? ? divide_data(negative_train_dir, "劃分訓(xùn)練集負(fù)樣本", 2200, 3200) ? ? divide_data(positive_validation_dir, "劃分驗(yàn)證集正樣本", 1000, 1500) ? ? divide_data(negative_validation_dir, "劃分驗(yàn)證集負(fù)樣本", 3000, 3500) ? ? divide_data(positive_test_dir, "劃分測(cè)試集正樣本", 1500, 2000) ? ? divide_data(negative_test_dir, "劃分測(cè)試集負(fù)樣本", 2800, 3500)
train.py
import os from keras import layers from keras import models from tensorflow import optimizers import matplotlib.pyplot as plt from keras.preprocessing.image import ImageDataGenerator train_dir = "../resources/data/train" validation_dir = "../resources/data/validation" # 創(chuàng)建網(wǎng)絡(luò) def create_model(): ? ? model = models.Sequential() ? ? model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3))) ? ? model.add(layers.MaxPooling2D((2, 2))) ? ? model.add(layers.Conv2D(64, (3, 3), activation='relu')) ? ? model.add(layers.MaxPooling2D((2, 2))) ? ? model.add(layers.Conv2D(128, (3, 3), activation='relu')) ? ? model.add(layers.MaxPooling2D((2, 2))) ? ? model.add(layers.Conv2D(128, (3, 3), activation='relu')) ? ? model.add(layers.MaxPooling2D((2, 2))) ? ? model.add(layers.Flatten()) ? ? model.add(layers.Dropout(0.5)) ? ? model.add(layers.Dense(512, activation='relu')) ? ? model.add(layers.Dense(1, activation='sigmoid')) ? ? model.compile(loss='binary_crossentropy', ? ? ? ? ? ? ? ? ? optimizer=optimizers.RMSprop(lr=1e-4), ? ? ? ? ? ? ? ? ? metrics=['acc']) ? ? return model # 訓(xùn)練模型 def train_model(model): ? ? # 歸一化處理 ? ? train_datagen = ImageDataGenerator( ? ? ? ? rescale=1. / 255, ? ? ? ? rotation_range=40, ? ? ? ? width_shift_range=0.2, ? ? ? ? height_shift_range=0.2, ? ? ? ? shear_range=0.2, ? ? ? ? zoom_range=0.2, ? ? ? ? horizontal_flip=True, ) ? ? test_datagen = ImageDataGenerator(rescale=1. / 255) ? ? train_generator = train_datagen.flow_from_directory( ? ? ? ? # This is the target directory ? ? ? ? train_dir, ? ? ? ? # All images will be resized to 150x150 ? ? ? ? target_size=(150, 150), ? ? ? ? batch_size=32, ? ? ? ? # Since we use binary_crossentropy loss, we need binary labels ? ? ? ? class_mode='binary') ? ? validation_generator = test_datagen.flow_from_directory( ? ? ? ? validation_dir, ? ? ? ? target_size=(150, 150), ? ? ? ? batch_size=32, ? ? ? ? class_mode='binary') ? ? history = model.fit_generator( ? ? ? ? train_generator, ? ? ? ? steps_per_epoch=60, ? ? ? ? epochs=12, ? ? ? ? validation_data=validation_generator, ? ? ? ? validation_steps=30) ? ? # 保存模型 ? ? save_path = "../output/model" ? ? if not os.path.exists(save_path): ? ? ? ? os.makedirs(save_path) ? ? model.save(save_path + "/smileDetect.h5") ? ? return history # 展示訓(xùn)練結(jié)果 def show_results(history): ? ? # 數(shù)據(jù)增強(qiáng)過后的訓(xùn)練集與驗(yàn)證集的精確度與損失度的圖形 ? ? acc = history.history['acc'] ? ? val_acc = history.history['val_acc'] ? ? loss = history.history['loss'] ? ? val_loss = history.history['val_loss'] ? ? # 繪制結(jié)果 ? ? epochs = range(len(acc)) ? ? plt.plot(epochs, acc, 'bo', label='Training acc') ? ? plt.plot(epochs, val_acc, 'b', label='Validation acc') ? ? plt.title('Training and validation accuracy') ? ? plt.legend() ? ? plt.figure() ? ? plt.plot(epochs, loss, 'bo', label='Training loss') ? ? plt.plot(epochs, val_loss, 'b', label='Validation loss') ? ? plt.title('Training and validation loss') ? ? plt.legend() ? ? plt.show() if __name__ == "__main__": ? ? model = create_model() ? ? history = train_model(model) ? ? show_results(history)
predict.py
import os from keras import layers from keras import models from tensorflow import optimizers import matplotlib.pyplot as plt from keras.preprocessing.image import ImageDataGenerator train_dir = "../resources/data/train" validation_dir = "../resources/data/validation" # 創(chuàng)建網(wǎng)絡(luò) # 檢測(cè)視頻或者攝像頭中的人臉 import cv2 from keras.preprocessing import image from keras.models import load_model import numpy as np import dlib from PIL import Image model = load_model('../output/model/smileDetect.h5') detector = dlib.get_frontal_face_detector() video = cv2.VideoCapture(0) font = cv2.FONT_HERSHEY_SIMPLEX def rec(img): ? ? gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ? ? dets = detector(gray, 1) ? ? if dets is not None: ? ? ? ? for face in dets: ? ? ? ? ? ? left = face.left() ? ? ? ? ? ? top = face.top() ? ? ? ? ? ? right = face.right() ? ? ? ? ? ? bottom = face.bottom() ? ? ? ? ? ? cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2) ? ? ? ? ? ? img1 = cv2.resize(img[top:bottom, left:right], dsize=(150, 150)) ? ? ? ? ? ? img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB) ? ? ? ? ? ? img1 = np.array(img1) / 255. ? ? ? ? ? ? img_tensor = img1.reshape(-1, 150, 150, 3) ? ? ? ? ? ? prediction = model.predict(img_tensor) ? ? ? ? ? ? if prediction[0][0] > 0.5: ? ? ? ? ? ? ? ? result = 'unsmile' ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? result = 'smile' ? ? ? ? ? ? cv2.putText(img, result, (left, top), font, 2, (0, 255, 0), 2, cv2.LINE_AA) ? ? ? ? cv2.imshow('Video', img) while video.isOpened(): ? ? res, img_rd = video.read() ? ? if not res: ? ? ? ? break ? ? rec(img_rd) ? ? if cv2.waitKey(1) & 0xFF == ord('q'): ? ? ? ? break video.release() cv2.destroyAllWindows()
以上就是Python基于keras訓(xùn)練實(shí)現(xiàn)微笑識(shí)別的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Python keras微笑識(shí)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?pyecharts?Map地圖數(shù)據(jù)不顯示的原因及完美解決
這篇文章主要給大家介紹了關(guān)于Python?pyecharts?Map地圖數(shù)據(jù)不顯示的原因及解決辦法,pyecharts是一款將python與echarts結(jié)合的強(qiáng)大的數(shù)據(jù)可視化工具,文中通過圖文以及代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將名稱映射到序列元素中的方法
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將名稱映射到序列元素中的方法,結(jié)合實(shí)例形式分析了Python使用collections.namedtuple()進(jìn)行元組命名相關(guān)操作技巧,需要的朋友可以參考下2018-03-03詳解python?sklearn中的數(shù)據(jù)預(yù)處理方法
本篇文章主要講解Python的sklearn庫中常用的數(shù)據(jù)預(yù)處理方法,主要介紹工具中的內(nèi)容,即該庫中的相關(guān)方法包含的常用接口和基本使用,希望對(duì)大家有所幫助2023-08-08pycharm 實(shí)現(xiàn)顯示project 選項(xiàng)卡的方法
今天小編就為大家分享一篇pycharm 實(shí)現(xiàn)顯示project 選項(xiàng)卡的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01python實(shí)現(xiàn)新年倒計(jì)時(shí)實(shí)例代碼
大家好,本篇文章主要講的是python實(shí)現(xiàn)新年倒計(jì)時(shí)實(shí)例代碼,昂星期的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12