Welcome to Raspberry Pi 5 repository
04.Instance segmentation
Instance Segmentation
Instance Segmentation1. Model Introduction2. Instance Segmentation: ImagePreview3. Instance Segmentation: VideoPreview4. Instance Segmentation: Real-time Detection4.1 USB CameraPreview4.2 CSI CameraPreviewReferences
Demonstrate Ultralytics using Python: Instance Segmentation effects on images, videos, and real-time detection.
1. Model Introduction
Instance segmentation goes a step further than object detection. It involves identifying individual objects in an image and segmenting them from the rest of the image.
The output of an instance segmentation model is a set of masks or contours that outline each object in the image, along with class labels and confidence scores for each object. Instance segmentation is very useful when you not only need to know where objects are in the image, but also their specific shapes.
2. Instance Segmentation: Image
Use yolo26n-seg_ncnn_model to predict images in the ultralytics26 folder.
Enter the code folder:
xxxxxxxxxxcd /home/pi/ultralytics26/yahboom_demoRun the code:
xxxxxxxxxxpython3 02.segmentation_image.pyPreview
yolo recognition output image location: /home/pi/ultralytics26/output/

Example code:
xxxxxxxxxxfrom ultralytics import YOLO# Load a modelmodel = YOLO("/home/pi/ultralytics26/yolo26n-seg_ncnn_model")# model = YOLO("/home/pi/ultralytics26/yolo26n-seg.pt")# Run batched inference on a list of imagesresults = model("/home/pi/ultralytics26/assets/zidane.jpg") # return a list of Results objects# Process results listfor 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/pi/ultralytics26/output/zidane_output.jpg") # save to disk3. Instance Segmentation: Video
Use yolo26n-seg_ncnn_model to predict videos in the ultralytics26 folder (not videos that come with ultralytics).
Enter the code folder:
xxxxxxxxxxcd /home/pi/ultralytics26/yahboom_demoRun the code:
xxxxxxxxxxpython3 02.segmentation_video.pyPreview
yolo recognition output video location: /home/pi/ultralytics26/ultralytics/output/

Example code:
xxxxxxxxxximport cv2from ultralytics import YOLO# Load the YOLO modelmodel = YOLO("/home/pi/ultralytics26/yolo26n-seg_ncnn_model")# model = YOLO("/home/pi/ultralytics26/yolo26n-seg.pt")# Open the video filevideo_path = "/home/pi/ultralytics26/videos/people_animals.mp4"cap = cv2.VideoCapture(video_path)# Get the video frame size and frame rateframe_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 videooutput_path = "/home/pi/ultralytics26/output/02.people_animals_output.mp4"fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can use 'XVID' or 'mp4v' depending on your platformout = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))# Loop through the video frameswhile 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 windowcap.release()out.release()cv2.destroyAllWindows()4. Instance Segmentation: Real-time Detection
4.1 USB Camera
Use yolo26n-seg_ncnn_model to predict USB camera footage.
Enter the code folder:
xxxxxxxxxxcd /home/pi/ultralytics26/yahboom_demoRun the code:
xxxxxxxxxxpython3 02.segmentation_camera_usb.pyPreview
yolo recognition output video location: /home/pi/ultralytics26/output/

Example code:
xxxxxxxxxximport cv2from ultralytics import YOLO# Load the YOLO modelmodel = YOLO("/home/pi/ultralytics26/yolo26n-seg_ncnn_model")# model = YOLO("/home/pi/ultralytics26/yolo26n-seg.pt")# Open the cameracap = 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 rateframe_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 videooutput_path = "/home/pi/ultralytics26/output/02.segmentation_camera_usb.mp4"fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can use 'XVID' or 'mp4v' depending on your platformout = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))# Loop through the video frameswhile 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 windowcap.release()out.release()cv2.destroyAllWindows()4.2 CSI Camera
Use yolo26n-seg_ncnn_model to predict CSI camera footage.
Enter the code folder:
xxxxxxxxxxcd /home/pi/ultralytics26/yahboom_demoRun the code:
xxxxxxxxxxpython3 02.segmentation_camera_csi.pyPreview
yolo recognition output video location: /home/pi/ultralytics26/output/

Example code:
xxxxxxxxxximport cv2from picamera2 import Picamera2from ultralytics import YOLO# Initialize the Picamera2picam2 = Picamera2()picam2.preview_configuration.main.size = (640, 480)picam2.preview_configuration.main.format = "RGB888"picam2.preview_configuration.align()picam2.configure("preview")picam2.start()# Load the YOLO modelmodel = YOLO("/home/pi/ultralytics26/yolo26n-seg_ncnn_model")# model = YOLO("/home/pi/ultralytics26/yolo26n-seg.pt")# Set up video outputoutput_path = "/home/pi/ultralytics26/output/02.segmentation_camera_csi.mp4"fourcc = cv2.VideoWriter_fourcc(*'mp4v')out = cv2.VideoWriter(output_path, fourcc, 30, (640, 480))# Loop through the video frameswhile True: # Capture frame-by-frame frame = picam2.capture_array() # Run YOLO11 inference on the frame results = model(frame) # Visualize the results on the frame annotated_frame = results[0].plot() # Write the frame to the video file out.write(annotated_frame) # Display the resulting frame cv2.imshow("Camera", annotated_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) == ord("q"): break# Release resources and close windowspicam2.close()out.release()cv2.destroyAllWindows()