from ultralytics import YOLO
# Load a model
model = YOLO("yolo11n.pt") # pretrained YOLO11n model
# Run batched inference on a list of images
results = model(["image1.jpg", "image2.jpg"], stream=True) # return a generator of Results objects
# Process results generator
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="result.jpg") # save to disk
YOLO11 可以处理不同类型的输入源进行推理,如下表所示。输入源包括静态图像、视频流和各种数据格式。表中还标明了每种输入源是否可以在流模式下使用参数 stream=True
✅.流模式有利于处理视频或实时流,因为它会创建一个结果生成器,而不是将所有帧加载到内存中。
使用 stream=True
用于处理长视频或大型数据集,以有效管理内存。当 stream=False
在这种情况下,所有帧或数据点的结果都会存储在内存中,这可能会迅速累加,并导致大量输入出现内存不足错误。与此形成鲜明对比的是 stream=True
利用生成器,只将当前帧或数据点的结果保存在内存中,从而大大减少了内存消耗,并防止出现内存不足的问题。
model = YOLO("yolo11n.pt")
# Create a random numpy array of HWC shape (640, 640, 3) with values in range [0, 255] and type uint8
source = np.random.randint(low=0, high=255, size=(640, 640, 3), dtype="uint8")
# Run inference on the source
results = model(source) # list of Results objects
对表示为 PyTorchtensor.