|
| 1 | +from dwf.datapattern.metadata.base_pattern import BasePattern |
| 2 | +from dwf.common.exception import DATA_PATTERN_MISMATCH |
| 3 | +import os |
| 4 | +import cv2 |
| 5 | +from PIL import Image |
| 6 | + |
| 7 | +class TwoImageFolder4Detection_txt(BasePattern): |
| 8 | + def __init__(self): |
| 9 | + super(TwoImageFolder4Detection_txt, self).__init__() |
| 10 | + self.data_type = 'image' |
| 11 | + self.organization = 'twoImageFolder4Detection@txt' |
| 12 | + self.algos = 'ssd' |
| 13 | + self.organization_parameter_width = None |
| 14 | + self.organization_parameter_height = None |
| 15 | + self.organization_parameter_channel = None |
| 16 | + self.organization_parameter_preprocess_resize_need = True |
| 17 | + self.organization_parameter_preprocess_resize_size = 224 |
| 18 | + self.organization_parameter_preprocess_crop_need = True |
| 19 | + self.organization_parameter_preprocess_crop_need = 256 |
| 20 | + self.organization_parameter_preprocess_shuffle_need = True |
| 21 | + self.organization_parameter_preprocess_normalization_need = True |
| 22 | + self.organization_parameter_preprocess_normalization_mean = [0.5, 0.5, 0.5] |
| 23 | + self.organization_parameter_preprocess_normalization_std = [0.5, 0.5, 0.5] |
| 24 | + |
| 25 | + self.semantic = '01' |
| 26 | + |
| 27 | + def check(self, folder_path): |
| 28 | + image_path = os.path.join(folder_path, 'Images') |
| 29 | + label_path = os.path.join(folder_path, 'Labels') |
| 30 | + |
| 31 | + if not os.path.exists(image_path): |
| 32 | + raise DATA_PATTERN_MISMATCH |
| 33 | + |
| 34 | + if not os.path.exists(label_path): |
| 35 | + raise DATA_PATTERN_MISMATCH |
| 36 | + |
| 37 | + image_list = os.listdir(image_path) |
| 38 | + label_list = os.listdir(label_path) |
| 39 | + |
| 40 | + if len(image_list) != len(label_list): |
| 41 | + raise DATA_PATTERN_MISMATCH |
| 42 | + return True |
| 43 | + |
| 44 | + def generate(self, folder_path): |
| 45 | + max_width = 0 |
| 46 | + min_width = 10000 |
| 47 | + max_height = 0 |
| 48 | + min_height = 10000 |
| 49 | + channel_num = None |
| 50 | + for file in os.listdir(os.path.join(folder_path, 'Images')): |
| 51 | + im = cv2.imread(os.path.join(folder_path, 'Images',file)) |
| 52 | + width, height, channel = im.shape[0],im.shape[1],im.shape[2] |
| 53 | + if channel_num is None: |
| 54 | + channel_num = channel |
| 55 | + if channel != channel_num: |
| 56 | + raise DATA_PATTERN_MISMATCH |
| 57 | + max_width = max(max_width, width) |
| 58 | + min_width = min(min_width, width) |
| 59 | + max_height = max(max_height, height) |
| 60 | + min_height = min(min_height, height) |
| 61 | + self.organization_parameter_width = [min_width, max_width] |
| 62 | + self.organization_parameter_height = [min_height, max_height] |
| 63 | + self.organization_parameter_channel = channel_num |
| 64 | + return self.dumps() |
| 65 | + |
| 66 | +# pattern = TwoImageFolder4Detection_txt() |
| 67 | +# print(pattern.generate('/Users/sherry/Desktop/xlearn_data/1228')) |
| 68 | + |
| 69 | + |
0 commit comments