[mediapipe] Detección en tiempo real de puntos clave del cuerpo humano, puede cambiar el color de los puntos clave y mostrar FPS en tiempo real

Detección en tiempo real de puntos clave humanos

prefacio

El artículo anterior ha realizado la detección de los puntos clave del cuerpo humano en la imagen.Este artículo realizará la detección de los puntos clave del cuerpo humano llamando a la cámara.

Implementación de código (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

Llame a la cámara para obtener cada cuadro, presione q o esc para salir

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()

Implementación de código (2)

La detección de puntos clave del cuerpo humano se realizó anteriormente, y el siguiente código realizará el uso de diferentes colores para diferentes puntos clave y mostrará FPS en tiempo real

#导入库和模型
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)#追踪阈值

Funciones para procesar fotogramas individuales

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

Llame a la cámara, presione q o esc para salir

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()

Supongo que te gusta

Origin blog.csdn.net/qq_64605223/article/details/125607380
Recomendado
Clasificación