python 实现性别识别


Posted in Python onNovember 21, 2020

使用keras实现性别识别,模型数据使用的是oarriaga/face_classification的模型

实现效果

python 实现性别识别

准备工作

在开始之前先要安装keras和tensorflow

安装keras使用命令:pip3 install keras

安装tensorflow使用命令:pip3 install tensorflow

编码部分

们使用OpenCV先识别到人脸,然后在通过keras识别性别,具体代码如下

#coding=utf-8
#性别识别

import cv2
from keras.models import load_model
import numpy as np
import ChineseText

img = cv2.imread("img/gather.png")
face_classifier = cv2.CascadeClassifier(
  "C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"
)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(
  gray, scaleFactor=1.2, minNeighbors=3, minSize=(140, 140))

gender_classifier = load_model(
  "classifier/gender_models/simple_CNN.81-0.96.hdf5")
gender_labels = {0: '女', 1: '男'}
color = (255, 255, 255)

for (x, y, w, h) in faces:
  face = img[(y - 60):(y + h + 60), (x - 30):(x + w + 30)]
  face = cv2.resize(face, (48, 48))
  face = np.expand_dims(face, 0)
  face = face / 255.0
  gender_label_arg = np.argmax(gender_classifier.predict(face))
  gender = gender_labels[gender_label_arg]
  cv2.rectangle(img, (x, y), (x + h, y + w), color, 2)
  img = ChineseText.cv2ImgAddText(img, gender, x + h, y, color, 30)

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

以上就是python 实现性别识别的详细内容,更多关于python 性别识别的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python实现simhash算法实例
Apr 25 Python
python中__call__方法示例分析
Oct 11 Python
理解Python中函数的参数
Apr 27 Python
python获得文件创建时间和修改时间的方法
Jun 30 Python
python 计算两个日期相差多少个月实例代码
May 24 Python
老生常谈python函数参数的区别(必看篇)
May 29 Python
python爬虫之BeautifulSoup 使用select方法详解
Oct 23 Python
scrapy-redis源码分析之发送POST请求详解
May 15 Python
python Django中models进行模糊查询的示例
Jul 18 Python
Python搭建代理IP池实现检测IP的方法
Oct 27 Python
Python 面向对象部分知识点小结
Mar 09 Python
Python+Selenium实现读取网易邮箱验证码
Mar 13 Python
python遍历路径破解表单的示例
Nov 21 #Python
Python如何批量生成和调用变量
Nov 21 #Python
在终端启动Python时报错的解决方案
Nov 20 #Python
python 批量下载bilibili视频的gui程序
Nov 20 #Python
Python ellipsis 的用法详解
Nov 20 #Python
python 动态渲染 mysql 配置文件的示例
Nov 20 #Python
Python paramiko使用方法代码汇总
Nov 20 #Python
You might like
frename PHP 灵活文件命名函数 frename
2009/09/09 PHP
php操作MongoDB类实例
2015/06/17 PHP
js 操作select与option(示例讲解)
2013/12/20 Javascript
js获取下拉列表的值和元素个数示例
2014/05/07 Javascript
JS的事件绑定深入认识
2014/06/26 Javascript
jQuery Mobile操作HTML5的常用函数总结
2016/05/17 Javascript
深入理解jQuery()方法的构建原理
2016/12/05 Javascript
jquery插件bootstrapValidator表单验证详解
2016/12/15 Javascript
解决vue.js 数据渲染成功仍报错的问题
2018/08/25 Javascript
小程序实现新用户判断并跳转激活的方法
2019/05/20 Javascript
微信小程序之下拉列表实现方法解析(附完整源码)
2019/08/23 Javascript
[43:24]完美世界DOTA2联赛PWL S3 INK ICE vs DLG 第二场 12.12
2020/12/17 DOTA
Python日志模块logging简介
2015/04/13 Python
理解Python中的With语句
2016/03/18 Python
Python学习入门之区块链详解
2017/07/25 Python
Python模块WSGI使用详解
2018/02/02 Python
使用pandas的DataFrame的plot方法绘制图像的实例
2018/05/24 Python
Pandas:Series和DataFrame删除指定轴上数据的方法
2018/11/10 Python
jupyter notebook 中输出pyecharts图实例
2020/04/23 Python
python给图像加上mask,并提取mask区域实例
2020/01/19 Python
Python中 Global和Nonlocal的用法详解
2020/01/20 Python
django rest framework使用django-filter用法
2020/07/15 Python
python 实现控制鼠标键盘
2020/11/27 Python
css3 中实现炫酷的loading效果
2019/04/26 HTML / CSS
亚马逊巴西站:Amazon.com.br
2019/09/22 全球购物
土木工程师岗位职责
2013/11/24 职场文书
学生党员思想汇报
2013/12/28 职场文书
婚庆司仪主持词
2014/03/15 职场文书
学校标语大全
2014/06/19 职场文书
档案工作汇报材料
2014/08/21 职场文书
国庆促销活动总结
2014/08/29 职场文书
小学生教师节演讲稿
2014/09/03 职场文书
党的群众路线教育实践活动查摆问题及整改措施
2014/10/10 职场文书
个人工作失误的保证书怎么写?
2019/06/21 职场文书
小程序实现文字循环滚动动画
2021/06/14 Javascript
python自动化八大定位元素讲解
2021/07/09 Python