Welcome to ROSMASTER M3 repository
3.Object detection
Object Detection
Note: Using the Docker container in the factory image does not require re-setting up the environment. The environment is already set up. Simply follow the previous tutorial to enter Docker and run the corresponding function commands.
1. Object Prediction: Image
Use yolo11n.pt to predict images provided with the Ultralytics project.
Enter the code folder:
cd /root/ultralytics/ultralytics/yahboom_demo
Run the code:
xxxxxxxxxxpython3 01.detection_image.py

Preview
Yolo recognition output image location:
xxxxxxxxxx/root/ultralytics/ultralytics/output/bus_output.jpg

Sample code:
xxxxxxxxxxfrom ultralytics import YOLO# Load a modelmodel = YOLO("/root/ultralytics/ultralytics/yolo11n.pt")# Run batched inference on a list of imagesresults = model("/root/ultralytics/ultralytics/assets/bus.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="/root/ultralytics/ultralytics/output/bus_output.jpg") 2. Target Prediction: Video
Use yolo11n.pt to predict videos in the Ultralytics project (not included with Ultralytics).
Go to the code folder:
xxxxxxxxxxcd /root/ultralytics/ultralytics/yahboom_demo
Run the code:
xxxxxxxxxxpython3 01.detection_video.py
Preview
Yolo recognition output video location: /root/ultralytics/ultralytics/output/

Sample Code:
xxxxxxxxxximport cv2from ultralytics import YOLO# Load the YOLO modelmodel = YOLO("/root/ultralytics/ultralytics/yolo11n.pt")# Open the video filevideo_path = "/root/ultralytics/ultralytics/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 = "/root/ultralytics/ultralytics/output/01.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", annotated_frame) # 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()3. Target Prediction: Real-Time Detection
3.1. Starting the Camera
Start the following program based on your camera model and enter,
xxxxxxxxxx#USB cameraros2 launch usb_cam camera.launch.py#nuwa cameraros2 launch ascamera hp60c.launch.pyOpen another terminal and navigate to the code folder:
xxxxxxxxxxcd /root/ultralytics/ultralytics/yahboom_demo
Run the code: Click the preview screen and press q to terminate the program!