-
python 人脸识别
2018-06-11 19:08:58python 人脸识别代码,大家可以一起学习讨论, python 人脸识别代码,大家可以一起学习讨论, -
python人脸识别
2015-06-28 17:50:38python人脸识别 -
Python人脸识别
2019-04-26 14:38:35Python人脸识别介绍及部分代码,仅供学习参考,此内容为学习获取,禁止转载。 -
python人脸识别plc_实例详解Python人脸识别
2021-02-09 21:44:52最近iPhone X博人眼球,其中最绝妙的设计就是人脸识别解锁,本文主要为大家详细介绍了Python人脸识别初探的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。1.利用opencv库sudo apt...最近iPhone X博人眼球,其中最绝妙的设计就是人脸识别解锁,本文主要为大家详细介绍了Python人脸识别初探的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。
1.利用opencv库sudo apt-get install libopencv-*
sudo apt-get install python-opencv
sudo apt-get install python-numpy
2 .Python实现import os
import os
from PIL import Image,ImageDraw
import cv
def detect_object(image):
grayscale = cv.CreateImage((image.width,image.height),8,1)#创建空的灰度值图片
cv.CvtColor(image,grayscale,cv.CV_BGR2GRAY)
cascade=cv.Load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml")#记载特征值库,此目录下还有好多库可以选用
rect=cv.HaarDetectObjects(grayscale,cascade,cv.CreateMemStorage(),1.1,2,cv.CV_HAAR_DO_CANNY_PRUNING,(20,20))
result=[]#标记位置
for r in rect:
result.append((r[0][0],r[0][1],r[0][0]+r[0][2],r[0][1]+r[0][3]))
return result
def process(infile):
image = cv.LoadImage(infile)
if image:
faces = detect_object(image)
im = Image.open(infile)
path = os.path.abspath(infile)
save_path = os.path.splitext(path)[0]+"_face"
try:
os.mkdir(save_path)
except:
pass
if faces:
draw = ImageDraw.Draw(im)
count=0
for f in faces:
count+=1
draw.rectangle(f,outline=(255,0,0))
a=im.crop(f)
file_name=os.path.join(save_path,str(count)+".jpg")
a.save(file_name)
drow_save_path = os.path.join(save_path,"out.jpg")
im.save(drow_save_path,"JPEG",quality=80)
else:
print "Error: cannot detect faces on %s" % infile
if __name__ == "__main__":
process("test3.jpg")
3.效果对比
4.参考资料
python使用opencv进行人脸识别
Python+OpenCV人脸检测原理及示例详解
python利用OpenCV2实现人脸检测
相关推荐:
AI中Python 的人脸识别
基于HTML5 的人脸识别活体认证的实现方法
求微信开发人脸识别源码,详细
-
python 人脸识别_Python实现人脸识别
2020-12-07 06:32:09今天配置了一天的开发环境,终于写了一套人脸识别的程序用的库是face_recognition,这是一款免费、开源、实时、离线的Python人脸识别库。Github网址为https://github.com/ageitgey/face_recognition。我电脑上安装了...今天配置了一天的开发环境,终于写了一套人脸识别的程序
用的库是face_recognition,这是一款免费、开源、实时、离线的Python人脸识别库。Github网址为https://github.com/ageitgey/face_recognition。我电脑上安装了Anaconda,dlib会自动获取。Windows系统安装dlib可直接在网站https://pypi.org/simple/dlib/下载对应版本的whl文件,用pip安装,简单高效。
#读取照片并圈出人脸import numpy as npimport cv2import face_recognitionimage = face_recognition.load_image_file("mypic.jpg")#return (A,B,C,D)(top, right, bottom, left) (D,A,B,C)face_locations = face_recognition.face_locations(image)print(face_locations)image1=image*1image1[:,:,0]=image[:,:,2]image1[:,:,2]=image[:,:,0]for (A,B,C,D) in face_locations: cv2.rectangle(image1,(D,A),(B,C),(0,255,0),2)cv2.imshow('image',image1)cv2.waitKey(0)cv2.destroyAllWindows()
(1)从本地读图片并使用face_recognition和opencv识别人脸并标注显示
#摄像头读取并识别人脸import numpy as npimport cv2 import face_recognitioncap=cv2.VideoCapture(0)while True: ret,frame=cap.read() #return (A,B,C,D)(top, right, bottom, left) (D,A,B,C) face_locations = face_recognition.face_locations(frame) for (A,B,C,D) in face_locations: cv2.rectangle(frame,(D,A),(B,C),(0,255,0),2) cv2.imshow('image',frame) if cv2.waitKey(1)& 0xFF == ord('q'): breakcap.release()cv2.destroyAllWindows()
(2)从摄像头读取图像并显示
#人脸比对,单对单的比对,和多对单的比对import numpy as npimport cv2import face_recognitionzhujun=face_recognition.face_encodings(face_recognition.load_image_file('zhujun.jpg'))dongqing=face_recognition.face_encodings(face_recognition.load_image_file('dongqing.jpg'))#ss=face_recognition.compare_faces([dongqing,lisisi],zhujun)#hezhao=face_recognition.face_encodings(face_recognition.load_image_file('mypic.jpg'))#ss=face_recognition.compare_faces(hezhao,lisisi,0.5)hezhao=face_recognition.load_image_file('mypic.jpg') locations=face_recognition.face_locations(hezhao)for [A,B,C,D] in locations: face=hezhao[A:C,D:B,:] faceencoding=face_recognition.face_encodings(face) ss=face_recognition.compare_faces(faceencoding,zhujun[0],0.5) print(ss)
(3)人脸比对
最后的实现
哈哈哈
有点酷
留言区
-
python人脸识别方法_Python人脸识别技术有多强?看完这个,我彻底服了!
2020-11-22 15:10:01原标题:Python人脸识别技术有多强?看完这个,我彻底服了!用Python进行人脸识别附源码,进行了多张图片的测试,可以很精准的定位在人脸的部位,以方框来标记人脸部位,和android智能手机拍摄时的人脸识别效果相似...原标题:Python人脸识别技术有多强?看完这个,我彻底服了!
用Python进行人脸识别附源码,进行了多张图片的测试,可以很精准的定位在人脸的部位,以方框来标记人脸部位,和android智能手机拍摄时的人脸识别效果相似,但代码差别就大了。
638855753,即可领取,里面不光有Python编程教学,每晚8点还有Python直播教学哦!
如何用Python实现人脸识别:
示例一(1行代码实现人脸识别):
1. 首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片。其中每个人一张图片,图片以人的名字命名:
known_people文件夹下有babe、成龙、容祖儿的照片
2. 接下来,你需要准备另一个文件夹,里面是你要识别的图片:
unknown_pic文件夹下是要识别的图片,其中韩红是机器不认识的
3. 然后你就可以运行face_recognition命令了,把刚刚准备的两个文件夹作为参数传入,命令就会返回需要识别的图片中都出现了谁:
识别成功!!!
示例二(识别图片中的所有人脸并显示出来):
# filename : find_faces_in_picture.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imagingfrom PIL
import Image
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition
# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("/opt/face/unknown_pic/all_star.jpg")
# 使用默认的给予HOG模型查找图像中所有人脸
# 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速
# 另请参见: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)
# 使用CNN模型
# face_locations = face_recognition.
face_locations(image, number_of_times_to_upsample=0, model="cnn")
# 打印:我从图片中找到了 多少 张人脸
print("I found {} face(s) in this photograph.".format(len(face_locations)))
# 循环找到的所有人脸
for face_location in face_locations:
# 打印每张脸的位置信息
top, right, bottom, left = face_location
print("A face is located at pixel location Top:
{}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# 指定人脸的位置信息,然后显示人脸图片
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show
用于识别的图片# 执行python文件 $ python find_faces_in_picture.py
从图片中识别出7张人脸,并显示出来
示例三(自动识别人脸特征):
# filename : find_facial_features_in_picture.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition
# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("biden.jpg")
#查找图像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
for face_landmarks in face_landmarks_list:
#打印此图像中每个面部特征的位置
facial_features = [
'chin',
'left_eyebrow',
'right_eyebrow',
'nose_bridge',
'nose_tip',
'left_eye',
'right_eye',
'top_lip',
'bottom_lip'
]
for facial_feature in facial_features:
print("The {} in this face has the following points: {}".format(facial_feature,
face_landmarks[facial_feature]))
#让我们在图像中描绘出每个人脸特征!
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for facial_feature in facial_features:
d.line(face_landmarks[facial_feature], width=5)
pil_image.show
自动识别出人脸特征
示例四(识别人脸鉴定是哪个人):
# filename : recognize_faces_in_pictures.py
# -*- conding: utf-8 -*-
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition
#将jpg文件加载到numpy数组中
babe_image = face_recognition.load_image_file("/opt/face/known_people/babe.jpeg")
Rong_zhu_er_image = face_recognition.load_image_file("/opt/face/known_people/Rong zhu er.jpg")
unknown_image = face_recognition.load_image_file("/opt/face/unknown_pic/babe2.jpg")
#获取每个图像文件中每个面部的面部编码
#由于每个图像中可能有多个面,所以返回一个编码列表。
#但是由于我知道每个图像只有一个脸,我只关心每个图像中的第一个编码,所以我取索引0。
babe_face_encoding = face_recognition.face_encodings(babe_image)[0]
Rong_zhu_er_face_encoding = face_recognition.face_encodings(Rong_zhu_er_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
known_faces = [
babe_face_encoding,
Rong_zhu_er_face_encoding
]
#结果是True/false的数组,未知面孔known_faces阵列中的任何人相匹配的结果
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
print("这个未知面孔是 Babe 吗? {}".format(results[0]))
print("这个未知面孔是 容祖儿 吗? {}".format(results[1]))
print("这个未知面孔是 我们从未见过的新面孔吗? {}".format(not True in results))
显示结果如图
示例五(识别人脸特征并美颜):
# filename : digital_makeup.py
# -*- coding: utf-8 -*-
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition
#将jpg文件加载到numpy数组中
image = face_recognition.load_image_file("biden.jpg")
#查找图像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
for face_landmarks in face_landmarks_list:
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
#让眉毛变成了一场噩梦
d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)
#光泽的嘴唇
d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)
#闪耀眼睛
d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))
#涂一些眼线
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)
pil_image.show()
声明:本文于网络整理,著作权归原作者所有,如有侵权,请联系小编删除。返回搜狐,查看更多
责任编辑:
-
-
python人脸识别截取
2018-10-20 20:37:42python人脸识别并截取人脸保存到本地,自带haarcascades库 -
python人脸识别.zip
2020-02-21 19:46:04python人脸识别.zip -
Python人脸识别初探
2020-09-20 23:20:31主要为大家详细介绍了Python人脸识别初探的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
python人脸识别防小偷_Python人脸识别
2020-12-16 05:20:49今天来分享一下关于Python图片膨胀和腐蚀、图片人脸识别以及动态人脸识别的代码~~~开心呢,本菜鸟当时可是沉浸在cv2库中久久不能自拔了好久的呢~~希望你也能享受Python带来的小惊喜哟。。0x 00 Python图片膨胀与腐蚀...今天来分享一下关于Python图片膨胀和腐蚀、图片人脸识别以及动态人脸识别的代码~~~开心呢,本菜鸟当时可是沉浸在cv2库中久久不能自拔了好久的呢~~希望你也能享受Python带来的小惊喜哟。。
0x 00 Python图片膨胀与腐蚀
图像的膨胀和腐蚀主要是寻找图像中的极大和极小区域。代码中的结构元素是指:设有两幅图象B,X。若X是被处理的对象,而B是用来处理X的,则称B为结构元素(structure element),又被形象地称做刷子。结构元素通常都是一些比较小的图象。详细的一些原理概念见文末链接啦~~
"""图片膨胀与腐蚀"""
import cv2
#读取图片:cv2.imread(路径,num)
img = cv2.imread("1.jpg",0)
#构造一个3*3的结构元素
elment = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
#膨胀图像cv2.dilate(图像,元素结构)
dilate = cv2.dilate(img,elment)
#腐蚀图像cv2.erode(图像,元素结构)
erode = cv2.erode(img,elment)
#将两幅图像相减获得边,第一个参数是膨胀后的图像,第二个参数是腐蚀后的图像
result = cv2.absdiff(dilate,erode)
#阈值类型:'TERM_CRITERIA_COUNT', 'TERM_CRITERIA_EPS', 'TERM_CRITERIA_MAX_ITER',
# 'THRESH_BINARY', 'THRESH_BINARY_INV', 'THRESH_MASK', 'THRESH_OTSU',
# 'THRESH_TOZERO_INV', 'THRESH_TRIANGLE', 'THRESH_TRUNC'
retval,result = cv2.threshold(result,50,255,cv2.THRESH_BINARY);
#反色,即对二值图像每个像素取反
result = cv2.bitwise_not(result);
#显示图像
cv2.imshow('origin',img)
#原图
cv2.imshow('result',result)
#边缘检测图
cv2.waitKey(0)
cv2.destroyAllWindows()
0x01 图片人脸识别
图片人脸识别分为以下几步:
图片灰度化、几何变换、图像增强、归一化
特征点定位、人脸对齐、抓取人脸特征"""人脸检测"""
import cv2
#调用人脸检测特征库
face = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
#读取图像文件
sample_imag = cv2.imread('1.jpg')
#人脸检测
faces = face.detectMultiScale(sample_imag,scaleFactor=1.1,minNeig hbors=5,minSize=(10,10))
#画框处理
for (x,y,w,h) in faces:
cv2.rectangle(sample_imag,(x,y),(x+w,y+h),(0,255,0),2)
#结果写入图像
cv2.imwrite('face.jpg',sample_imag)
print("detect success")
#新建窗口显示图像
cv2.namedWindow("Image")
cv2.imshow("Image",sample_imag)
cv2.waitKey(0)
cv2.destroyAllWindows()
展示一下识别我詹皇:帅
还有识别勇士的时候:(不会吧不会吧,竟然没识别到~~我去学习训库了)
0x02 动态人脸识别
动态人脸识别是不需要停驻等待,你只要出现在一定识别范围内,无论你是行走还是停立,系统就会自动进行识别,也就是说,人以自然的形态走过去,摄像头会进行信息的抓拍和采集,发出相应的指令,进行动态人脸识别。
首要是依据人脸器官的形状描绘以及他们之间的间隔特性来获得有助于人脸分类的特征数据,其特征重量一般包含特征点间的欧氏间隔、曲率和视点等。
import cv2
# 1.使用OpenCV的分类器
# 2.从摄像头或本地中读取照片
# 3.在图片上换框
# 4.在新窗口上展示图片
# 1.使用OpenCV的分类器/特征库
detector = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
# 2.从摄像头或本地中读取照片
cap = cv2.VideoCapture(0)
while True:
ret,img = cap.read()
faces = detector.detectMultiScale(img,1.3,5)
# 3.在图片上换框
for (x,y,w,h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('frame',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 4.在新窗口上展示图片
cap.release()
cv2.destroyAllWindows()
0x03 原理详解参考
0x04 小结
当时学习这个的时候超级Happy的,就是那种长见识的那种感觉,眼里放着光的那种~~学Python,真快乐呢。。。希望你也能从代码中获得乐趣,呐,以下是那个人脸识别库的xml网址,下载来试试,识别一下你会超快乐的哟。。(下面链接失效请私信我发给你)
-
python人脸识别教程_python人脸识别教程
2020-11-20 19:55:08python有两个著名的人脸识别库:dlib和face-recognition,face-recognition是基于dlib开发的,比dlib更加简单,这次我们为了理解人脸识别的过程,所以选择dlib库,大家也可以访问...第一步,安装dlib:打开cmd,输入 ...