东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 4111|回复: 1
打印 上一主题 下一主题

[课堂笔记] 12、从摄像头进行实时人脸识别的简单Demo附源码

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14437
QQ
跳转到指定楼层
楼主
发表于 2020-2-28 10:10:21 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
12、从摄像头进行实时人脸识别的简单Demo附源码




  1. # -*- coding: utf-8 -*-
  2. import face_recognition
  3. import cv2
  4. import numpy as np

  5. # 12、从摄像头进行实时人脸识别的简单Demo附源码


  6. # Get a reference to webcam #0 (the default one)
  7. video_capture = cv2.VideoCapture(0)

  8. # Load a sample picture and learn how to recognize it.
  9. obama_image = face_recognition.load_image_file("obama.jpg")
  10. obama_face_loc = face_recognition.face_locations(obama_image)
  11. # 只有一个人脸位置
  12. obama_face_encoding = face_recognition.face_encodings(obama_image, obama_face_loc)[0]

  13. dfy_image = face_recognition.load_image_file("dfy.jpg")
  14. dfy_face_loc = face_recognition.face_locations(dfy_image)
  15. # 问题:face_encodings 是对大图还是小的人脸图获取128维embedding
  16. # face_image: The image that contains one or more faces 结论:大图 还有一个参数known_face_locations
  17. dfy_face_encoding = face_recognition.face_encodings(face_image=dfy_image, known_face_locations=dfy_face_loc)[0]

  18. # Load a second sample picture and learn how to recognize it.
  19. biden_image = face_recognition.load_image_file("biden.jpg")
  20. biden_face_loc = face_recognition.face_locations(biden_image)
  21. biden_face_encoding = face_recognition.face_encodings(biden_image, biden_face_loc)[0]

  22. # Create arrays of known face encodings and their names
  23. # 知道的人脸编码 是唯一的每个人
  24. known_face_encodings = [
  25.     obama_face_encoding,
  26.     dfy_face_encoding,
  27.     biden_face_encoding
  28. ]
  29. known_face_names = [
  30.     "Barack Obama",
  31.     "Dfy",
  32.     "Joe Biden"
  33. ]

  34. # Initialize some variables
  35. face_locations = []
  36. face_encodings = []
  37. face_names = []
  38. process_this_frame = True

  39. while True:

  40.     # Grab a single frame of video
  41.     ret, frame = video_capture.read()

  42.     # Resize frame of video to 1/4 size for faster face recognition processing
  43.     # 问题:这里的(0,0)代表中心 缩放到原来的四分之一
  44.     # 默认的插值方法为:双线性插值   cv2.INTER_NEAREST最近邻插值法
  45.     small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)

  46.     # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
  47.     rgb_small_frame = small_frame[:, :, ::-1]

  48.     # 仅每隔一帧处理一次视频以节省时间
  49.     if process_this_frame:
  50.         # Find all the faces and face encodings in the current frame of video
  51.         face_locations = face_recognition.face_locations(rgb_small_frame)
  52.         # 找到所有的人脸 和 人脸的特征编码
  53.         face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

  54.         face_names = []  # 这里并没有重复 为了下一次的清空
  55.         for face_encoding in face_encodings:
  56.             # See if the face is a match for the known face(s)
  57.             matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
  58.             name = "Unknown"

  59.             # # If a match was found in known_face_encodings, just use the first one.
  60.             # if True in matches:
  61.             #     first_match_index = matches.index(True)
  62.             #     name = known_face_names[first_match_index]

  63.             # Or instead, use the known face with the smallest distance to the new face
  64.             face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
  65.             best_match_index = np.argmin(face_distances)
  66.             # 同时考虑两个API:compare_faces  face_distance
  67.             if matches[best_match_index]:
  68.                 # 识别出来 人脸是谁了
  69.                 name = known_face_names[best_match_index]

  70.             face_names.append(name)

  71.     # 这是为了降低处理速度的
  72.     process_this_frame = not process_this_frame

  73.     # Display the results   人脸位置的格式:(y1, x2, y2, x1)
  74.     for (top, right, bottom, left), name in zip(face_locations, face_names):
  75.         # Scale back up face locations since the frame we detected in was scaled to 1/4 size
  76.         top *= 4
  77.         right *= 4
  78.         bottom *= 4
  79.         left *= 4

  80.         # Draw a box around the face  cv2里坐标必须都是整数
  81.         cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

  82.         # Draw a label with a name below the face
  83.         # 也是画了一个矩形 但是有背景的 cv2.filled
  84.         cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  85.         font = cv2.FONT_HERSHEY_DUPLEX
  86.         cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

  87.     # Display the resulting image
  88.     cv2.imshow('Video', frame)

  89.     # Hit 'q' on the keyboard to quit!
  90.     if cv2.waitKey(1) & 0xFF == ord('q'):
  91.         break

  92. # Release handle to the webcam
  93. video_capture.release()
  94. cv2.destroyAllWindows()
复制代码


obama2.jpg (180.13 KB, 下载次数: 266)

obama2.jpg

obama.jpg (273.36 KB, 下载次数: 264)

obama.jpg

biden.jpg (345.43 KB, 下载次数: 271)

biden.jpg

face_recog_webcam.png (360.14 KB, 下载次数: 268)

face_recog_webcam.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

0

主题

7

帖子

26

积分

新手上路

Rank: 1

积分
26
沙发
发表于 2020-3-5 16:54:11 | 只看该作者
您好,怎么提示没有  ModuleNotFoundError: No module named 'face_recognition'
回复

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-3 18:07 , Processed in 0.190216 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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