连个文件json和图片随机压缩代码

import os
import cv2
import numpy as np
import json

def resize_polygon(polygon, resize_factor):
    return [[int(point[0] * resize_factor), int(point[1] * resize_factor)] for point in polygon]

def resize_image(image, target_size):
    return cv2.resize(image, target_size, interpolation=cv2.INTER_LINEAR)

def apply_brightness_contrast(image, brightness_factor, contrast_factor):
    # Explicitly specify the output array type as np.uint8
    image = cv2.convertScaleAbs(image, alpha=contrast_factor, beta=brightness_factor)
    return image

def add_noise(image, noise_level):
    noise = np.random.normal(0, noise_level, image.shape)
    noisy_image = cv2.add(image, noise, dtype=cv2.CV_8U)  # Explicitly specify the output array type
    return np.clip(noisy_image, 0, 255).astype(np.uint8)

def augment_data(image, label_polygon):
    # Randomly adjust image size
    target_size = (np.random.randint(200, 400), np.random.randint(200, 400))
    image = resize_image(image, target_size)

    # Convert polygon coordinates to 32-bit signed integers
    label_polygon = np.array(label_polygon, dtype=np.int32)

    # Create label_mask
    label_mask = np.zeros_like(image, dtype=np.uint8)
    cv2.fillPoly(label_mask, [label_polygon], (255, 255, 255))

    # Randomly adjust brightness and contrast
    brightness_factor = np.random.uniform(0.8, 1.2)
    contrast_factor = np.random.uniform(0.8, 1.2)
    image = apply_brightness_contrast(image, brightness_factor, contrast_factor)

    # Add Gaussian noise
    noise_level = np.random.uniform(0, 30)
    image = add_noise(image, noise_level)

    # Other data augmentation operations...

    return image, label_polygon

def process_images_in_folder(image_folder, json_folder, output_image_folder, output_json_folder):
    for filename in os.listdir(image_folder):
        if filename.endswith(('.jpg', '.jpeg', '.png')):
            image_path = os.path.join(image_folder, filename)
            json_path = os.path.join(json_folder, filename.replace('.jpg', '.json'))
            if os.path.isfile(json_path):
                with open(json_path, 'r') as f:
                    annotation_data = json.load(f)

                image = cv2.imread(image_path)
                label_polygon = annotation_data['shapes'][0]['points']

                # Perform data augmentation
                augmented_image, augmented_label_polygon = augment_data(image, label_polygon)

                # Build paths and file names for saving
                output_filename = filename.replace('.jpg', '_01.jpg')
                output_image_path = os.path.join(output_image_folder, output_filename)
                output_json_path = os.path.join(output_json_folder, output_filename.replace('.jpg', '.json'))

                # Save the augmented image
                cv2.imwrite(output_image_path, augmented_image)

                # Update and save the augmented JSON file
                annotation_data['shapes'][0]['points'] = augmented_label_polygon.tolist()
                with open(output_json_path, 'w') as f:
                    json.dump(annotation_data, f, indent=2)

# Example usage: Replace 'path_to_your_folders' with the actual folder paths
input_image_folder = 'F:\\test_file_data_zengqiang\\input\\2023-11-17-640\\images'
input_json_folder = 'F:\\test_file_data_zengqiang\\input\\2023-11-17-640\\json'
output_image_folder = 'F:\\test_file_data_zengqiang\\output\\images'
output_json_folder = 'F:\\test_file_data_zengqiang\\output\\json'
process_images_in_folder(input_image_folder, input_json_folder, output_image_folder, output_json_folder)
  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值