找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7863|回复: 12
收起左侧

用python3.6在电脑上实现用摄像头来人脸识别源程序

  [复制链接]
ID:541667 发表于 2019-5-18 22:03 | 显示全部楼层 |阅读模式
在这里,我用了anaconda,OpenCV,python3.6来实现代码的,代码在附件中,可以自己下载了,自己试试

python源程序如下:
  1. # -*- coding: UTF-8 -*-
  2. import face_recognition
  3. import cv2
  4. import os
  5. # 这是一个超级简单(但很慢)的例子,在你的网络摄像头上实时运行人脸识别
  6. # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
  7. # 请注意:这个例子需要安装OpenCV
  8. # 具体的演示。如果你安装它有困难,试试其他不需要它的演示。
  9. # 得到一个参考的摄像头# 0(默认)
  10. video_capture = cv2.VideoCapture(0)
  11. # 加载示例图片并学习如何识别它。
  12. path ="images"#在同级目录下的images文件中放需要被识别出的人物图
  13. total_image=[]
  14. total_image_name=[]
  15. total_face_encoding=[]
  16. for fn in os.listdir(path): #fn 表示的是文件名
  17.   total_face_encoding.append(face_recognition.face_encodings(face_recognition.load_image_file(path+"/"+fn))[0])
  18.   fn=fn[:(len(fn)-4)]#截取图片名(这里应该把images文件中的图片名命名为为人物名)
  19.   total_image_name.append(fn)#图片名字列表
  20. while True:
  21.   # 抓取一帧视频
  22.   ret, frame = video_capture.read()
  23.   # 发现在视频帧所有的脸和face_enqcodings
  24.   face_locations = face_recognition.face_locations(frame)
  25.   face_encodings = face_recognition.face_encodings(frame, face_locations)
  26.   # 在这个视频帧中循环遍历每个人脸
  27.   for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
  28.       # 看看面部是否与已知人脸相匹配。
  29.       for i,v in enumerate(total_face_encoding):
  30.           match = face_recognition.compare_faces([v], face_encoding,tolerance=0.5)
  31.           name = "Unknown"
  32.           if match[0]:
  33.               name = total_image_name[i]
  34.               break
  35.       # 画出一个框,框住脸
  36.       cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  37.       # 画出一个带名字的标签,放在框下
  38.       cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  39.       font = cv2.FONT_HERSHEY_DUPLEX
  40.       cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  41.   # 显示结果图像
  42.   cv2.imshow('Video', frame)
  43.   # 按q退出
  44.   if cv2.waitKey(1) & 0xFF == ord('q'):
  45.       break
  46. # 释放摄像头中的流
  47. video_capture.release()
  48. cv2.destroyAllWindows()
复制代码

以上代码下载:
人脸识别.zip (1.32 KB, 下载次数: 201)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:473410 发表于 2019-10-25 10:58 | 显示全部楼层
这个怎么么用啊...电脑上自带的摄像头可以吗
回复

使用道具 举报

ID:630764 发表于 2019-10-27 08:52 | 显示全部楼层
想試一試
回复

使用道具 举报

ID:384552 发表于 2019-10-31 15:58 | 显示全部楼层
谢谢你的辛勤劳动
回复

使用道具 举报

ID:282095 发表于 2020-4-24 17:38 | 显示全部楼层
楼上的人脸识别face_recognition的定义是否需要更改
回复

使用道具 举报

ID:701198 发表于 2020-5-12 08:43 | 显示全部楼层
人脸识别这么简单吗?
回复

使用道具 举报

ID:749484 发表于 2020-5-13 09:26 | 显示全部楼层
不错。支持一下,现在python是主流了。
回复

使用道具 举报

ID:74229 发表于 2020-5-25 10:28 | 显示全部楼层
不错。支持一下。
回复

使用道具 举报

ID:766208 发表于 2020-6-1 09:16 | 显示全部楼层
可以试试
回复

使用道具 举报

ID:764088 发表于 2020-6-9 09:16 | 显示全部楼层
thanks you for your sharing
回复

使用道具 举报

ID:796792 发表于 2021-2-1 13:29 | 显示全部楼层
放眼天下 发表于 2019-10-25 10:58
这个怎么么用啊...电脑上自带的摄像头可以吗

同问 用电脑自带的摄像头可以吗
回复

使用道具 举报

ID:964308 发表于 2021-11-2 09:29 来自手机 | 显示全部楼层
xnhtao 发表于 2020-5-12 08:43
人脸识别这么简单吗?

都是别人写好了的库,真实的人脸识别难得多
回复

使用道具 举报

ID:359281 发表于 2023-10-8 10:40 | 显示全部楼层
谢谢分享一下这个程序,谢谢楼主!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表