Skip to main content

图像分类

图像分类1、模型介绍2、图像分类:图像效果预览3、图像分类:视频效果预览4、图像分类:实时检测4.1、USB摄像头效果预览4.2、CSI摄像头效果预览资料参考

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

1、模型介绍

图像分类是三项任务中最简单的一项,涉及将整幅图像归入一组预定义类别中的某一类。

图像分类器的输出是单一类别标签和置信度分数。当你只需要知道图像属于哪一类,而不需要知道该类对象的位置或确切形状时,图像分类就非常有用。

2、图像分类:图像

使用yolo26n-cls_ncnn_model预测ultralytics26文件夹下的图片(非ultralytics自带图片)。

进入代码文件夹:

cd /home/jetson/ultralytics/yahboom_demo

运行代码:

xxxxxxxxxx python3 04.classification_image.py

效果预览

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

image-20260321190831833

示例代码:

​ x from ultralytics import YOLO ​ # Load a model model = YOLO ( "/home/jetson/ultralytics/yolo26n-cls.engine" ) ​ # Run batched inference on a list of images results = model ( "/home/jetson/ultralytics/assets/bus.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 # Print classification results print ( "Top 1 class:" , probs . top1 ) print ( "Top 5 classes:" , probs . top5 ) print ( "Class names:" , [ model . names [ i ] for i in probs . top5 ]) result . show () # display to screen result . save ( filename = "/home/jetson/ultralytics/output/bus_classification.jpg" ) # save to disk ​

3、图像分类:视频

使用yolo26n-cls_ncnn_model预测ultralytics26文件夹下的视频(非ultralytics自带视频)。

进入代码文件夹:

xxxxxxxxxx cd /home/jetson/ultralytics/yahboom_demo

运行代码:

xxxxxxxxxx python3 04.classification_video.py

效果预览

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

image-20260321190926425

示例代码:

xxxxxxxxxx import cv2 from ultralytics import YOLO ​ # Load the YOLO classification model model = YOLO ( "/home/jetson/ultralytics/yolo26n-cls.engine" ) ​ # 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/output/04.classification_video_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 classification inference on the frame results = model ( frame ) ​ # Get classification results probs = results [ 0 ]. probs top1_class = probs . top1 conf = probs . top1conf . item () class_name = model . names [ top1_class ] # Add classification label to the frame label = f"{class_name}: {conf:.2f}" cv2 . putText ( frame , label , ( 10 , 30 ), cv2 . FONT_HERSHEY_SIMPLEX , 1 , ( 0 , 255 , 0 ), 2 ) ​ # Write the annotated frame to the output video file out . write ( frame ) ​ # Display the annotated frame cv2 . imshow ( "YOLO Classification" , cv2 . resize ( 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摄像头

使用yolo26n-cls_ncnn_model预测USB摄像头画面。

进入代码文件夹:

xxxxxxxxxx cd /home/jetson/ultralytics/yahboom_demo

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

xxxxxxxxxx python3 04.classification_camera_usb.py

效果预览

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

image-20260321191104626

示例代码:

xxxxxxxxxx import cv2 from ultralytics import YOLO ​ # Load the YOLO classification model model = YOLO ( "/home/jetson/ultralytics/yolo26n-cls.engine" ) ​ # Open the camera cap = cv2 . VideoCapture ( 0 ) cap . set ( 6 , cv2 . VideoWriter . fourcc ( 'M' , 'J' , 'P' , 'G' )) cap . set ( cv2 . CAP_PROP_FRAME_WIDTH , 640 ) cap . set ( cv2 . CAP_PROP_FRAME_HEIGHT , 480 ) ​ # 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/output/04.classification_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 classification inference on the frame results = model ( frame ) ​ # Get classification results probs = results [ 0 ]. probs top1_class = probs . top1 conf = probs . top1conf . item () class_name = model . names [ top1_class ] # Add classification label to the frame label = f"{class_name}: {conf:.2f}" cv2 . putText ( frame , label , ( 10 , 30 ), cv2 . FONT_HERSHEY_SIMPLEX , 1 , ( 0 , 255 , 0 ), 2 ) ​ # Write the annotated frame to the output video file out . write ( frame ) ​ # Display the annotated frame cv2 . imshow ( "YOLO Classification" , cv2 . resize ( 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摄像头

使用yolo26n-cls_ncnn_model预测CSI摄像头画面。

进入代码文件夹:

xxxxxxxxxx cd /home/jetson/ultralytics/yahboom_demo

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

xxxxxxxxxx python3 04.classification_camera_csi.py

效果预览

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

image-20260310180912775

示例代码:

xxxxxxxxxx import cv2 from ultralytics import YOLO from jetcam . csi_camera import CSICamera ​ # Load the YOLO classification model model = YOLO ( "/home/jetson/ultralytics/yolo26n-cls.engine" ) ​ # 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/output/04.classification_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 classification inference on the frame results = model ( frame ) ​ # Get classification results probs = results [ 0 ]. probs top1_class = probs . top1 conf = probs . top1conf . item () class_name = model . names [ top1_class ] # Add classification label to the frame label = f"{class_name}: {conf:.2f}" cv2 . putText ( frame , label , ( 10 , 30 ), cv2 . FONT_HERSHEY_SIMPLEX , 1 , ( 0 , 255 , 0 ), 2 ) ​ # Write the annotated frame to the output video file out . write ( frame ) ​ # Display the annotated frame cv2 . imshow ( "YOLO Classification" , cv2 . resize ( 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 () ​

资料参考

图像分类 - Ultralytics YOLO 文档