[mediapipe] Real-time detection of key points of the human body, can change the color of key points and display FPS in real time

Real-time detection of human key points

foreword

The previous article has realized the detection of the key points of the human body in the picture. This article will realize the detection of the key points of the human body by calling the camera.

Code implementation (1)

#导入库
import cv2
import mediapipe as mp
from tqdm import tqdm
import time
#导入模型
mp_pose=mp.solutions.pose
mp_drawing=mp.solutions.drawing_utils
pose=mp_pose.Pose(static_image_mode=False,#选择静态图片还是连续视频帧
                 model_complexity=2,#选择人体姿态关键点检测模型,0性能差但快,2性能好但慢,1介于之间
                 smooth_landmarks=True,#是否选择平滑关键点
                 min_detection_confidence=0.5,#置信度阈值
                 min_tracking_confidence=0.5)#追踪阈值
def process_frame(img):
    #BGRRGB
    img_RGB=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    #RGB图像输入模型,获取预测结果
    result=pose.process(img_RGB)
    #可视化
    mp_drawing.draw_landmarks(img,result.pose_landmarks,mp_pose.POSE_CONNECTIONS)
    return img

Call the camera to get each frame, press q or esc to exit

import cv2
import time
#获取摄像头,0表示系统默认摄像头
cap=cv2.VideoCapture(0)
#打开cap
cap.open(0)
#无限循环,直到break
while cap.isOpened():
    #获取画面
    success,frame=cap.read()
    if not success:
        print('Error')
        break
    #处理帧函数
    frame=process_frame(frame)
    #展示处理后的三通道图像
    cv2.imshow('my_window',frame)
    if cv2.waitKey(1) in [ord('q'),27]: #q或esc退出
        break
#关闭摄像头
cap.release()
#关闭图像窗口
cv2.destroyAllWindows()

Code implementation (2)

The detection of the key points of the human body has been realized above, and the following code will realize the use of different colors for different key points, and display the FPS in real time

#导入库和模型
import cv2
import mediapipe as mp
from tqdm import tqdm
import time
mp_pose=mp.solutions.pose
mp_drawing=mp.solutions.drawing_utils
pose=mp_pose.Pose(static_image_mode=False,#选择静态图片还是连续视频帧
                 model_complexity=2,#选择人体姿态关键点检测模型,0性能差但快,2性能好但慢,1介于之间
                 smooth_landmarks=True,#是否选择平滑关键点
                 min_detection_confidence=0.5,#置信度阈值
                 min_tracking_confidence=0.5)#追踪阈值

Functions for processing single frames

def process_frame(img):
    #记录该帧开始处理时间
    start_time=time.time()
    #获取图像宽,高
    h,w=img.shape[0],img.shape[1]
    #BGRRGB
    img_RGB=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    #RGB图像输入模型,获取预测结果
    results=pose.process(img_RGB)
    #若检测出人体关键点
    if results.pose_landmarks:
        mp_drawing.draw_landmarks(img,results.pose_landmarks,mp_pose.POSE_CONNECTIONS)
        for i in range(33):
            cx=int(results.pose_landmarks.landmark[i].x*w)
            cy=int(results.pose_landmarks.landmark[i].y*h)
            cz=results.pose_landmarks.landmark[i].z
            radius=10
            if i==0:#鼻尖
                img=cv2.circle(img,(cx,cy),radius,(0,0,255),-1)
            elif i in [11,12]:#肩膀
                img=cv2.circle(img,(cx,cy),radius,(223,155,6),-1)
            elif i in [23,24]:#髋关节
                img=cv2.circle(img,(cx,cy),radius,(1,240,255),-1)
            elif i in [13,14]:#胳膊肘
                img=cv2.circle(img,(cx,cy),radius,(140,47,240),-1)
            elif i in [25,26]:#膝盖
                img=cv2.circle(img,(cx,cy),radius,(0,0,255),-1)
            elif i in [15,16,27,28]:#手腕和脚腕
                img=cv2.circle(img,(cx,cy),radius,(223,150,60),-1)
            elif i in [17,19,21]:#左手
                img=cv2.circle(img,(cx,cy),radius,(94,218,121),-1)
            elif i in [18,20,22]:#右手
                img=cv2.circle(img,(cx,cy),radius,(16,144,247),-1)
            elif i in [27,29,31]:#左脚
                img=cv2.circle(img,(cx,cy),radius,(29,123,243),-1)
            elif i in [28,30,32]:#右脚
                img=cv2.circle(img,(cx,cy),radius,(193,182,255),-1)
            elif i in [9,10]:#嘴
                img=cv2.circle(img,(cx,cy),radius,(205,235,255),-1)
            elif i in [1,2,3,4,5,6,7,8]:#眼及脸颊
                img=cv2.circle(img,(cx,cy),radius,(94,218,121),-1)
            else:                #其他关键点
                img=cv2.circle(img,(cx,cy),radius,(0,255,0),-1)
    else:
        scaler=1
        failure_str='No Person'
        img=cv2.putText(img,failure_str,(25*scaler,100*scaler),cv2.FONT_HERSHEY_SIMPLEX,1.25*scaler,(255,0,0),3)
    #该帧处理完毕时间
    end_time=time.time()
    #计算每秒处理图像帧数FPS
    FPS=1/(end_time-start_time)
    scaler=1
    #在图像上写FPS数值,参数依次是图片,添加的文字,左上角坐标,字体,字体大小,颜色,字体粗细
    img=cv2.putText(img,'FPS '+str(int(FPS)),(25*scaler,50*scaler),cv2.FONT_HERSHEY_SIMPLEX,1.25*scaler,(255,0,0),3)
    return img

Call the camera, press q or esc to exit

import cv2
import time
#获取摄像头,0表示系统默认摄像头
cap=cv2.VideoCapture(0)
#打开cap
cap.open(0)
#无限循环,直到break
while cap.isOpened():
    #获取画面
    success,frame=cap.read()
    if not success:
        print('Error')
        break
    #处理帧函数
    frame=process_frame(frame)
    #展示处理后的三通道图像
    cv2.imshow('my_window',frame)
    if cv2.waitKey(1) in [ord('q'),27]: #q或esc退出
        break
#关闭摄像头
cap.release()
#关闭图像窗口
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/qq_64605223/article/details/125607380