# Draw contours on the original image cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
# Get bounding boxes of contours bboxes = [] for contour in contours: x, y, w, h = cv2.boundingRect(contour) right = x + w lower = y + h left = x upper = y bboxes.append([left, upper, right, lower])
# Open the image for further processing image = Image.open("000000000885.jpg")
# Define the target box coordinates as a tuple box_coords = (100, 100, 300, 300)
# Extract the target box as a new image target_image = image.crop(box_coords)
# Assume you have obtained the target class, bounding box, and segmentation label information and stored them in respective variables
# Create an empty list to store the entry for each object annotations_list = []
# For each object, create a dictionary entry and add it to the list num_objects = 3 image_id = 885 category_ids = ["human", "dog", "cat"] # use list instead of string segmentations = ["person", "dog", "cat"] # use list instead of string for i inrange(num_objects): annotation_dict = { 'image_id': image_id, 'category_id': category_ids[i], 'bbox': bboxes[i], 'segmentation': segmentations[i] } annotations_list.append(annotation_dict)
# Create the final dictionary containing keys such as 'image_ids', 'category_ids', 'bboxes', and 'segmentations', and store it as a list annotations_dict = { 'image_ids': [image_id], 'category_ids': [ann['category_id'] for ann in annotations_list], 'bboxes': [ann['bbox'] for ann in annotations_list], 'segmentations': [ann['segmentation'] for ann in annotations_list] }
# Open the file for writing withopen("annotations.txt", "w") as f: # Write the dictionary to the file f.write(str(annotations_dict))
# Close the file f.close()
########################################
# Define functions to get image and category information defget_image_info('image_id'): # Implementation of getting image info pass
defget_categories(): # Implementation of getting categories pass
# Load the image using PIL image_path = '000000000885.jpg' image = Image.open(image_path)
# Get the image id, bounding boxes, and segmentation masks image_id = 885 image_info = get_image_info(image_id) categories = get_categories()
import matplotlib.pyplot as plt from matplotlib.patches import Rectangle
# Create a plot with the image and bounding boxes fig, ax = plt.subplots() ax.imshow(image)
for i inrange(len(image_info['bboxes'])): bbox = image_info['bboxes'][i] category_id = image_info['category_ids'][i] category_name = categories[category_id]['name'] seg_mask = image_info['segmentations'][i]
# Add a rectangle for the bounding box rect = Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], linewidth=1, edgecolor='r', facecolor='none') ax.add_patch(rect)
# Add the category name as text next to the bounding box ax.text(bbox[0], bbox[1], category_name, fontsize=8, color='r')
import os import requests from PIL import Image import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from pycocotools.coco import COCO
for img_id in coco.imgs: img_info = coco.loadImgs(ids=img_id)[0] url = img_info['coco_url'] response = requests.get(url) if response.status_code == 200: withopen(os.path.join(output_dir, f'{img_id}.jpg'), 'wb') as f: f.write(response.content)
# 任务3: 转换数据格式 deftransform_data(coco): transformed_data = {} for img_id in coco.imgs: ann_ids = coco.getAnnIds(imgIds=img_id) annotations = coco.loadAnns(ids=ann_ids) transformed_data[img_id] = {'image_ids': [img_id], 'category_ids': [ann['category_id'] for ann in annotations], 'bboxes': [ann['bbox'] for ann in annotations], 'segmentations': [ann['segmentation'] for ann in annotations]} return transformed_data