人脸检测识别-python库包or开源项目(仅CPU)

目录

0. 前提

1. 人脸的python库包or开源项目

2. windows上安装教程

3. 样例代码


0. 前提

        不是所有人都有GPU,因此这篇文章探索的是笔记本CPU就可以inference的程序;而且鉴于很多人对C++不是很熟悉(比如我),所以这里均为python入门!

        你可以拿这个练手,即时效果很好。

1. 人脸的python库包or开源项目

        [1] dlib 可以采用GPU,但是默认关闭,博主暂时没有免费的显卡用

        [2] face_recognition  此项目基于dlib实现,但是dlib自己也可实现;API文档

        [3] libfacedetection 可以基于opencv-python-DNN实现,Link

        TODO

2. windows上安装教程

        在linux上均容易安装,但是windows下不易。

        环境: windows、python3.8

        pip install dlib-19.19.0-cp38-cp38-win_amd64.whl

        pip install face_recognition

        pip install opencv-python

        dlib百度云链接 提取码:1sk6

3. 样例代码

        

import cv2
import numpy as np
import torch
import time
'''
    目标检测(AI)-yolov5
'''

# model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
# # img = r"E:\Python_Code\test\test.jpg"
# # results = model(img)
# # print(results.pandas().xyxy[0]["class"])
# # results.show()
# cap = cv2.VideoCapture(700)
# num = 0
# while True:
#     if num % 2 != 0:
#         continue
#     ret, img = cap.read()
#     if ret:
#         results = model(img)
#         results_pandas = results.pandas().xyxy[0]  # 只有1张图像
#         # 筛选数据,逻辑判断
#         results_person = results_pandas[results_pandas["class"]==0]
#         results_person_ = results_person.to_numpy()
#         # 遍历画框
#         rows = results_person_.shape[0]
#         for i in range(rows):
#             x1 = int(results_person_[i][0])
#             y1 = int(results_person_[i][1])
#             x2 = int(results_person_[i][2])
#             y2 = int(results_person_[i][3])

#             cv2.rectangle(img, (x1,y1), (x2,y2), (0,0,255), 2)
#         cv2.imshow("img", img)
#         if cv2.waitKey(1) & 0xFF == ord('q'):
#             break


'''
    人脸检测(opencv-python)
'''
# face_cascade = cv2.CascadeClassifier("D:\Anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml")
# eye_cascade = cv2.CascadeClassifier("D:\Anaconda3\Lib\site-packages\cv2\data\haarcascade_eye.xml")
# cap = cv2.VideoCapture(700)// 笔记本的可能为0 或 700

# while True:
#     ret, img = cap.read()
#     if ret:
#         gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#         faces = face_cascade.detectMultiScale(gray,1.1,5)
#         if len(faces)>0:
#             for faceRect in faces:
#                 x, y, w, h = faceRect
#                 cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
#                 roi_gray = gray[y:y+h//2, x:x+w]
#                 roi_color = img[y:y+h//2, x:x+w]
#                 eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 1, cv2.CASCADE_SCALE_IMAGE, (2,2))
#                 for (ex,ey,ew,eh) in eyes:
#                     cv2.rectangle(roi_color, (ex,ey), (ex+ew,ey+eh), (0,255,0), 2)
#         cv2.imshow("img",img)
#         if cv2.waitKey(1) & 0xFF == ord('q'):
#             break


'''
    dlib人脸检测
'''
# import dlib
# detector = dlib.get_frontal_face_detector()

# cap = cv2.VideoCapture(700)
# while True:
#     ret, img = cap.read()
#     if ret:
#         dets = detector(img, 1) 
#         for i, d in enumerate(dets):
#             cv2.rectangle(img, (d.left()-10,d.top()-10), (d.right()+10,d.bottom()+10), (0,0,255), 4)
#         print(img)
#         cv2.imshow("img",img)
#         if cv2.waitKey(1) & 0xFF == ord('q'):
#             break


# import sys
# import dlib
# from skimage import io
 
# detector = dlib.get_frontal_face_detector()
# window = dlib.image_window() 
# img = cv2.imread(r"D:\20191215155844414.png")
 
# dets = detector(img, 1) 
# print("Number of faces detected: {}".format(len(dets)))
# for i, d in enumerate(dets):
#     print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(i, d.left(), d.top(), d.right(), d.bottom()))
 
# window.clear_overlay()
# window.set_image(img)
# window.add_overlay(dets)
# dlib.hit_enter_to_continue()


'''
    基于dlib的 face_recognition 人脸检测识别
'''
import face_recognition
import cv2
import numpy as np

# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

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

# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("leilei.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("leilei1.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "LeiLei",
    "LeiLei1"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

downsample_ratio = 0.5
upsample_ratio = 1/downsample_ratio

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=downsample_ratio, fy=downsample_ratio)

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

    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_small_frame) # model="cnn" 默认hog更快 
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

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

            # Or instead, use the known face with the smallest distance to the new face
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_names[best_match_index]

            face_names.append(name)

    process_this_frame = not process_this_frame


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= upsample_ratio
        right *= upsample_ratio
        bottom *= upsample_ratio
        left *= upsample_ratio

        top = int(top)
        right = int(right)
        bottom = int(bottom)
        left = int(left)

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

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

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

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

猜你喜欢

转载自blog.csdn.net/LEILEI18A/article/details/121424991