当前位置:   article > 正文

face_recognition+python-opencv实现摄像头实时人脸识别

face_recognition+python-opencv实现摄像头实时人脸识别

参考:
https://www.jianshu.com/p/9d5f0020acd5

完整代码

# -*- coding: utf-8 -*-

import face_recognition

import cv2

import os

camera = cv2.VideoCapture(0)

font = cv2.FONT_HERSHEY_DUPLEX

face_names = []

face_codings = []

person_list = os.listdir("faces/")

for i in range(len(person_list)):

    person_name = os.listdir("faces/" + "person_" + str(i + 1))

    # print(person_name[0])

    face_img = face_recognition.load_image_file("faces/" + "person_" + str(i + 1) + "/" + person_name[0])

    face_codings.append(face_recognition.face_encodings(face_img)[0])

    face_names.append(person_name[0][:person_name[0].index(".")])

while True:

    success,img=camera.read()

    img_new = cv2.resize(img, (0, 0), fx=0.25, fy=0.25)

    process_this_frame = True

    if process_this_frame:

        marks = face_recognition.face_locations(img_new)

        codings = face_recognition.face_encodings(img_new, marks)

        for coding in codings:

            result = face_recognition.compare_faces(face_codings, coding,0.4)

            print(result)

            for i in range(len(result)):

                if result[i]:

                    name = face_names[i]

                    break

                if i == len(result)-1:

                    name = "unknown"

                #break

        process_this_frame = not process_this_frame

        for (top, right, bottom, left)in (marks):

            top *= 4

            right *= 4

            bottom *= 4

            left *= 4

            cv2.rectangle(img, (left, top), (right, bottom), (255, 0, 0), 2)

            cv2.putText(img, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

        cv2.imshow('face', img)

        if cv2.waitKey(1) & 0xFF == ord('q'):

            break

camera.release()

cv2.destroyAllWindows()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/545477
推荐阅读
相关标签
  

闽ICP备14008679号