COCO
Download

MS-COCO 2014 | Paper
train2014(提取码: jarb)
val2014(提取码: ww94)
test2014(提取码: txwn)
selected_masks(提取码: vqkx)

MS-COCO 2017
2017 Train images 118K/18GB
2017 Val images 5K/1GB
2017 Test images 41K/6GB
2017 Unlabeled images 123K/19GB

WikiArt | Web
WikiArt(提取码: 46cs)

1
2
pip install wget
wget http://images.cocodataset.org/zips/train2017.zip

j2m.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import numpy as np
import cv2
from pycocotools.coco import COCO
from pycocotools import mask as maskUtils
import matplotlib.pyplot as plt

# Paths to the dataset
image_dir = 'val2014'
annotation_file = 'annotations/instances_val2014.json'
output_mask_dir = 'SegmentationClass_select'

# Create the output directory if it doesn't exist
os.makedirs(output_mask_dir, exist_ok=True)

# Initialize COCO API for instance annotations
coco = COCO(annotation_file)

# Get image ids from the dataset
image_ids = coco.getImgIds()

for img_id in image_ids:
# Load image details
img_info = coco.loadImgs(img_id)[0]
img_path = os.path.join(image_dir, img_info['file_name'])

# Read the image (for size reference)
img = cv2.imread(img_path)
height, width, _ = img.shape

# Get all annotations for the current image
ann_ids = coco.getAnnIds(imgIds=img_id)
anns = coco.loadAnns(ann_ids)

# Initialize an empty mask for the current image
mask = np.zeros((height, width), dtype=np.uint8)

for ann in anns:
if 'segmentation' in ann:
if isinstance(ann['segmentation'], list): # Polygon
for seg in ann['segmentation']:
poly = np.array(seg).reshape((len(seg) // 2, 2))
poly = np.round(poly).astype(int)
cv2.fillPoly(mask, [poly], color=1)
else: # RLE
rle = maskUtils.frPyObjects(ann['segmentation'], height, width)
rle_mask = maskUtils.decode(rle)
mask = np.maximum(mask, rle_mask)

# Save the mask image
mask_filename = os.path.splitext(img_info['file_name'])[0] + '.png'
mask_path = os.path.join(output_mask_dir, mask_filename)
cv2.imwrite(mask_path, mask * 255)

print(f"Saved mask for image {img_id} as {mask_filename}")

print("All masks have been generated and saved.")