Skip to main content

模型预测

模型预测1、最佳性能模式1.1、启用MAX模式1.2、启用Jetson时钟2、模型预测2.1、CLI使用2.2、Python使用2.2.1、USB摄像头效果预览2.2.1、CSI摄像头效果预览

1、最佳性能模式

1.1、启用MAX模式

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

sudo nvpmodel -m 2

1.2、启用Jetson时钟

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

xxxxxxxxxx sudo jetson_clocks

2、模型预测

2.1、CLI使用

CLI目前只支持调用USB摄像头,CSI摄像头用户可以直接修改前面的python代码进行onnx和engine模型的调用!

xxxxxxxxxx yolo predict model=best.engine source=0 save=False show # 多个摄像头则跟环source后的数字

image-20250102163340185

2.2、Python使用

使用Python调用USB摄像头和CSI摄像头识别橙子。

2.2.1、USB摄像头

使用best.engine预测摄像头画面:

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

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

xxxxxxxxxx python3 06.orange_camera_usb.py

效果预览

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

image-20250102165136998

示例代码:

​ x import cv2 from ultralytics import YOLO # Load the YOLO model # model = YOLO("/home/jetson/ultralytics/ultralytics/data/yahboom_data/orange_data/runs/detect/train/weights/best.pt") # model = YOLO("/home/jetson/ultralytics/ultralytics/data/yahboom_data/orange_data/runs/detect/train/weights/best.onnx") model = YOLO("/home/jetson/ultralytics/ultralytics/data/yahboom_data/orange_data/runs/detect/train/weights/best.engine") # 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/06.orange_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()

2.2.1、CSI摄像头

使用best.engine预测摄像头画面:

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

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

xxxxxxxxxx python3 06.orange_camera_csi.py

效果预览

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

image-20250102164847286

示例代码:

xxxxxxxxxx import cv2 from ultralytics import YOLO from jetcam.csi_camera import CSICamera # Load the YOLO model # model = YOLO("/home/jetson/ultralytics/ultralytics/data/yahboom_data/orange_data/runs/detect/train/weights/best.pt") # model = YOLO("/home/jetson/ultralytics/ultralytics/data/yahboom_data/orange_data/runs/detect/train/weights/best.onnx") model = YOLO("/home/jetson/ultralytics/ultralytics/data/yahboom_data/orange_data/runs/detect/train/weights/best.engine") # Open the camera (CSI Camera) cap = CSICamera(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/06.orange_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()