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)
连个文件json和图片随机压缩代码
最新推荐文章于 2024-07-14 18:05:06 发布