import os import cv2 import numpy as np import json import base64 def resize_polygon(polygon, resize_factor): return [[int(point[0] * resize_factor), int(point[1] * resize_factor)] for point in polygon] def resize_image_and_update_json(image, max_dimension, annotation_data): # Calculate the resize factor while maintaining the aspect ratio height, width = image.shape[:2] aspect_ratio = width / height if width > height: new_width = max_dimension new_height = int(max_dimension / aspect_ratio) else: new_height = max_dimension new_width = int(max_dimension * aspect_ratio) # Resize the image augmented_image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_LINEAR) # Update image height and width in the JSON data annotation_data['imageHeight'] = augmented_image.shape[0] annotation_data['imageWidth'] = augmented_image.shape[1] # Encode the resized image to base64 and update "imageData" field _, encoded_image = cv2.imencode('.jpg', augmented_image) print(encoded_image.shape) annotation_data['imageData'] = base64.b64encode(encoded_image).decode('utf-8') # Resize polygon coordinates label_polygon = annotation_data['shapes'][0]['points'] augmented_label_polygon = resize_polygon(label_polygon, max(new_width / width, new_height / height)) return augmented_image, augmented_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): try: with open(json_path, 'r', encoding='utf-8') as f: annotation_data = json.load(f) # Check if 'shapes' list is not empty if 'shapes' in annotation_data and annotation_data['shapes']: label_polygon = annotation_data['shapes'][0]['points'] image = cv2.imread(image_path) # Perform data augmentation and update JSON augmented_image, augmented_label_polygon = resize_image_and_update_json(image, 640, annotation_data) # 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 with open(output_json_path, 'w') as f: json.dump(annotation_data, f, indent=2) else: print(f"Warning: 'shapes' list is empty or does not exist in {json_path}") except json.decoder.JSONDecodeError as e: print(f"Error reading JSON file {json_path}: {e}") continue # Example usage: Replace 'path_to_your_folders' with the actual folder paths input_image_folder = 'E:\\nai_liang_ceshitupian\\2023-12-18' input_json_folder = 'E:\\nai_liang_ceshitupian\\2023-12-18-json' output_image_folder = 'E:\\nai_liang_ceshitupian\\2023-12-18-640' output_json_folder = 'E:\\nai_liang_ceshitupian\\2023-12-18-640_json' process_images_in_folder(input_image_folder, input_json_folder, output_image_folder, output_json_folder)
压缩图片json文件也被压缩代码
最新推荐文章于 2024-07-14 18:05:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)