File size: 2,260 Bytes
66aa7f2 190a9a5 66aa7f2 |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# Run Batch_Inference.py to generate image–mask pairs.
# Use this script to convert the image–mask pairs into a COCO 1.1-compatible JSON annotation file.
# The resulting JSON can be imported into CVAT as annotations.
import os
import cv2
import json
from tqdm import tqdm
from PIL import Image
# Input folders
images_dir = "newimgs/images"
masks_dir = "newimgs/masks"
output_json = "annotations.json"
# COCO format template
coco = {
"images": [],
"annotations": [],
"categories": [{"id": 1, "name": "leaf"}] # change "leaf" if needed
}
ann_id = 1
img_id = 1
for fname in tqdm(os.listdir(images_dir)):
if fname.lower().endswith((".jpg", ".jpeg", ".png")):
name = os.path.splitext(fname)[0]
img_path = os.path.join(images_dir, fname)
mask_path = os.path.join(masks_dir, name + ".png")
if not os.path.exists(mask_path):
print(f" No mask for {fname}, skipping...")
continue
# Load image and mask
img = Image.open(img_path).convert("RGB")
w, h = img.size
mask = cv2.imread(mask_path, 0)
# Ensure binary
_, mask_bin = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
# Add image info
coco["images"].append({
"id": img_id,
"file_name": fname,
"width": w,
"height": h
})
# Extract polygons from mask
contours, _ = cv2.findContours(mask_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
if len(c) < 6: # too few points
continue
segmentation = c.flatten().tolist()
x, y, w_box, h_box = cv2.boundingRect(c)
coco["annotations"].append({
"id": ann_id,
"image_id": img_id,
"category_id": 1,
"segmentation": [segmentation],
"bbox": [x, y, w_box, h_box],
"area": float(cv2.contourArea(c)),
"iscrowd": 0
})
ann_id += 1
img_id += 1
# Save JSON
with open(output_json, "w") as f:
json.dump(coco, f)
print(f"Done. Saved {len(coco['images'])} images and {len(coco['annotations'])} polygons to {output_json}")
|