其他分享
首页 > 其他分享> > 是否带眼镜检测

是否带眼镜检测

作者:互联网

第一个版本:

数据样本,带眼镜的图片2000

                 不带眼镜的2000张

 

开始训练模型

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 10 15:39:22 2019

@author: 01
"""


import cv2
import glob
import os
import tensorflow as tf
import numpy as np
import time
tf.reset_default_graph()
#数据集地址
path='C:/Users/01/Desktop/face/data/images/img_small/'
#模型保存地址
model_path='F:/1/model.ckpt'

#将所有的图片resize成100*100
w=100
h=100
c=3


#读取图片
def read_img(path):
    cate=[path+x for x in os.listdir(path) if os.path.isdir(path+x)]
    imgs=[]
    labels=[]
    for idx,folder in enumerate(cate):
        for im in glob.glob(folder+'/*.jpg'):
            print('reading the images:%s'%(im))
            img=cv2.imread(im)
            img=cv2.resize(img,(w,h))
            imgs.append(img)
            labels.append(idx)
    return np.asarray(imgs,np.float32),np.asarray(labels,np.int32)
data,label=read_img(path)


#打乱顺序
num_example=data.shape[0]
arr=np.arange(num_example)
np.random.shuffle(arr)
data=data[arr]
label=label[arr]


#将所有数据分为训练集和验证集
ratio=0.8
s=np.int(num_example*ratio)
x_train=data[:s]
y_train=label[:s]
x_val=data[s:]
y_val=label[s:]

#-----------------构建网络----------------------
#占位符
x=tf.placeholder(tf.float32,shape=[None,w,h,c],name='x')
y_=tf.placeholder(tf.int32,shape=[None,],name='y_')

def inference(input_tensor, train, regularizer):
    with tf.variable_scope('layer1-conv1'):
        conv1_weights = tf.get_variable("weight",[5,5,3,32],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv1_biases = tf.get_variable("bias", [32], initializer=tf.constant_initializer(0.0))
        conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))

    with tf.name_scope("layer2-pool1"):
        pool1 = tf.nn.max_pool(relu1, ksize = [1,2,2,1],strides=[1,2,2,1],padding="VALID")

    with tf.variable_scope("layer3-conv2"):
        conv2_weights = tf.get_variable("weight",[5,5,32,64],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv2_biases = tf.get_variable("bias", [64], initializer=tf.constant_initializer(0.0))
        conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))

    with tf.name_scope("layer4-pool2"):
        pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

    with tf.variable_scope("layer5-conv3"):
        conv3_weights = tf.get_variable("weight",[3,3,64,128],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv3_biases = tf.get_variable("bias", [128], initializer=tf.constant_initializer(0.0))
        conv3 = tf.nn.conv2d(pool2, conv3_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu3 = tf.nn.relu(tf.nn.bias_add(conv3, conv3_biases))

    with tf.name_scope("layer6-pool3"):
        pool3 = tf.nn.max_pool(relu3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

    with tf.variable_scope("layer7-conv4"):
        conv4_weights = tf.get_variable("weight",[3,3,128,128],initializer=tf.truncated_normal_initializer(stddev=0.1))
        conv4_biases = tf.get_variable("bias", [128], initializer=tf.constant_initializer(0.0))
        conv4 = tf.nn.conv2d(pool3, conv4_weights, strides=[1, 1, 1, 1], padding='SAME')
        relu4 = tf.nn.relu(tf.nn.bias_add(conv4, conv4_biases))

    with tf.name_scope("layer8-pool4"):
        pool4 = tf.nn.max_pool(relu4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
        nodes = 6*6*128
        reshaped = tf.reshape(pool4,[-1,nodes])

    with tf.variable_scope('layer9-fc1'):
        fc1_weights = tf.get_variable("weight", [nodes, 1024],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None: tf.add_to_collection('losses', regularizer(fc1_weights))
        fc1_biases = tf.get_variable("bias", [1024], initializer=tf.constant_initializer(0.1))

        fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_weights) + fc1_biases)
        if train: fc1 = tf.nn.dropout(fc1, 0.5)

    with tf.variable_scope('layer10-fc2'):
        fc2_weights = tf.get_variable("weight", [1024, 512],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None: tf.add_to_collection('losses', regularizer(fc2_weights))
        fc2_biases = tf.get_variable("bias", [512], initializer=tf.constant_initializer(0.1))

        fc2 = tf.nn.relu(tf.matmul(fc1, fc2_weights) + fc2_biases)
        if train: fc2 = tf.nn.dropout(fc2, 0.5)

    with tf.variable_scope('layer11-fc3'):
        fc3_weights = tf.get_variable("weight", [512, 2],
                                      initializer=tf.truncated_normal_initializer(stddev=0.1))
        if regularizer != None: tf.add_to_collection('losses', regularizer(fc3_weights))
        fc3_biases = tf.get_variable("bias", [2], initializer=tf.constant_initializer(0.1))
        logit = tf.matmul(fc2, fc3_weights) + fc3_biases

    return logit

#---------------------------网络结束---------------------------
regularizer = tf.contrib.layers.l2_regularizer(0.0001)
logits = inference(x,False,regularizer)

#(小处理)将logits乘以1赋值给logits_eval,定义name,方便在后续调用模型时通过tensor名字调用输出tensor
b = tf.constant(value=1,dtype=tf.float32)
logits_eval = tf.multiply(logits,b,name='logits_eval') 

loss=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y_)
train_op=tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
correct_prediction = tf.equal(tf.cast(tf.argmax(logits,1),tf.int32), y_)    
acc= tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


#定义一个函数,按批次取数据
def minibatches(inputs=None, targets=None, batch_size=None, shuffle=False):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batch_size]
        else:
            excerpt = slice(start_idx, start_idx + batch_size)
        yield inputs[excerpt], targets[excerpt]


#训练和测试数据,可将n_epoch设置更大一些

n_epoch=10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
batch_size=64
saver=tf.train.Saver()
sess=tf.Session()  
sess.run(tf.global_variables_initializer())
for epoch in range(n_epoch):
    start_time = time.time()

    #training
    train_loss, train_acc, n_batch = 0, 0, 0
    for x_train_a, y_train_a in minibatches(x_train, y_train, batch_size, shuffle=True):
        _,err,ac=sess.run([train_op,loss,acc], feed_dict={x: x_train_a, y_: y_train_a})
        train_loss += err; train_acc += ac; n_batch += 1
    print("   train loss: %f" % (np.sum(train_loss)/ n_batch))
    print("   train acc: %f" % (np.sum(train_acc)/ n_batch))

    #validation
    val_loss, val_acc, n_batch = 0, 0, 0
    for x_val_a, y_val_a in minibatches(x_val, y_val, batch_size, shuffle=False):
        err, ac = sess.run([loss,acc], feed_dict={x: x_val_a, y_: y_val_a})
        val_loss += err; val_acc += ac; n_batch += 1
    print("   validation loss: %f" % (np.sum(val_loss)/ n_batch))
    print("   validation acc: %f" % (np.sum(val_acc)/ n_batch))
saver.save(sess,model_path)
sess.close()

 

 

 

 

大概10次后,训练准确率达到99%,验证准确率80%左右,

测试模型

# -*- coding: utf-8 -*-

import cv2
import tensorflow as tf
import numpy as np


path1 = "./1.jpg"
path2 = "./2.jpg"
path3 = "./3.jpg"
path4 = "./4.jpg"
path5 = "./face5.jpg"
path6 = "./face6.jpg"
path7 = "./face7.jpg"
path8 = "./face8.jpg"
face_dict = {1:'Has Glass',0:'No Glass'}

w=100
h=100
c=3

def read_one_image(path):
    img = cv2.imread(path)
    print(img)
    img = cv2.resize(img,(w,h))
    return np.asarray(img)

with tf.Session() as sess:
    data = []
    data1 = read_one_image(path1)
    data2 = read_one_image(path2)
    data3 = read_one_image(path3)
    data4 = read_one_image(path4)
    data5 = read_one_image(path5)
    data6 = read_one_image(path6)
    data7 = read_one_image(path7)
    data8 = read_one_image(path8)
    data.append(data1)
    data.append(data2)
    data.append(data3)
    data.append(data4)
    data.append(data5)
    data.append(data6)
    data.append(data7)
    data.append(data8)
    saver = tf.train.import_meta_graph('./models/model.ckpt.meta')
    saver.restore(sess,tf.train.latest_checkpoint('./models/'))

    graph = tf.get_default_graph()
    x = graph.get_tensor_by_name("x:0")
    feed_dict = {x:data}
    logits = graph.get_tensor_by_name("logits_eval:0")

    classification_result = sess.run(logits,feed_dict)

    #打印出预测矩阵
    print(classification_result)
    #打印出预测矩阵每一行最大值的索引
    #print(tf.argmax(classification_result,1).eval())
    #根据索引通过字典对应人脸的分类
    output = []
    output = tf.argmax(classification_result,1).eval()
    for i in range(len(output)):
        print("No.",i+1,"face is belong to:"+face_dict[output[i]])

如果想要c++调用模型,需要把ckpt的模型文件转换为pb的形式,代码如下

# -*- coding: utf-8 -*-
"""
Created on Mon Jul 15 08:48:53 2019

@author: 01
"""

import cv2
import tensorflow as tf
import numpy as np

def freeze_graph(input_checkpoint,output_graph):
    '''
    :param input_checkpoint:
    :param output_graph: PB模型保存路径
    :return:
    '''
    # checkpoint = tf.train.get_checkpoint_state(model_folder) #检查目录下ckpt文件状态是否可用
    # input_checkpoint = checkpoint.model_checkpoint_path #得ckpt文件路径
    # 指定输出的节点名称,该节点名称必须是原模型中存在的节点
    output_node_names = "logits_eval"
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=True)
    graph = tf.get_default_graph() # 获得默认的图
    input_graph_def = graph.as_graph_def()  # 返回一个序列化的图代表当前的图
    with tf.Session() as sess:
        saver.restore(sess, input_checkpoint) #恢复图并得到数据
        output_graph_def = tf.graph_util.convert_variables_to_constants(  # 模型持久化,将变量值固定
            sess=sess,
            input_graph_def=input_graph_def,# 等于:sess.graph_def
            output_node_names=output_node_names.split(","))# 如果有多个输出节点,以逗号隔开
        with tf.gfile.GFile(output_graph, "wb") as f: #保存模型
            f.write(output_graph_def.SerializeToString()) #序列化输出
        print("%d ops in the final graph." % len(output_graph_def.node)) #得到当前图有几个操作节点
        # for op in graph.get_operations():
        #     print(op.name, op.values())

    # 输入ckpt模型路径
input_checkpoint='C:/Users/01/Desktop/face/CNN_Face_Glass_Classfy-master/model/model.ckpt'
    # 输出pb模型的路径
out_pb_path="models/pb/frozen_model.pb"
    # 调用freeze_graph将ckpt转为pb

freeze_graph(input_checkpoint,out_pb_path)

然后就是c++调用pb文件。

 

 

#include<opencv2\opencv.hpp>
#include<opencv2\dnn.hpp>
#include<stdlib.h>
#include<iostream>
#include <Windows.h>
#include<string.h> 
#include <cstdlib>
#include "lib\facedetect-dll.h"
#include "lib\facedetect-dll.h"
using namespace cv;
using namespace std;
using namespace dnn;
String labels_txt_file = "F:/123.txt";
#define DETECT_BUFFER_SIZE 0x20000

int Glasses_ide(const char* path)
{
	cout << "==================眼镜检测开始==========================" << endl;
	//=======对图像进行头像裁剪================
	//const char* path = "E:/fangtu1/CNN_Face_Glass_Classfy-master/face9.jpg";
	//const char* path = "E:/fangtu1/1.png";

	//face_dec(path);
	//Pr_Fo(path);
	//(path);
	//=======对图像进行头像裁剪================
	unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
	Mat src = imread(path);//如果预测结果出错,请修改一下图片路径,为什么?不知道	
	//检测人脸位置
	Mat gray;
	cvtColor(src, gray, CV_BGR2GRAY);


	auto pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1);
	Mat m_roi_1(7, 7, CV_32FC2, Scalar(1, 3));
	for (int i = 0; i < (pResults ? *pResults : 0); i++)
	{
		//cout << "==================眼镜检测开始==========================" << endl;
		short * p = ((short*)(pResults + 1)) + 142 * i;
		int x = p[0];
		int y = p[1];
		int w = p[2];
		int h = p[3];
		int neighbors = p[4];
		int angle = p[5];
		//const char *saveFilePath = "F:\\00res.jpg";
		//printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x, y, w, h, neighbors, angle);
		//cv::rectangle(src, Rect(x-w/2, y, w, h), Scalar(0, 255, 0), 2);

		//cv::rectangle(src, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
		//cv::rectangle(src, Rect(x + w + w / 16, y + h / 3, w / 5, h / 5), Scalar(255, 0, 0), 2);
		//cv::rectangle(src, Rect(x + w + w / 16, y + 4 * h / 3, w / 5, h / 5), Scalar(0, 0, 225), 2);
		//cout << "hehe" << x << y << w << h << endl;

		// save roi image  
		//设定感兴趣的区域ROI
		Mat m_roi_1 = src(cv::Rect(x, y, w, h));
		//imshow("Image Classfic11ation1", m_roi_1);
		imwrite("hear.jpg", m_roi_1);
	}
	Mat src1 = imread("hear.jpg");
	//检测人脸位置
	if (src1.empty())//判断是否为空,判断图片是否导入
	{
		cout << "error:no img" << endl;
	}
	String weights = "F:/output/frozen_model.pb";
	Net net = readNetFromTensorflow(weights);
	DWORD timestart = GetTickCount(); //定义事件节点,测试运行效率
	if (net.empty())//判断模型是否调起。
	{
		cout << "error :no model" << endl;
	}
	vector <String> labels = readClassNames();  //读取分类类别

	Mat rgb;
	int ww = 100;//图像的长和宽
	int hh = 100;
	resize(src1, src, Size(ww, hh));
	cvtColor(src, rgb, COLOR_BGR2RGB);
	//cout << "src" << src<< endl;
	Mat inputBlob = blobFromImage(src, 0.00390625f, Size(ww, hh), Scalar(), false, false);//false如果用caffe训练的要改成true,tensordlow的不用改变
	Mat prob;

	net.setInput(inputBlob, "x");//x是输入的名字,必须和tensorflow定义的名字一致。
	prob = net.forward("logits_eval");//logits_eval是输出的名字,必须和tensorflow定义的名字一致。
	DWORD timeend = GetTickCount();
	Mat probMat = prob.reshape(1, 1);
	//cout << "pro" << probMat << endl;
	Point classNumber;

	double classProb;
	minMaxLoc(probMat, NULL, &classProb, NULL, &classNumber);
	int classidx = classNumber.x;
	printf("\n current image classification : %s, possible : %.2f\n", labels.at(classidx).c_str(), classProb);
	putText(src, labels.at(classidx), Point(20, 20), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 255), 2, 8);
	cout << "用时(毫秒):" << timeend - timestart << endl;
	// 显示文本	
	cout << "==================眼镜检测结束==========================" << endl;
	cout << endl;
	cout << endl;
	imshow("Image Classfication", src);
	system("del hear.jpg");
	waitKey(1);
	//free(pBuffer);
	system("pause");
	return 0;
}
vector <String>readClassNames()
{
	vector <String>classNames;
	fstream fp(labels_txt_file);
	if (!fp.is_open())
	{
		cout << "does not open" << endl;
		exit(-1);
	}
	string name;
	while (!fp.eof())
	{
		getline(fp, name);
		if (name.length())
			classNames.push_back(name);
	}
	fp.close();
	return classNames;
}

虽然可以使用但是准确度不高,改进版本

 

1:定位人脸,把人脸裁剪出来作为训练数据。代码如下

# created at 2018-01-22
# updated at 2018-09-29
# Author:   coneypo
# Blog:     http://www.cnblogs.com/AdaminXie
# GitHub:   https://github.com/coneypo/Dlib_face_cut
import dlib         # 人脸识别的库dlib
import numpy as np  # 数据处理的库numpy
import cv2          # 图像处理的库OpenCv
import os
# 读取图像的路径
path_read = "./data/images/faces_for_test/1/1/"
# 用来存储生成的单张人脸的路径
path_save = "./data/images/3/"
# Delete old images
def clear_images():
    imgs = os.listdir(path_save)
    for img in imgs:
        os.remove(path_save + img)
    print("clean finish", '\n')
clear_images()
# Dlib 预测器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('data/dlib/shape_predictor_68_face_landmarks.dat')
# Dlib 检测

jj=1
for i in os.listdir(path_read):
    path_1read=path_read+i
    print(path_1read)
    img = cv2.imread(path_1read)
    a=img.shape
    jj=jj+1
    faces = detector(img, 1)
    print("人脸数:", len(faces), '\n')
    for k, d in enumerate(faces):
    # 计算矩形大小
    # (x,y), (宽度width, 高度height)
       pos_start = tuple([d.left(), d.top()])
       
    # 计算矩形框大小
       height = d.bottom()-d.top()
       if a[1]>=d.right():
          pos_end = tuple([d.right(), d.bottom()])
          width = d.right()-d.left()
       else:
          pos_end = tuple([a[1], d.bottom()])
          width =a[1]-d.left()
    # 根据人脸大小生成空的图像
       try:
          img_blank = np.zeros((height, width, 3), np.uint8)
          for i in range(height):
             for j in range(width):
                img_blank[i][j] = img[d.top()+i][d.left()+j]
    # cv2.imshow("face_"+str(k+1), img_blank)
    # 存在本地
          print("Save to:", path_save+"img_face_"+str(k+1)+".jpg")
          cv2.imwrite(path_save+"img_face3_"+str(k+1)+"_"+str(jj)+".jpg", img_blank)      
       except:
            print("发生异常")
       else:
            print("没有异常")
          
          

再次训练,测试后准确率达到90%左右,还是达不到要求。

 

再次改进

直接定位到眼镜区域,把其作为训练样本代码如下

 

# created at 2018-01-22
# updated at 2018-09-29
# Author:   coneypo
# Blog:     http://www.cnblogs.com/AdaminXie
# GitHub:   https://github.com/coneypo/Dlib_face_cut
import dlib         # 人脸识别的库dlib
import numpy as np  # 数据处理的库numpy
import cv2          # 图像处理的库OpenCv
import os
# 读取图像的路径
path_read = "./data/images/faces_for_test/1/1/"
# 用来存储生成的单张人脸的路径
path_save = "./data/images/img_small/0/"
# Delete old images
def clear_images():
    imgs = os.listdir(path_save)
    for img in imgs:
        os.remove(path_save + img)
    print("clean finish", '\n')
clear_images()
# Dlib 预测器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('data/dlib/shape_predictor_68_face_landmarks.dat')
# Dlib 检测

jj=1
for i in os.listdir(path_read):
    path_1read=path_read+i
    print(path_1read)
    img = cv2.imread(path_1read)
    a=img.shape
    jj=jj+1
    faces = detector(img, 1)
    print("人脸数:", len(faces), '\n')
    for k, d in enumerate(faces):
    # 计算矩形大小
    # (x,y), (宽度width, 高度height)
       pos_start = tuple([d.left(), d.top()])
       
    # 计算矩形框大小
       height = d.bottom()-d.top()
       if a[1]>=d.right():
          pos_end = tuple([d.right(), d.bottom()])
          width = d.right()-d.left()
       else:
          pos_end = tuple([a[1], d.bottom()])
          width =a[1]-d.left()
    # 根据人脸大小生成空的图像
       try:
          b=height/2
          
          img_blank = np.zeros((int ((4*b)/5), width, 3), np.uint8)
          for i in range(int((4*b)/5)):
             for j in range(width):
                img_blank[i][j] = img[d.top()+int((b)/5)+i][d.left()+j]
    # cv2.imshow("face_"+str(k+1), img_blank)
    # 存在本地
          print("Save to:", path_save+"img_face_"+str(k+1)+".jpg")
          cv2.imwrite(path_save+"img_face3_"+str(k+1)+"_"+str(jj)+".jpg", img_blank)      
       except:
            print("发生异常")
       else:
            print("没有异常")
          
          

再次测试,准确度达到99%左右,nice

标签:img,检测,眼镜,initializer,train,graph,是否,tf,path
来源: https://blog.csdn.net/zhouguangfei0717/article/details/97921162