【人脸识别】人脸遮挡算法程序+数据集制作全过程

一.Keras实现人脸遮挡学习

在进行人脸识别前,人脸质量评估也是不可或缺的一步,主要分为三个关键部分:人脸姿态评估,人脸遮挡评估,人脸清晰度评估.人脸遮挡部分,参考了大神的方法,如下:
  https://www.jianshu.com/p/88986c27546a
  https://github.com/Oreobird/Face-Occlusion-Detect

该方法使用的是cofw人脸数据集(作者给出.mat格式数据集)上面根据左眼,右眼,鼻子,嘴巴,下巴五个位置添加随机色块的方法做人脸数据集,后发现胡须和眼镜容易误识别,遂又增加眼镜和胡须数据集.其中cofw数据集给出的标注格式如下:图片地址,人脸框坐标,29个点XY坐标+关键点是否遮挡(0遮挡1不遮挡).

./data/cofw/train/0.jpg,85.0 159.0 121.0 134.0 ,85.99722946649642 195.41106079612535 123.44891725116838 148.1552662611304 105.65446350014858 104.57601175768406 171.09687605660224 171.78316352862788 99.67227767846481 182.45479366749245 124.8675763275294 155.97385609797809 112.99690216245236 113.04442551768672 169.32712676583802 169.47840944529483 114.2325093893163 169.12198428387694 121.84452106323161 153.14763332876228 135.55312540008788 135.76900893320334 112.88463000725581 165.69423368395275 137.50187272686335 137.02519029553042 138.38577815069323 138.3871401002516 138.84203133510087 154.19324372870395 156.9874141526525 166.00719236280764 166.15425396395602 154.97757226887177 160.76199525092338 158.21292749626537 164.19343261389142 169.34796830067236 172.80054003260622 171.27108006129237 173.68421069713375 164.57794384495102 174.6346777947214 167.20994564975052 178.0009154327801 167.11014660418434 170.59519262961896 210.655601611006 213.57002930486962 213.46208753795693 221.34183648779592 223.97481870370993 229.11617921587566 228.6735455298915 231.49959137396021 238.24941460634867 245.35743063719343 270.11291126146864 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 

首先采用dlib人脸检测和关键点模型,按照上述方法进行训练,识别率不高,后发现其中prepare_data代码部分没有修改.

label = [1, 0, 0, 0, 0, 0]
if slot[8] or slot[10] or slot[12] or slot[13] or slot[16]:
		label[1] = 1  # left eye
		label[0] = 0
if slot[9] or slot[11] or slot[14] or slot[15] or slot[17]:
		label[2] = 1  # right eye
		label[0] = 0
if slot[18] or slot[19] or slot[20] or slot[21]:
		label[3] = 1  # nose
		label[0] = 0
if slot[22] or slot[23] or slot[25] or slot[26] or slot[27] or slot[24]:
		label[4] = 1  # mouth
		label[0] = 0
if slot[28]:
        label[5] = 1  # chin
		label[0] = 0

参考下图人脸关键点位置,进行标签设置.举例右眼,作者原代码只有17位置出现了遮挡才算遮挡,改为若9,11,13,14,17中任意一点遮挡都算遮挡了.效果依然不好,遂决定对该数据集进行一下标注.
在这里插入图片描述

二.人脸遮挡标注小程序

小程序效果图如下:
在这里插入图片描述
在这里插入图片描述
部分实现代码如下:

from PyQt5.QtWidgets import (QLabel, QWidget, QPushButton,
                             QFrame, QApplication)
from PyQt5.QtGui import (QColor, QPixmap, QImage)
import sys
import cv2
import numpy as np


class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()
        self.label_index = 0
        self.label = [1, 0, 0, 0, 0, 0]

    def initUI(self):

        lbl1 = QLabel('矩形框内有无遮挡??', self)
        lbl1.move(10, 10)
        self.setGeometry(300, 300, 1000, 1000)
        self.setWindowTitle('遮挡判断')
        self.yesb = QPushButton('yes', self)
        self.yesb.setCheckable(True)
        self.yesb.move(20, 50)
        self.yesb.clicked[bool].connect(self.set_label_yes)
        self.nob = QPushButton('no', self)
        self.nob.setCheckable(True)
        self.nob.move(20, 90)
        self.nob.clicked[bool].connect(self.set_label_no)

        self.imgl1 = QLabel(self)
        self.imgl1.setMinimumSize(800,800)
        self.imgl1.move(200, 20)

        self.show()

        data_dir = "/home/papa/Face-Occlusion-Detect/data/cofw/"
        self.new_file = open(data_dir + 'shangpapa1.txt', 'w+')

        with open(data_dir + 'test_ground_true.txt', "r") as file:
            self.file_lines = file.readlines()
            self.itr = iter(self.file_lines)
        self.line = next(self.itr)
        

    def starMark(self): 
        
        self.img_path, self.bbox, phis = self.line.split(',')
        phis = phis.strip('\n').strip(' ').split(' ')
        phis = [int(float(x)) for x in phis]
        print(self.img_path)

        img = cv2.imread(self.img_path)

        i = self.label_index
        if i == 0:  #左眼
            cv2.rectangle(img, (phis[8] - 8, phis[29 + 12] - 8), (phis[10] + 8, phis[29 + 13] + 6), (0, 255, 0), 2)
            cv2.putText(img, 'left_eye', (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1.2, (255, 255, 255), 2)
        if i == 1:  #右眼
            cv2.rectangle(img, (phis[11] - 8, phis[29 + 14] - 8), (phis[9] + 8, phis[29 + 15] + 6), (0, 255, 0), 2)
            cv2.putText(img, 'right_eye', (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1.2, (255, 255, 255), 2)
        if i == 2:  #鼻子
            cv2.rectangle(img, (phis[18] - 4, phis[29 + 20] - 20), (phis[19] + 5, phis[29 + 21] + 1), (0, 255, 0), 2)
            cv2.putText(img, 'nose', (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1.2, (255, 255, 255), 2)
        if i == 3:  #嘴
            cv2.rectangle(img, (phis[22] - 2, phis[29 + 24] - 1), (phis[23] + 2, phis[29 + 27] + 1), (0, 255, 0), 2)
            cv2.putText(img, 'mouth', (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1.2, (255, 255, 255), 2)
        if i == 4:  #下巴
            cv2.rectangle(img, (phis[28] - 25, phis[29 + 28] - 12), (phis[28] + 25, phis[29 + 28] + 15), (0, 255, 0), 2)
            cv2.putText(img, 'chic', (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1.2, (255, 255, 255), 2)

        height, width, channel = img.shape
        bytesPerLine = 3 * width
        qImg = QImage(img.data, width, height, bytesPerLine, QImage.Format_RGB888)

        pixmap = QPixmap.fromImage(qImg)
        self.imgl1.setPixmap(pixmap)

        # self.label_index += 1


    def set_label_yes(self, pressed):  #当 self.label_index == 4 的时候 就表示这张图标完了,就可以调到下一张图
        self.label[self.label_index+1] = 1
        self.label[0] = 0
        self.toNext()

    def set_label_no(self, pressed): 

        self.toNext()

    def toNext(self):
        if self.label_index == 4:
            try:
                lab_str = ''
                print(self.label)
                for x in self.label:
                    lab_str += str(x) + ' '
                content = self.img_path + ',' + self.bbox + ',' + lab_str + '\n'
                print(content)
                self.new_file.write(content)

                self.line = next(self.itr)
                self.label_index=0
                self.label = [1, 0, 0, 0, 0, 0]
            except StopIteration:
                return
        else:
            self.label_index+=1
        self.starMark()
    

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.starMark()
    sys.exit(app.exec_())



三.训练参数和最终效果

拜拜~
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/shangpapa3/article/details/102904231
今日推荐