如何使用json数据进行掩码来训练u网络模型?

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

我想用json格式屏蔽我的图像作为u net训练模型的数据。 我使用下面的代码来掩盖它们:


import json
import numpy as np
import cv2
import os

# Path to the folder containing JSON files
json_folder = os.path.expanduser('~/Desktop/jeson')  # Folder with JSON files
# Path to the folder for saving mask images
mask_folder = os.path.expanduser('~/Desktop/masks')  # Folder to save masks

# Ensure the folder for saving masks exists
os.makedirs(mask_folder, exist_ok=True)

# List all JSON files in the folder
for filename in os.listdir(json_folder):
    if filename.endswith('.json'):
        json_file = os.path.join(json_folder, filename)

        # Load data from the JSON file
        with open(json_file) as f:
            data = json.load(f)

        # Create an empty mask
        mask = np.zeros((data['imageHeight'], data['imageWidth']), dtype=np.uint8)

        # Add regions to the mask
        for shape in data['shapes']:
            points = np.array(shape['points'], dtype=np.int32)
            if len(points) > 0:
                # Fill the area defined by the points with white color
                cv2.fillPoly(mask, [points], 49)

        # Save the mask as a PNG image using OpenCV
        mask_filename = os.path.splitext(filename)[0] + '_mask.png'
        cv2.imwrite(os.path.join(mask_folder, mask_filename), mask)

print("Conversion completed, and mask images have been saved in the 'masks' folder!")


但是有一个有趣的问题。它只掩盖了其中的一些,一开始很好,但其他的只用一条细线掩盖。当我再次尝试使用此代码时,它用一条细线掩盖了所有这些代码。

例如在 labelme 工具中处理之前的图像: 我的图像在用lamelme处理之前

例如屏蔽的 jeson 数据: 杰森蒙面

我该如何解决这个问题?

data-annotations unet-neural-network instance-segmentation
1个回答
0
投票

我无法解决使用 JESON 数据屏蔽准确的问题。我尝试使用打开的 CV 并创建多边形来掩盖我的原始图像,因此我决定使用此脚本作为我的时间和准确性项目要求:


    import cv2
import os
import time

# Path to the folder containing red images
red_folder = os.path.expanduser('~/Desktop/red')

# Path to the folder for saving masked images
mask_folder = os.path.expanduser('~/Desktop/q')

# Ensure the folder for saving masks exists
os.makedirs(mask_folder, exist_ok=True)

# List all image files in the folder
image_files = [f for f in os.listdir(red_folder) if f.endswith(('.jpg', '.png', '.jpeg'))]

# Iterate over each image
for i, image_file in enumerate(image_files):
    # Read the image
    image_path = os.path.join(red_folder, image_file)
    image = cv2.imread(image_path)
    
    # Display the original image for debugging
    cv2.imshow("Original Image", image)
    cv2.waitKey(0)  # Wait for a key press
    cv2.destroyAllWindows()  # Close the window

    # Create binary mask for red regions
    mask1 = cv2.inRange(image, (0, 0, 100), (100, 100, 255))  # Bright red
    mask2 = cv2.inRange(image, (0, 100, 100), (100, 255, 255))  # Dark red
    mask = cv2.bitwise_or(mask1, mask2)  # Combine both masks

    # Display the mask for debugging
    cv2.imshow("Mask", mask)
    cv2.waitKey(0)  # Wait for a key press
    cv2.destroyAllWindows()  # Close the window

    # Save the masked image
    mask_filename = f"mask_{image_file}"
    mask_path = os.path.join(mask_folder, mask_filename)
    cv2.imwrite(mask_path, mask)
    
    print(f"Processed image {i+1}/{len(image_files)}: {mask_filename}")
    
    # Wait for 2 seconds
    time.sleep(2)


然而,当红色物体较多时,这种方法在遮蔽方面存在挑战。此代码将这些添加到掩码并降低质量。

© www.soinside.com 2019 - 2024. All rights reserved.