jetson nano face_recognition 人脸识别代码

path中存放人脸数据 并以名字命名

import face_recognition
import cv2
import os
import numpy as np


def face_encode(img, model='large'):
    points = face_recognition.face_locations(img, model='cnn')
    encodeing = face_recognition.face_encodings(img, points, model=model)
    return points, encodeing


path = r'./root_face'    
files = os.listdir(path)
face_dict = {}
root_face = []
index = 0
for file in files:
    file_path = os.path.join(path, file)
    img = cv2.imread(file_path)
    w, h = img.shape[1::-1]
    ratio = w / h
    h = 400
    img = cv2.resize(img, (int(h * ratio), h))
    _, encodeing = face_encode(img)
    face_dict['{}'.format(index)] = '{}'.format(file.split('.')[0])
    root_face.append(encodeing[0])
    index += 1
 
def gstreamer_pipeline(
    capture_width=1280,
    capture_height=720,
    display_width=1280,
    display_height=720,
    framerate=24,
    flip_method=0,
):
    return (
        "nvarguscamerasrc ! "
        "video/x-raw(memory:NVMM), "
        "width=(int)%d, height=(int)%d, "
        "format=(string)NV12, framerate=(fraction)%d/1 ! "
        "nvvidconv flip-method=%d ! "
        "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
        "videoconvert ! "
        "video/x-raw, format=(string)BGR ! appsink"
        % (
            capture_width,
            capture_height,
            framerate,
            flip_method,
            display_width,
            display_height,
        )
    )
 
 
def show_camera():
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)

 
    while cap.isOpened():
        flag, frame = cap.read()
        frame_count = cap.get(cv2.CAP_PROP_FPS)
        print(frame_count)
        w, h = frame.shape[1::-1]
        ratio = w / h
        h = 150
        frame = cv2.resize(frame, (int(h * ratio), h))
        points, encodeings = face_encode(frame)
        frame_count = cap.get(cv2.CAP_PROP_FPS)
        print(frame_count)
        for pts, encodeing in zip(points, encodeings):
            cv2.rectangle(frame, tuple((pts[1], pts[0])), tuple((pts[3], pts[2])), (0, 255, 0))
            face_distances = face_recognition.face_distance(root_face, encodeing)
            if np.min(face_distances ) < 0.4:
                continue
            best_match_index = np.argmin(face_distances)
            str1 = face_dict['{}'.format(best_match_index)]
            cv2.putText(frame, str1, tuple((pts[1], pts[0])), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 255, 255), 2)

        cv2.imshow("windowName", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        k = cv2.waitKey()
        if k == 27:
            break
        if cv2.getWindowProperty('windowName', cv2.WND_PROP_AUTOSIZE) < 1:
            break
    cap.release()
    cv2.destroyAllWindows()
    
 
if __name__ == "__main__":
    show_camera()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徒手写bug326

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值