import os.path import time from datetime import datetime, timedelta from pathlib import Path import albumentations as A import cv2 import numpy as np from constants import (data_images_dir, data_labels_dir, processed_images_dir, processed_labels_dir, annotation_classes, checkpoint_file, checkpoint_date_format) from dto.imageLabel import ImageLabel def image_processing(img_ann: ImageLabel) -> [ImageLabel]: transforms = [ A.Compose([A.HorizontalFlip(always_apply=True)], bbox_params=A.BboxParams(format='yolo')), A.Compose([A.RandomBrightnessContrast(always_apply=True)], bbox_params=A.BboxParams(format='yolo')), A.Compose([A.SafeRotate(limit=90, always_apply=True)], bbox_params=A.BboxParams(format='yolo')), A.Compose([A.SafeRotate(limit=90, always_apply=True), A.RandomBrightnessContrast(always_apply=True)], bbox_params=A.BboxParams(format='yolo')), A.Compose([A.ShiftScaleRotate(scale_limit=0.2, always_apply=True), A.VerticalFlip(always_apply=True), ], bbox_params=A.BboxParams(format='yolo')), A.Compose([A.ShiftScaleRotate(scale_limit=0.2, always_apply=True)], bbox_params=A.BboxParams(format='yolo')), A.Compose([A.SafeRotate(limit=90, always_apply=True), A.RandomBrightnessContrast(always_apply=True)], bbox_params=A.BboxParams(format='yolo')) ] results = [] for i, transform in enumerate(transforms): try: res = transform(image=img_ann.image, bboxes=img_ann.labels) path = Path(img_ann.image_path) name = f'{path.stem}_{i + 1}' img = ImageLabel( image=res['image'], labels=res['bboxes'], image_path=os.path.join(processed_images_dir, f'{name}{path.suffix}'), labels_path=os.path.join(processed_labels_dir, f'{name}.txt') ) results.append(img) except Exception as e: print(f'Error during transformation: {e}') return results def write_result(img_ann: ImageLabel, show_image=False): os.makedirs(os.path.dirname(img_ann.image_path), exist_ok=True) os.makedirs(os.path.dirname(img_ann.labels_path), exist_ok=True) if show_image: img_ann.visualize(annotation_classes) cv2.imencode('.jpg', img_ann.image)[1].tofile(img_ann.image_path) print(f'{img_ann.image_path} written') with open(img_ann.labels_path, 'w') as f: lines = [f'{ann[4]} {round(ann[0], 5)} {round(ann[1], 5)} {round(ann[2], 5)} {round(ann[3], 5)}\n' for ann in img_ann.labels] f.writelines(lines) f.close() print(f'{img_ann.labels_path} written') def read_labels(labels_path) -> [[]]: with open(labels_path, 'r') as f: rows = f.readlines() arr = [] for row in rows: str_coordinates = row.split(' ') class_num = str_coordinates.pop(0) coordinates = [float(n.replace(',', '.')) for n in str_coordinates] # noinspection PyTypeChecker coordinates.append(class_num) arr.append(coordinates) return arr def process_image(img_ann): results = image_processing(img_ann) for res_ann in results: write_result(res_ann) write_result(ImageLabel( image=img_ann.image, labels=img_ann.labels, image_path=os.path.join(processed_images_dir, Path(img_ann.image_path).name), labels_path=os.path.join(processed_labels_dir, Path(img_ann.labels_path).name) )) # os.remove(img_ann.image_path) # os.remove(img_ann.labels_path) def main(): checkpoint = datetime.now() - timedelta(days=720) try: with open(checkpoint_file, 'r') as f: checkpoint = datetime.strptime(f.read(), checkpoint_date_format) except: pass last_date = checkpoint while True: images = [] with os.scandir(data_images_dir) as imd: for image_file in imd: if not image_file.is_file(): continue mod_time = datetime.fromtimestamp(image_file.stat().st_mtime) if mod_time > checkpoint: images.append(image_file) last_date = max(last_date, mod_time) for image_file in images: try: image_path = os.path.join(data_images_dir, image_file.name) labels_path = os.path.join(data_labels_dir, f'{Path(image_path).stem}.txt') image = cv2.imdecode(np.fromfile(image_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED) process_image(ImageLabel( image_path=image_path, image=image, labels_path=labels_path, labels=read_labels(labels_path) )) except Exception as e: print(f'Error appeared {e}') if last_date != checkpoint: checkpoint = last_date try: with open(checkpoint_file, 'w') as f: f.write(datetime.strftime(checkpoint, checkpoint_date_format)) except: pass time.sleep(5) if __name__ == '__main__': main()