当前位置: 首页 > news >正文

htmlcss项目实战源码_基于TensorFlow深度学习人脸识别源码级项目实战

45440973fa1685ba798a5de1d803ad28.png

人脸年龄识别属于人脸属性识别的范畴,人脸属性识别可对图片中的人脸进行检测定位,并识别出人脸的相关属性(如年龄、性别、表情、种族、颜值等)内容。不同属性识别的算法可以相同,也可以不同。rude-carnie是做年龄识别和性别识别的一个开源项目,基于TensorFlow,源代码网址:http://www.github.com/dpressel/rude-carnie。下面我们基于这个项目源码来讲年龄识别。

1.年龄识别核心代码guess.py

我们可以把年龄划分为几个段['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)'],然后基于分类的思想来做年龄预测问题。用下面的脚本命令:

python3 guess.py --model_type inception --model_dir /home/hadoop/chongdianleme/ nianling/22801/inception/22801 --filename /home/hadoop/chongdianleme/data/myimages/baidu1.jpg

基于训练好的年龄模型和人脸图片就能预测出年龄。但是有一个问题,直接这样预测不是很准,因为图片没有经过任何处理。我们可以通过opencv和上面讲到的FaceNet的人脸检测和对齐算法来做。但opencv比较简单,FaceNet的人脸检测和对齐效果比较好,我们可以使用前面提供的http服务接口http://172.17.100.216:8816/detectAndAlignedService来处理,然后把检测和对齐后的人脸图片传给guess.py,这样预测处理的效果精准很多。

另外一个问题是,因为训练的模型用的是开源项目训练好的,是拿外国人的人脸数据训练,这样用来预测我们中国人的年龄会有一些差异,最好的方式是拿我们中国人自己的人脸年龄数据做训练,这样预测才会更好。

针对年龄识别和性别识别都是用guess.py这个文件,年龄识别是多分类,性别是二分类。我们看下guess.py的源码,如代码8.5所示:

【代码8.5】 guess.py

from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionfrom datetime import datetimeimport mathimport timefrom data import inputsimport numpy as npimport tensorflow as tffrom model import select_model, get_checkpointfrom utils import *import osimport jsonimport csv
RESIZE_FINAL = 227#性别有两个
GENDER_LIST =['M','F']#年龄是分段的,可以看成多分类任务
AGE_LIST = ['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)']
MAX_BATCH_SZ = 128#模型文件目录
tf.app.flags.DEFINE_string('model_dir', '','Model directory (where training data lives)')#性别和年龄都是用的这个,通过参数age|gender来区分
tf.app.flags.DEFINE_string('class_type', 'age','Classification type (age|gender)')#用cpu还是用GPU来训练
tf.app.flags.DEFINE_string('device_id', '/cpu:0','What processing unit to execute inference on')
tf.app.flags.DEFINE_string('filename', '','File (Image) or File list (Text/No header TSV) to process')
tf.app.flags.DEFINE_string('target', '','CSV file containing the filename processed along with best guess and score')#检查点
tf.app.flags.DEFINE_string('checkpoint', 'checkpoint','Checkpoint basename')
tf.app.flags.DEFINE_string('model_type', 'default','Type of convnet')
tf.app.flags.DEFINE_string('requested_step', '', 'Within the model directory, a requested step to restore e.g., 9000')
tf.app.flags.DEFINE_boolean('single_look', False, 'single look at the image or multiple crops')
tf.app.flags.DEFINE_string('face_detection_model', '', 'Do frontal face detection with model specified')
tf.app.flags.DEFINE_string('face_detection_type', 'cascade', 'Face detection model type (yolo_tiny|cascade)')
FLAGS = tf.app.flags.FLAGSdef one_of(fname, types):return any([fname.endswith('.' + ty) for ty in types])def resolve_file(fname):if os.path.exists(fname): return fnamefor suffix in ('.jpg', '.png', '.JPG', '.PNG', '.jpeg'):
cand = fname + suffixif os.path.exists(cand):return candreturn Nonedef classify_many_single_crop(sess, label_list, softmax_output, coder, images, image_files, writer):try:
num_batches = math.ceil(len(image_files) / MAX_BATCH_SZ)
pg = ProgressBar(num_batches)for j in range(num_batches):
start_offset = j * MAX_BATCH_SZ
end_offset = min((j + 1) * MAX_BATCH_SZ, len(image_files))
batch_image_files = image_files[start_offset:end_offset]
print(start_offset, end_offset, len(batch_image_files))
image_batch = make_multi_image_batch(batch_image_files, coder)
batch_results = sess.run(softmax_output, feed_dict={images:image_batch.eval()})
batch_sz = batch_results.shape[0]for i in range(batch_sz):
output_i = batch_results[i]
best_i = np.argmax(output_i)
best_choice = (label_list[best_i], output_i[best_i])
print('Guess @ 1 %s, prob = %.2f' % best_choice)if writer is not None:
f = batch_image_files[i]
writer.writerow((f, best_choice[0], '%.2f' % best_choice[1]))
pg.update()
pg.done()except Exception as e:
print(e)
print('Failed to run all images')def classify_one_multi_crop(sess, label_list, softmax_output, coder, images, image_file, writer):try:
print('Running file %s' % image_file)
image_batch = make_multi_crop_batch(image_file, coder)
batch_results = sess.run(softmax_output, feed_dict={images:image_batch.eval()})
output = batch_results[0]
batch_sz = batch_results.shape[0]for i in range(1, batch_sz):
output = output + batch_results[i]
output /= batch_sz
best = np.argmax(output)
best_choice = (label_list[best], output[best])
print('Guess @ 1 %s, prob = %.2f' % best_choice)
nlabels = len(label_list)if nlabels > 2:
output[best] = 0
second_best = np.argmax(output)
print('Guess @ 2 %s, prob = %.2f' % (label_list[second_best], output[second_best]))if writer is not None:
writer.writerow((image_file, best_choice[0], '%.2f' % best_choice[1]))except Exception as e:
print(e)
print('Failed to run image %s ' % image_file)def list_images(srcfile):with open(srcfile, 'r') as csvfile:
delim = ',' if srcfile.endswith('.csv') else 't'
reader = csv.reader(csvfile, delimiter=delim)if srcfile.endswith('.csv') or srcfile.endswith('.tsv'):
print('skipping header')
_ = next(reader)return [row[0] for row in reader]def main(argv=None): # pylint: disable=unused-argument
files = []
print("target %s" % FLAGS.target)if FLAGS.face_detection_model:
print('Using face detector (%s) %s' % (FLAGS.face_detection_type, FLAGS.face_detection_model))
face_detect = face_detection_model(FLAGS.face_detection_type, FLAGS.face_detection_model)
face_files, rectangles = face_detect.run(FLAGS.filename)
print(face_files)
files += face_files
config = tf.ConfigProto(allow_soft_placement=True)with tf.Session(config=config) as sess:
label_list = AGE_LIST if FLAGS.class_type == 'age' else GENDER_LIST
nlabels = len(label_list)
print('Executing on %s' % FLAGS.device_id)
model_fn = select_model(FLAGS.model_type)with tf.device(FLAGS.device_id):
images = tf.placeholder(tf.float32, [None, RESIZE_FINAL, RESIZE_FINAL, 3])
logits = model_fn(nlabels, images, 1, False)
init = tf.global_variables_initializer()
requested_step = FLAGS.requested_step if FLAGS.requested_step else None
checkpoint_path = '%s' % (FLAGS.model_dir)
model_checkpoint_path, global_step = get_checkpoint(checkpoint_path, requested_step, FLAGS.checkpoint)
saver = tf.train.Saver()
saver.restore(sess, model_checkpoint_path)
softmax_output = tf.nn.softmax(logits)
coder = ImageCoder()# Support a batch mode if no face detection modelif len(files) == 0:if (os.path.isdir(FLAGS.filename)):for relpath in os.listdir(FLAGS.filename):
abspath = os.path.join(FLAGS.filename, relpath)if os.path.isfile(abspath) and any([abspath.endswith('.' + ty) for ty in ('jpg', 'png', 'JPG', 'PNG', 'jpeg')]):
print(abspath)
files.append(abspath)else:
files.append(FLAGS.filename)# If it happens to be a list file, read the list and clobber the filesif any([FLAGS.filename.endswith('.' + ty) for ty in ('csv', 'tsv', 'txt')]):
files = list_images(FLAGS.filename)
writer = None
output = None if FLAGS.target:
print('Creating output file %s' % FLAGS.target)
output = open(FLAGS.target, 'w')
writer = csv.writer(output)
writer.writerow(('file', 'label', 'score'))
image_files = list(filter(lambda x: x is not None, [resolve_file(f) for f in files]))
print(image_files)if FLAGS.single_look:
classify_many_single_crop(sess, label_list, softmax_output, coder, images, image_files, writer)else:for image_file in image_files:
classify_one_multi_crop(sess, label_list, softmax_output, coder, images, image_file, writer)if output is not None:
output.close()if __name__ == '__main__':
tf.app.run()

2.年龄识别Web工程化代码

年龄识别我们对外提供一个Web接口,guessAgeWeb_chongdianleme.py如代码8.6所示:

【代码8.6】 guessAgeWeb_chongdianleme.py

from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionfrom datetime import datetimeimport mathimport timefrom data import inputsimport numpy as npimport tensorflow as tffrom model import select_model, get_checkpointfrom utils import *import osimport csv#pip3 install flaskfrom flask import Flaskfrom flask import requestimport urllib# pip3 install requestsimport requests, urllib.requestfrom scipy import miscimport argparseimport facenetimport align.detect_faceimport json
RESIZE_FINAL = 227#性别有两个
GENDER_LIST = ['M', 'F']#年龄是分段的,可以看成多分类任务
AGE_LIST = ['(0, 2)', '(4, 6)', '(8, 12)', '(15, 20)', '(25, 32)', '(38, 43)', '(48, 53)', '(60, 100)']
MAX_BATCH_SZ = 128def one_of(fname, types):return any([fname.endswith('.' + ty) for ty in types])def resolve_file(fname):if os.path.exists(fname): return fnamefor suffix in ('.jpg', '.png', '.JPG', '.PNG', '.jpeg'):
cand = fname + suffixif os.path.exists(cand):return candreturn Nonedef list_images(srcfile):with open(srcfile, 'r') as csvfile:
delim = ',' if srcfile.endswith('.csv') else 't'
reader = csv.reader(csvfile, delimiter=delim)if srcfile.endswith('.csv') or srcfile.endswith('.tsv'):
print('skipping header')
_ = next(reader)return [row[0] for row in reader]# 初始化
class_type = 'age'
device_id = "/cpu:0"
model_type = "inception"
requested_step = ""#模型文件目录
model_dir = "/home/hadoop/chongdianleme/nianling/22801/inception/22801"
checkpoint = "checkpoint"
config = tf.ConfigProto(allow_soft_placement=True)
sess = tf.Session(config=config)with sess.as_default():
label_list = AGE_LIST if class_type == 'age' else GENDER_LIST
nlabels = len(label_list)
model_fn = select_model(model_type)
images = tf.placeholder(tf.float32, [None, RESIZE_FINAL, RESIZE_FINAL, 3])
logits = model_fn(nlabels, images, 1, False)
init = tf.global_variables_initializer()
requested_step = requested_step if requested_step else None
checkpoint_path = '%s' % (model_dir)
model_checkpoint_path, global_step = get_checkpoint(checkpoint_path, requested_step, checkpoint)
saver = tf.train.Saver()
saver.restore(sess, model_checkpoint_path)
softmax_output = tf.nn.softmax(logits)
coder = ImageCoder()def _is_png(filename):return '.png' in filename
app = Flask(__name__)
@app.route('/predictAge', methods=['GET', 'POST'])def prediction():
start = time.time()
imageUrl = request.values.get("imageUrl")#用户信息
device = request.values.get("device")
userid = request.values.get("userid")
urlType = request.values.get("urlType")
imageType = request.values.get("imageType")
files = []#支持本地图片和网络图片if urlType=="local":
filename = imageUrlelse:
baseImageName = os.path.basename(imageUrl)
filename = "/home/hadoop/chongdianleme/ageimage/%s" % baseImageName
filename = filename+imageType
urllib.request.urlretrieve(imageUrl, filename)#通过我们前面讲的FaceNet里的人脸检测和对齐的http接口对图片进行处理,这样识别的年龄更精准
url = "http://172.17.100.216:8816/detectAndAlignedService"
body_value = {"image_file": filename, "alignedImage_files":filename}
data_urlencode = urllib.parse.urlencode(body_value).encode(encoding='UTF8')
request2 = urllib.request.Request(url, data_urlencode)#调用接口
resultJson = urllib.request.urlopen(request2).read().decode('UTF-8')#解析json格式的数据
o = json.loads(resultJson)
ts = o["times"]
i = o["i"]
newFileName = filename.replace(".","_0_.")
files.append(newFileName)
image_files = list(filter(lambda x: x is not None, [resolve_file(f) for f in files]))
print(image_files)
finalAge = 0
avgBest = 0.00
bestProb = 0.00
avgSecond = 0.00
secondProb = 0.00for image_file in image_files:try:
print('Running file %s' % image_file)#image_batch = make_multi_crop_batch(image_file, coder) #startwith tf.gfile.FastGFile(filename, 'rb') as f:
image_data = f.read()# Convert any PNG to JPEG's for consistency.if _is_png(filename):
print('Converting PNG to JPEG for %s' % filename)
image_data = coder.png_to_jpeg(image_data)
image = coder.decode_jpeg(image_data)
crops = []
print('Running multi-cropped image')
h = image.shape[0]
w = image.shape[1]
hl = h - RESIZE_FINAL
wl = w - RESIZE_FINAL
crop = tf.image.resize_images(image, (RESIZE_FINAL, RESIZE_FINAL))
crops.append(standardize_image(crop))
crops.append(tf.image.flip_left_right(crop))
corners = [(0, 0), (0, wl), (hl, 0), (hl, wl), (int(hl / 2), int(wl / 2))]for corner in corners:
ch, cw = corner
cropped = tf.image.crop_to_bounding_box(image, ch, cw, RESIZE_FINAL, RESIZE_FINAL)
crops.append(standardize_image(cropped))
flipped = tf.image.flip_left_right(cropped)
crops.append(standardize_image(flipped))
image_batch = tf.stack(crops)#end
batch_results = sess.run(softmax_output, feed_dict={images:image_batch.eval(session=sess)})
output = batch_results[0]
batch_sz = batch_results.shape[0]for i in range(1, batch_sz):
output = output + batch_results[i]
output /= batch_sz
best = np.argmax(output)
ageClass = label_list[best] # (25, 32)
bestAgeArr = ageClass.replace(" ", "").replace("(", "").replace(")", "").split(",")#AGE_LIST = ['(0, 2)', '(4, 6)', '(8, 12)', '(15, 20)', '(25, 32)', '(38, 43)', '(48, 53)', '(60, 100)'] #因为训练的模型用的是开源项目训练好的,是拿外国人的人脸数据训练, # 这样用来预测我们中国人的年龄会有一些差异,最好的方式是拿我们中国人自己的人脸年龄数据做训练,这样预测才会更好。 # 所以针对外国人训练数据预测我们中国人需要对年龄做一些经验上的特殊处理if int(bestAgeArr[1]) == 53:
avgBest= 56elif int(bestAgeArr[1]) == 43:
avgBest = 45elif int(bestAgeArr[1]) == 20:
avgBest = 22.66elif int(bestAgeArr[1]) == 6:
avgBest = 6.66else:
avgBest = (int(bestAgeArr[0]) + int(bestAgeArr[1])) / 1.66
bestProb = output[best]
best_choice = (label_list[best], output[best])
print('Guess @ 1 %s, prob = %.2f' % best_choice)
nlabels = len(label_list)if nlabels > 2:
output[best] = 0
second_best = np.argmax(output)
secondAgeClass = label_list[second_best] # (25, 32)
secondAgeArr = secondAgeClass.replace(" ", "").replace("(", "").replace(")", "").split(",")if int(secondAgeArr[1])== 53:
avgSecond = 56elif int(secondAgeArr[1])== 43:
avgSecond = 45elif int(secondAgeArr[1])== 20:
avgSecond = 22.66elif int(secondAgeArr[1])== 6:
avgSecond = 6.66else:
avgSecond = (int(secondAgeArr[0]) + int(secondAgeArr[1])) / 1.66
secondProb = output[second_best]
print('Guess @ 2 %s, prob = %.2f' % (label_list[second_best], output[second_best]))except Exception as e:import traceback
traceback.print_exc()
print('Failed to run image %s ' % image_file)if avgSecond > 0:#基于加权平均法计算最终的合适的年龄
finalAge = (avgBest * bestProb + avgSecond * secondProb) / (bestProb + secondProb)else:
finalAge = avgBest
print("finalAge %s " % finalAge)
end = time.time()
times = str(end - start)#返回年龄等json格式数据
result = {"finalAge": finalAge,"times": times}
out = json.dumps(result, ensure_ascii=False)
print("out={0}".format(out))return outif __name__ == '__main__':#指定ip地址和端口号
app.run(host='172.17.100.216', port=8818)

然后我们看下怎么部署和启动基于Flask的年龄识别服务,脚本代码如下所示:

#创建shell脚本文件vim guessAgeService.sh

#输入:

python3 guessAgeWeb_chongdianleme.py

#然后:wq保存

#对guessAgeService.sh脚本授权可执行权限

sudo chmod 755 guessAgeService.sh

#然后再创建一个以后台方式运行的shell脚本:

vim nohupguessAgeService.sh

#输入:

nohup /home/hadoop/chongdianleme/guessAgeService.sh > guessAge.log 2>&1 &

#然后:wq保存

#同样对nohupguessAgeService.sh脚本授权可执行权限

sudo chmod 755 nohupguessAgeService.sh

#最后运行sh nohupguessAgeService.sh脚本启动基于flask的人脸识别比对服务接口。

启动完成后,就可以在浏览器地址里输入url访问我们的服务了。这个http接口声明了同时支持get和post访问,我们在浏览器里输入地址就可以直接访问了。

http://172.17.100.216:8818/predictAge?imageUrl=/home/hadoop/chongdianleme/age/luhan2.jpg&urlType=local&imageType=jpg

这个就是一个接口服务,其他系统或者php、java web网站都可以调用这个接口,输入要预测imageUrl人脸图片路径,urlType是同时支持本地图片和网络图片链接的设置,返回人脸年龄json格式数据。

知乎视频​www.zhihu.com
zhihu-card-default.svg

除了基于TensorFlow深度学习人脸识别源码级项目实战☞https://ke.qq.com/course/2554709?flowToken=1028991

其它深度学习框架也有不错的开源实现,比如MXNet,后面请大家关注充电了么app,课程,微信群,更多内容请看新书《分布式机器学习实战(人工智能科学与技术丛书)》

《分布式机器学习实战》本书对应清华大学出版社京东自营链接地址:

https://item.jd.com/12743009.html

Python编程零基础小白快速入门必听课

https://ke.qq.com/course/package/29782?flowToken=1028733

相关文章:

  • com口测试好坏_用modscan测试modbus协议离散仪表数据方法
  • mysql 统计连续重复值_mysql统计某列值连续出现次数小于五次的记录
  • mysql视图代码_MySQL视图简介与操作的介绍(附代码)
  • mysql密码存储过程_mysql设置用户名密码,存储过程,触发器
  • sqlserver mysql 乱码_SqlServer数据库中文乱码问题解决
  • oracle与mysql跨库连接_PostgreSQL+Oracle跨库连接实操
  • php mysql 域名空间_域名空间pipni.cz申请和使用攻略(MYSQL+PHP+CGI+mail+FTP)
  • mysql条件删除表中某些行数据_根据MySQL中的条件仅删除表中的某些行
  • mysql pdo rowcount_PDO rowCount()在MySQL上有效,但在SQL Server 2008 R...
  • mysql+数据库索引策略_mysql数据库索引
  • 企业组织架构mysql_[MySQL] 数据目录的组织架构
  • matlab一维搜索_工程优化设计与Matlab实现——一维搜索方法(黄金分割法)
  • mysql源码索引_Mysql中的索引
  • python3.5如何安装pip管理工具_python3.5.2安装pip管理工具
  • vue watch监听对象的属性_手把手教你深入Vue中对比computed和watch属性的区别
  • Docker 笔记(1):介绍、镜像、容器及其基本操作
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • js 实现textarea输入字数提示
  • JS笔记四:作用域、变量(函数)提升
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • PHP变量
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • unity如何实现一个固定宽度的orthagraphic相机
  • webpack4 一点通
  • 从输入URL到页面加载发生了什么
  • 第十八天-企业应用架构模式-基本模式
  • 关于Java中分层中遇到的一些问题
  • 线上 python http server profile 实践
  • 详解移动APP与web APP的区别
  • 一个项目push到多个远程Git仓库
  • ${factoryList }后面有空格不影响
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (C语言)fread与fwrite详解
  • (九)c52学习之旅-定时器
  • (没学懂,待填坑)【动态规划】数位动态规划
  • (七)Java对象在Hibernate持久化层的状态
  • (已更新)关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
  • (转)Linux下编译安装log4cxx
  • .net core使用RPC方式进行高效的HTTP服务访问
  • .NET 回调、接口回调、 委托
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • [ 环境搭建篇 ] 安装 java 环境并配置环境变量(附 JDK1.8 安装包)
  • [1525]字符统计2 (哈希)SDUT
  • [2021ICPC济南 L] Strange Series (Bell 数 多项式exp)
  • [⑧ADRV902x]: Digital Pre-Distortion (DPD)学习笔记
  • [AS3]URLLoader+URLRequest+JPGEncoder实现BitmapData图片数据保存
  • [bzoj1912]异象石(set)
  • [C++]类和对象【下】
  • [Docker]十二.Docker consul集群搭建、微服务部署,Consul集群+Swarm集群部署微服务实战
  • [Docker]十一.Docker Swarm集群raft算法,Docker Swarm Web管理工具
  • [JavaWeb学习] tomcat简介、安装及项目部署
  • [LeetCode] Minimum Path Sum
  • [LeetCode]—Copy List with Random Pointer 深度复制带“任意指针”的链表
  • [office] excel如何计算毛重和皮重的时间间隔 excel计算毛重和皮重时间间隔方法 #笔记#学习方法