小刘总Python实现人脸识别DAY03

该代码示例展示了如何利用Mediapipe库在Python中处理图像,识别并绘制人体的关键点。程序定义了一个Pose类,用于处理图像和计算关节角度。同时,HandleCapture类用于从摄像头捕获视频帧并应用关键点检测。
摘要由CSDN通过智能技术生成

昨天去那边 拿了最后的一点东西,成都算是彻底决断了

"""
当前使用Mediapipe去识别人体的关键点
"""
import cv2
import mediapipe as mp
import util
class Pose:
    def __init__(self):
        mpPose = mp.solutions.pose
        self.pose = mpPose.Pose()
        self.landmark = []
        self.img = None

    def process(self, img):
        self.img = img
        self.drawPointStyle()
        if len(self.landmark) == 0:
            return
        self.armBuildCut()


        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        result = pose.process(imgRGB)
        print(result.pose_landmarks)
        pointStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 255), thickness=2)
        lineStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 255), thickness=2)
        conn = mp.solutions.pose_connections
        mp.solutions.drawing_utils.draw_landmarks(img, result.pose_landmarks, conn.POSE_CONNECTIONS, pointStyle, lineStyle)


        self.drawPointStyle(img)

    def drawPointStyle(self):
        img = self.img
        pose = self.pose
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        result = pose.process(imgRGB)
        if not result.pose_landmarks:
            return
        print(result.pose_landmarks)
        pointStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 255),
                                                        thickness=2)
        lineStyle = mp.solutions.drawing_utils.DrawingSpec(color=(0, 0, 255),
                                                       thickness=2)
        conn = mp.solutions.pose_connections
        mp.solutions.drawing_utils.draw_landmarks(
        img,
        result.pose_landmarks,
        conn.POSE_CONNECTIONS,
        pointStyle,
        lineStyle
    )
    # 绘制关键点的序号
    landmark = result.pose_landmarks.landmark
    imgH, imgW = img.shape[:2]
    for idx, lm in enumerate(landmark):
        x = int(lm.x * imgW)
        y = int(lm.y * imgH)
        cv2.putText(img,
                str(idx),
                org=(x - 15, y - 5),
                fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                fontScale=0.4,
                color=(0, 255, 0),
                thickness=1)

    def armBuildCnt(self):
        point12 = self.indexCvPoint(12)
        point14 = self.indexCvPoint(14)
        point16 = self.indexCvPoint(16)
        angle = util.getAngle(point12, point14, point16)
        if angle < 15:
            print('------')

    def indexCvPoint(self, index):
        imgH, imgW = self.img.shape[:2]
        lm = self.landmark[index]
        x = lm.x * imgW
        y = lm.y * imgH
        return x, y
    
    
import cv2
from pose import Pose
class HandleCapture:
    def __init__(self):
        # 处理肢体关键点
        self.pose = Pose()
    def handelVideoCapture(self):
        # 创建一个视频的捕获器 打开摄像头
        cap = cv2.VideoCapture(0)
        while cap.isOpened():
            retval, frame = cap.read()
            if not retval:
                print('can not read image')
                break
            # 处理肢体关键点
            frame = cv2.flip(frame, 1)
            self.pose.process(frame)
            cv2.imshow('frame', frame)
            key = cv2.waitKey(25)
            if key == ord('q'):
                break
        cap.release()
        cv2.destroyAllWindows()

capture = HandleCapture()
capture.handelVideoCapture()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要用 Python 实现简单的人脸识别,可以使用 OpenCV 和 Dlib 库。 以下是一个简单的人脸识别例子: ```python import cv2 import dlib # 人脸检测器 detector = dlib.get_frontal_face_detector() # 人脸关键点检测器 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 加载已知人脸特征向量 known_face_encodings = [ # TODO: 添加已知人脸的特征向量 ] # 已知人脸的标签 known_face_names = [ # TODO: 添加已知人脸的标签 ] # 打开摄像头 video_capture = cv2.VideoCapture(0) while True: # 读取摄像头图像 ret, frame = video_capture.read() # 转换为灰度图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 检测人脸 faces = detector(gray, 0) # 遍历每一个人脸 for face in faces: # 获取人脸关键点 landmarks = predictor(gray, face) # 提取人脸特征向量 face_encoding = face_recognition.face_encodings(frame, [face])[0] # 在已知人脸库中匹配人脸 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) # 如果有匹配,则显示人脸标签 if True in matches: match_index = matches.index(True) name = known_face_names[match_index] else: name = "Unknown" # 在人脸周围绘制矩形和标签 top, right, bottom, left = face.top(), face.right(), face.bottom(), face.left() cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # 显示图像 cv2.imshow('Video', frame) # 检测键盘输入,按 Q 键退出程序 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头资源 video_capture.release() # 关闭所有窗口 cv2.destroyAllWindows() ``` 需要注意的是,这个例子中使用了已知人脸库进行人脸匹配,需要先构建已知人脸库,获取已知人脸的特征向量和标签。具体实现可以使用 FaceNet 等深度学习模型进行人脸特征提取和匹配。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值