跳到主要内容

姿态估计

姿态估计1、启用主板最佳性能1.1、启用MAX功率模式1.2、启用Jetson时钟2、姿态估计:图像效果预览3、姿态估计:视频效果预览4、姿态估计:实时检测4.1、USB摄像头效果预览4.2、CSI摄像头效果预览资料参考

使用Python演示Ultralytics :Pose Estimation在图像、视频、实时检测的效果。

1、启用主板最佳性能

1.1、启用MAX功率模式

在Jetson上启用 MAX Power Mode(最大功率模式)将确保打开所有CPU 、GPU 内核:

xxxxxxxxxx sudo nvpmodel -m 2

1.2、启用Jetson时钟

启用Jetson Clocks将确保所有CPU,GPU内核都以最大频率运行:

xxxxxxxxxx sudo jetson_clocks

2、姿态估计:图像

使用yolo11n-pose.pt预测ultralytics项目下的图片(非ultralytics自带图片)。

进入代码文件夹:

cd /home/jetson/ultralytics/ultralytics/yahboom_demo

运行代码:

x python3 03.pose_image.py

效果预览

yolo识别输出的图片位置:/home/jetson/ultralytics/ultralytics/output/

image-20241230170043997

示例代码:

​ x from ultralytics import YOLO # Load a model model = YOLO ( "/home/jetson/ultralytics/ultralytics/yolo11n-pose.pt" ) # Run batched inference on a list of images results = model ( "/home/jetson/ultralytics/ultralytics/assets/people.jpg" ) # return a list of Results objects # Process results list for result in results : # boxes = result.boxes # Boxes object for bounding box outputs # masks = result.masks # Masks object for segmentation masks outputs keypoints = result . keypoints # Keypoints object for pose outputs # probs = result.probs # Probs object for classification outputs # obb = result.obb # Oriented boxes object for OBB outputs result . show () # display to screen result . save ( filename = "/home/jetson/ultralytics/ultralytics/output/people_output.jpg" ) # save to disk

3、姿态估计:视频

使用yolo11n-pose.pt预测ultralytics项目下的视频(非ultralytics自带视频)。

进入代码文件夹:

xxxxxxxxxx cd /home/jetson/ultralytics/ultralytics/yahboom_demo

运行代码:

x python3 03.pose_video.py

效果预览

yolo识别输出的视频位置:/home/jetson/ultralytics/ultralytics/output/

image-20241230170512899

示例代码:

xxxxxxxxxx import cv2 from ultralytics import YOLO # Load the YOLO model model = YOLO ( "/home/jetson/ultralytics/ultralytics/yolo11n-pose.pt" ) # Open the video file video_path = "/home/jetson/ultralytics/ultralytics/videos/people_animals.mp4" cap = cv2 . VideoCapture ( video_path ) # Get the video frame size and frame rate frame_width = int ( cap . get ( cv2 . CAP_PROP_FRAME_WIDTH )) frame_height = int ( cap . get ( cv2 . CAP_PROP_FRAME_HEIGHT )) fps = int ( cap . get ( cv2 . CAP_PROP_FPS )) # Define the codec and create a VideoWriter object to output the processed video output_path = "/home/jetson/ultralytics/ultralytics/output/03.people_animals_output.mp4" fourcc = cv2 . VideoWriter_fourcc ( * 'mp4v' ) # You can use 'XVID' or 'mp4v' depending on your platform out = cv2 . VideoWriter ( output_path , fourcc , fps , ( frame_width , frame_height )) # Loop through the video frames while cap . isOpened (): # Read a frame from the video success , frame = cap . read () if success : # Run YOLO inference on the frame results = model ( frame ) # Visualize the results on the frame annotated_frame = results [ 0 ]. plot () # Write the annotated frame to the output video file out . write ( annotated_frame ) # Display the annotated frame cv2 . imshow ( "YOLO Inference" , cv2 . resize ( annotated_frame , ( 640 , 480 ))) # Break the loop if 'q' is pressed if cv2 . waitKey ( 1 ) & 0xFF == ord ( "q" ): break else : # Break the loop if the end of the video is reached break # Release the video capture and writer objects, and close the display window cap . release () out . release () cv2 . destroyAllWindows ()

4、姿态估计:实时检测

4.1、USB摄像头

使用yolo11n-pose.pt预测USB摄像头画面。

进入代码文件夹:

xxxxxxxxxx cd /home/jetson/ultralytics/ultralytics/yahboom_demo

运行代码:点击预览画面,按q键可以终止程序!

x python3 03.pose_camera_usb.py

效果预览

yolo识别输出的视频位置:/home/jetson/ultralytics/ultralytics/output/

image-20241230171319586

示例代码:

xxxxxxxxxx import cv2 from ultralytics import YOLO # Load the YOLO model model = YOLO ( "/home/jetson/ultralytics/ultralytics/yolo11n-pose.pt" ) # Open the cammera cap = cv2 . VideoCapture ( 0 ) # Get the video frame size and frame rate frame_width = int ( cap . get ( cv2 . CAP_PROP_FRAME_WIDTH )) frame_height = int ( cap . get ( cv2 . CAP_PROP_FRAME_HEIGHT )) fps = int ( cap . get ( cv2 . CAP_PROP_FPS )) # Define the codec and create a VideoWriter object to output the processed video output_path = "/home/jetson/ultralytics/ultralytics/output/03.pose_camera_usb.mp4" fourcc = cv2 . VideoWriter_fourcc ( * 'mp4v' ) # You can use 'XVID' or 'mp4v' depending on your platform out = cv2 . VideoWriter ( output_path , fourcc , fps , ( frame_width , frame_height )) # Loop through the video frames while cap . isOpened (): # Read a frame from the video success , frame = cap . read () if success : # Run YOLO inference on the frame results = model ( frame ) # Visualize the results on the frame annotated_frame = results [ 0 ]. plot () # Write the annotated frame to the output video file out . write ( annotated_frame ) # Display the annotated frame cv2 . imshow ( "YOLO Inference" , cv2 . resize ( annotated_frame , ( 640 , 480 ))) # Break the loop if 'q' is pressed if cv2 . waitKey ( 1 ) & 0xFF == ord ( "q" ): break else : # Break the loop if the end of the video is reached break # Release the video capture and writer objects, and close the display window cap . release () out . release () cv2 . destroyAllWindows ()

4.2、CSI摄像头

使用yolo11n-pose.pt预测CSI摄像头画面。

进入代码文件夹:

xxxxxxxxxx cd /home/jetson/ultralytics/ultralytics/yahboom_demo

运行代码:点击预览画面,按q键可以终止程序!

x python3 03.pose_camera_csi.py

效果预览

yolo识别输出的视频位置:/home/jetson/ultralytics/ultralytics/output/

image-20241230171927586

示例代码:

xxxxxxxxxx import cv2 from ultralytics import YOLO from jetcam.csi_camera import CSICamera # Load the YOLO model model = YOLO("/home/jetson/ultralytics/ultralytics/yolo11n-pose.pt") # Open the camera (CSI Camera) cap = CSICamera(capture_device=0, width=640, height=480) # Get the video frame size and frame rate frame_width = 640 frame_height = 480 fps = 30 # Define the codec and create a VideoWriter object to output the processed video output_path = "/home/jetson/ultralytics/ultralytics/output/03.pose_camera_csi.mp4" fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can use 'XVID' or 'mp4v' depending on your platform out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height)) # Loop through the video frames while True: # Read a frame from the camera frame = cap.read() if frame is not None: # Run YOLO inference on the frame results = model(frame) # Visualize the results on the frame annotated_frame = results[0].plot() # Write the annotated frame to the output video file out.write(annotated_frame) # Display the annotated frame cv2.imshow("YOLO Inference", cv2.resize(annotated_frame, (640, 480))) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord("q"): break else: # Break the loop if no frame is received (camera error or end of stream) print("No frame received, breaking the loop.") break # Release the video capture and writer objects, and close the display window cap.release() out.release() cv2.destroyAllWindows()

资料参考

https://docs.ultralytics.com/tasks/pose/