python简单实现人脸检测/跟随

import cv2

# 加载人脸识别器的模型
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# 打开摄像头
cap = cv2.VideoCapture(0)

# 初始化人脸框位置
prev_faces = []

# 定义绘制带圆角矩形边框的函数
def draw_rounded_rectangle(img, x, y, w, h, radius, color, thickness):
    # 画上面的边框
    cv2.line(img, (x + radius, y), (x + w - radius, y), color, thickness)
    # 画下面的边框
    cv2.line(img, (x + radius, y + h), (x + w - radius, y + h), color, thickness)
    # 画左边的边框
    cv2.line(img, (x, y + radius), (x, y + h - radius), color, thickness)
    # 画右边的边框
    cv2.line(img, (x + w, y + radius), (x + w, y + h - radius), color, thickness)
    # 画左上角的圆弧
    cv2.ellipse(img, (x + radius, y + radius), (radius, radius), 180, 0, 90, color, thickness)
    # 画右上角的圆弧
    cv2.ellipse(img, (x + w - radius, y + radius), (radius, radius), 270, 0, 90, color, thickness)
    # 画右下角的圆弧
    cv2.ellipse(img, (x + w - radius, y + h - radius), (radius, radius), 0, 0, 90, color, thickness)
    # 画左下角的圆弧
    cv2.ellipse(img, (x + radius, y + h - radius), (radius, radius), 90, 0, 90, color, thickness)
# 定义平滑处理人脸位置的函数
def smooth_faces(faces, prev_faces=None, alpha=0):
    if prev_faces is None or len(prev_faces) == 0:
        return faces

    smoothed_faces = []
    for (x, y, w, h) in faces:
        found = False
        for (prev_x, prev_y, prev_w, prev_h) in prev_faces:
            if x - 30 <= prev_x <= x + 30 and y - 30 <= prev_y <= y + 30:
                smoothed_x = int(alpha * x + (1 - alpha) * prev_x)
                smoothed_y = int(alpha * y + (1 - alpha) * prev_y)
                smoothed_w = int(alpha * w + (1 - alpha) * prev_w)
                smoothed_h = int(alpha * h + (1 - alpha) * prev_h)
                smoothed_faces.append((smoothed_x, smoothed_y, smoothed_w, smoothed_h))
                found = True
                break

        if not found:
            smoothed_faces.append((x, y, w, h))

    return smoothed_faces

# 连续未检测到人脸的帧计数器
no_face_count = 0
max_no_face_count = 100  # 连续未检测到人脸的最大帧数

while True:
    # 读取摄像头图像
    ret, frame = cap.read()

    if not ret:
        # 如果无法读取摄像头图像,则退出循环
        break

    # 将图像转换为灰度图像
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # 检测人脸
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
    # 平滑处理人脸位置
    faces = list(faces)
    faces = smooth_faces(faces, prev_faces, alpha=0.3)
    prev_faces = faces.copy()

    # 在图像上绘制带圆角矩形边框
    if len(faces) > 0:
        for (x, y, w, h) in faces:
            # 绘制带圆角矩形边框
            radius = int(min(w, h) / 10)
            draw_rounded_rectangle(frame, x, y, w, h, radius, (18, 153, 255), 2)
            # 在人脸框上方显示文本信息
            text = "Face"
            font = cv2.FONT_HERSHEY_SIMPLEX
            font_scale = 0.8
            thickness = 2
            text_size, _ = cv2.getTextSize(text, font, font_scale, thickness)
            text_x = x + int((w - text_size[0]) / 2)
            text_y = y - 10
            cv2.putText(frame, text, (text_x, text_y), font, font_scale, (18, 153, 255), thickness, cv2.LINE_AA)
    # 显示图像
    cv2.imshow('Face Detection', frame)

    # 按下ESC键退出循环
    if cv2.waitKey(1) == 27:
        break

# 释放摄像头和关闭窗口
cap.release()
cv2.destroyAllWindows()



在这里插入图片描述

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Python 进行摄像头人脸识别,你可以使用 OpenCV 和 face_recognition 库。 首先,你需要安装这两个库。你可以使用 pip 安装它们: ``` pip install opencv-python pip install face_recognition ``` 接下来,你需要编写 Python 代码来实现摄像头人脸识别。以下是一个简单的示例代码: ```python import cv2 import face_recognition # 加载已知人脸照片 known_image = face_recognition.load_image_file("known_face.jpg") known_encoding = face_recognition.face_encodings(known_image)[0] # 初始化摄像头 video_capture = cv2.VideoCapture(0) while True: # 读取摄像头画面 ret, frame = video_capture.read() # 检测人脸 face_locations = face_recognition.face_locations(frame) face_encodings = face_recognition.face_encodings(frame, face_locations) # 遍历每张识别到的人脸 for face_encoding, face_location in zip(face_encodings, face_locations): # 比较人脸照片和摄像头画面中的人脸 match = face_recognition.compare_faces([known_encoding], face_encoding) if match[0]: # 识别到已知人脸 top, right, bottom, left = face_location cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) # 显示摄像头画面 cv2.imshow('Video', frame) # 按下 q 键退出程序 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头并关闭窗口 video_capture.release() cv2.destroyAllWindows() ``` 在上面的代码中,我们首先加载了一个已知人脸的照片,并从中提取了人脸编码。然后,我们初始化了摄像头,从中读取每一帧画面,并使用 face_recognition 库检测人脸,并提取人脸编码。接下来,我们将人脸编码与已知人脸的编码进行比较,并在识别到已知人脸时,在摄像头画面中框出人脸。最后,我们显示摄像头画面,并等待用户按下 q 键退出程序。 你可以根据自己的需求修改代码,并使用更高级的算法来进行人脸识别

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值