给自己拍张照


  1. from time import sleep
  2. from picamera import PiCamera
  3. camera=PiCamera()
  4. camera.resolution=(320,240)
  5. camera.start_preview()
  6. sleep(2)
  7. camera.capture('photo.jpg')

这里考虑到树莓派的计算能力,分辨率选择320,240
如果对于速度要求不高,可以选择更高分辨率,需要对后面的代码进行相应修改


把自己添加到识别队列中


  1. # This is a demo of running face recognition on a Raspberry Pi.
  2. # This program will print out the names of anyone it recognizes to the console.
  3. # To run this, you need a Raspberry Pi 2 (or greater) with face_recognition and
  4. # the picamera[array] module installed.
  5. # You can follow this installation instructions to get your RPi set up:
  6. # https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
  7. import face_recognition
  8. import picamera
  9. import numpy as np
  10. # Get a reference to the Raspberry Pi camera.
  11. # If this fails, make sure you have a camera connected to the RPi and that you
  12. # enabled your camera in raspi-config and rebooted first.
  13. camera = picamera.PiCamera()
  14. camera.resolution = (320, 240)
  15. output = np.empty((240, 320, 3), dtype=np.uint8)
  16. # Load a sample picture and learn how to recognize it.
  17. print("Loading known face image(s)")
  18. obama_image = face_recognition.load_image_file("obama_small.jpg")
  19. obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
  20. lingxiao_image = face_recognition.load_image_file("LingxiaoWang.jpg")
  21. lingxiao_face_encoding = face_recognition.face_encodings(lingxiao_image)[0]
  22. # Initialize some variables
  23. face_locations = []
  24. face_encodings = []
  25. while True:
  26. print("Capturing image.")
  27. # Grab a single frame of video from the RPi camera as a numpy array
  28. camera.capture(output, format="rgb")
  29. # Find all the faces and face encodings in the current frame of video
  30. face_locations = face_recognition.face_locations(output)
  31. print("Found {} faces in image.".format(len(face_locations)))
  32. face_encodings = face_recognition.face_encodings(output, face_locations)
  33. # Loop over each face found in the frame to see if it's someone we know.
  34. for face_encoding in face_encodings:
  35. # See if the face is a match for the known face(s)
  36. match = face_recognition.compare_faces([obama_face_encoding,lingxiao_face_encoding], face_encoding)
  37. name = "<Unknown Person>"
  38. if match[0]:
  39. name = "Barack Obama"
  40. if match[1]:
  41. name = "Lingxiao Wang"
  42. print("I see someone named {}!".format(name))

这样树莓派便可以识别出我和奥巴马两个人了 :)
简单尝试 - 图1


利用opencv进行显示


进一步修改代码,利用opencv把自己圈出来

  1. # This is a demo of running face recognition on a Raspberry Pi.
  2. # This program will print out the names of anyone it recognizes to the console.
  3. # To run this, you need a Raspberry Pi 2 (or greater) with face_recognition and
  4. # the picamera[array] module installed.
  5. # You can follow this installation instructions to get your RPi set up:
  6. # https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65
  7. import face_recognition
  8. import picamera
  9. import numpy as np
  10. import cv2
  11. # Get a reference to the Raspberry Pi camera.
  12. # If this fails, make sure you have a camera connected to the RPi and that you
  13. # enabled your camera in raspi-config and rebooted first.
  14. camera = picamera.PiCamera()
  15. camera.resolution = (320, 240)
  16. frame = np.empty((240, 320, 3), dtype=np.uint8)
  17. output = np.empty((240, 320, 3), dtype=np.uint8)
  18. # Load a sample picture and learn how to recognize it.
  19. print("Loading known face image(s)")
  20. obama_image = face_recognition.load_image_file("obama_small.jpg")
  21. obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
  22. lingxiao_image = face_recognition.load_image_file("LingxiaoWang.jpg")
  23. lingxiao_face_encoding = face_recognition.face_encodings(lingxiao_image)[0]
  24. # Initialize some variables
  25. face_locations = []
  26. face_encodings = []
  27. while True:
  28. face_names=[]
  29. print("Capturing image.")
  30. # Grab a single frame of video from the RPi camera as a numpy array
  31. camera.capture(frame, format="bgr")
  32. #这里需要注意的是,opencv使用的是bgr格式的图像,要转换成rgb格式才能用来识别
  33. frame=frame.reshape((240,320,3))
  34. output=frame[:,:,::-1]
  35. # Find all the faces and face encodings in the current frame of video
  36. face_locations = face_recognition.face_locations(output)
  37. print("Found {} faces in image.".format(len(face_locations)))
  38. face_encodings = face_recognition.face_encodings(output, face_locations)
  39. # Loop over each face found in the frame to see if it's someone we know.
  40. for face_encoding in face_encodings:
  41. # See if the face is a match for the known face(s)
  42. match = face_recognition.compare_faces([obama_face_encoding,lingxiao_face_encoding], face_encoding)
  43. name = "<Unknown Person>"
  44. if match[0]:
  45. name = "Barack Obama"
  46. if match[1]:
  47. name = "Lingxiao Wang"
  48. print("I see someone named {}!".format(name))
  49. face_names.append(name)
  50. # Display the results
  51. for (top, right, bottom, left), name in zip(face_locations, face_names):
  52. # Draw a box around the face
  53. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  54. # Draw a label with a name below the face
  55. cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  56. font = cv2.FONT_HERSHEY_DUPLEX
  57. cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  58. # Display the resulting image
  59. cv2.imshow('Video', frame)
  60. if cv2.waitKey(1) & 0xFF == ord('q'):
  61. break
  62. cv2.destroyAllWindows()

简单尝试 - 图2我自己
简单尝试 - 图3我和奥巴马(手动滑稽)

简单尝试 - 图4帅到模糊的学长

另外,此模型对于亚洲人脸识别准确率有待提高,可能会出现张冠李戴的情况。


opencv的玄学


安装opencv是个玄学,碰到问题自行百度,安装相应的库即可
注意默认pip版本,默认python版本