如何裁剪出注释框及其中的所有内容?

问题描述 投票:0回答:0

运行yolov8后算法标注如下图:Density-Area

我的目标是裁剪出大量这些图片以用于进一步分析。所以,我希望保存边界框内的所有内容,并删除边界框之外的所有内容。

我尝试使用 torch、numpy、cv2 和 PIL,但没有成功。

import torch
import torchvision
from PIL import Image

# Load the image
image = Image.open("path to .jpg")

# Define the model and download the pre-trained weights
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True, weights=None)

# Set the model to evaluation mode
model.eval()

# Transform the image to a tensor
transform = torchvision.transforms.ToTensor()
image_tensor = transform(image)

# Make predictions on the image using the model
predictions = model([image_tensor])

# Extract the bounding boxes and object labels from the predictions
boxes = predictions[0]['boxes'].tolist()
labels = predictions[0]['labels'].tolist()

# Crop the image for each object detected
for i in range(len(boxes)):
    bbox = tuple(boxes[i])
    object_label = labels[i]
    object_image = image.crop(bbox)
    object_image.save(f"image_save.jpg")
    
python numpy pytorch python-imaging-library cv2
© www.soinside.com 2019 - 2024. All rights reserved.