def crop_circle(self, inverse=False): if self.mask is None: self.mask = self.get_circle_mask(inverse=inverse) cropped_img = np.copy(self.img_object.img) cropped_img = cropped_img * self.mask # 원 내부만 Cropping if not inverse: self.cropped_img_object = EOPImage(img=cropped_img[ self.circle_abst_bbox.y_st:self.circle_abst_bbox.y_ed, self.circle_abst_bbox.x_st:self.circle_abst_bbox.x_ed], abst_bbox=self.circle_abst_bbox) # 원 외부만 Cropping else: self.cropped_img_object = EOPImage(img=cropped_img, abst_bbox=self.circle_abst_bbox) self.img_object.add_child(self.cropped_img_object)
def __init__(self, serial_number: str, image_path: str, is_NG: bool, cam: int, defect_list: list = None): super(EOPData, self).__init__() if defect_list is None: defect_list = list() self.serial_number = serial_number self.img_object = EOPImage(img=image_path) self.is_NG = is_NG self.cam = cam self.defect_list = defect_list
def get_normalization_variables(self, is_saved=True): data_list = self.data_list data_list_size = len(data_list) entire_mean = 0 entire_std = 0 for data in data_list: img_object = None """ @TODO 이거 수정하기 (EOPImage 에서 모두 handling 되도록) """ if self.data_type == str: img_path = data # img_object = EOPImage(img=cv2.cvtColor(cv2.imread(data).astype(np.float32), cv2.COLOR_BGR2GRAY)) img_object = EOPImage(img=img_path) elif isinstance(self.data_type, np.ndarray): img_object = EOPImage(img=data) img = img_object.img entire_mean += np.mean(img) entire_std += np.std(img) entire_mean /= data_list_size entire_std /= data_list_size # Save to json if is_saved: normalization_variables_dict = dict() normalization_variables_dict["mean"] = entire_mean normalization_variables_dict["std"] = entire_std Path(self.normalization_variables_file_path).parent.mkdir( parents=True, exist_ok=True) with open(self.normalization_variables_file_path, "w", encoding="utf-8") as f: json.dump(normalization_variables_dict, f, indent=4) return (entire_mean, ), (entire_std, )
def crop_grid(cls, bbox_list, is_boundary_mask): if is_boundary_mask: # grid_mask_img_path = HousingCam3Cropper.BOUNDARY_MASK_IMG_PATH mask = cls.BOUNDARY_MASK_IMG else: # grid_mask_img_path = HousingCam3Cropper.MASK_IMG_PATH mask = cls.MASK_IMG # mask = cls.get_mask(mask_img_path=grid_mask_img_path) # @TODO masking 부분 수정해야 할 것 # import matplotlib.pyplot as plt # plt.imsave("./test.jpg", mask) cropper = cls(img_object=EOPImage(img=mask), defect_list=[]) return cropper.crop(bbox_list=bbox_list)
def crop_ring(self, center_coord: Coordinate): center_x = center_coord.x center_y = center_coord.y crop_abst_bbox = BoundingBox(x_st=center_x - self.big_rad, x_ed=center_x + self.big_rad, y_st=center_y - self.big_rad, y_ed=center_y + self.big_rad) if self.mask is None: self.mask = self.get_ring_mask(big_rad=self.big_rad, small_rad=self.small_rad) self.cropped_img_object = EOPImage( img=self.img_object.img[crop_abst_bbox.y_st:crop_abst_bbox.y_ed, crop_abst_bbox.x_st:crop_abst_bbox.x_ed] * self.mask, abst_bbox=crop_abst_bbox) # image object 에 child 로 저장 self.img_object.add_child(self.cropped_img_object) return self.cropped_img_object
def crop_grid(cls, first_crop_x_st, crop_size, first_crop_y_ed, crop_empty_space_y_gap, crop_y_gap, is_boundary_mask): if is_boundary_mask: mask = cls.BOUNDARY_MASK_IMG_PATH else: mask = cls.MASK_IMG cropper = cls(img_object=EOPImage(img=mask), defect_list=[]) return cropper.crop( first_crop_x_st=first_crop_x_st, crop_size=crop_size, first_crop_y_ed=first_crop_y_ed, crop_empty_space_y_gap=crop_empty_space_y_gap, crop_y_gap=crop_y_gap )
def __getitem__(self, idx): # 이미지 path 기반 데이터 반환 if not self.is_buffered: img_path = self.data_list[idx] img_file_name, ext = os.path.splitext(os.path.basename(img_path)) serial_number, img_idx, defect_category = img_file_name.split("_") defect_category = int(defect_category) if ext == ".npy": img_object = EOPImage(img=np.load(img_path)) elif ext == ".png": img_object = EOPImage(img=img_path) # Resize if self.resize_size is not None: img_object.img = cv2.resize(img_object.img, dsize=(self.resize_size[0], self.resize_size[1]), interpolation=cv2.INTER_AREA) if self.resize_rate is not None: img_object.img = cv2.resize(img_object.img, dsize=(0, 0), fx=self.resize_rate, fy=self.resize_rate, interpolation=cv2.INTER_AREA) input_data = InputData(img_object=img_object) # Set defect input_data.is_NG = np.zeros(1, dtype=np.float32) input_data.defect_category = np.zeros( DATASET_PROPERTIES.CLASS_NUMBER, dtype=np.float32) if defect_category != 0: input_data.is_NG[0] = 1 input_data.defect_category[defect_category] = 1 # Transform image input_data.img = self.transform(input_data.img) return input_data.img, input_data.is_NG, input_data.defect_category, list( ), list(), img_file_name # 메모리에 올려진 이미지 데이터 반환 else: input_data: InputData = self.img_data_list[idx] img_data = self.transform(input_data.img) return img_data, input_data.is_NG, input_data.defect_category, list( ), list(), ""
def set_buffer(self): for img_path in self.data_list: img_file_name = os.path.splitext(os.path.basename(img_path))[0] serial_number, img_idx, defect_category = img_file_name.split("_") defect_category = int(defect_category) img_object = EOPImage(img=cv2.cvtColor( cv2.imread(img_path).astype(np.float32), cv2.COLOR_BGR2GRAY)) input_data = InputData(img_object=img_object) # Set defect input_data.is_NG = np.zeros(1, dtype=np.float32) input_data.defect_category = np.zeros( DATASET_PROPERTIES.CLASS_NUMBER, dtype=np.float32) if defect_category != 0: input_data.is_NG[0] = 1 input_data.defect_category[defect_category] = 1 self.img_data_list.append(input_data) print("Completed to set image data to memory")
def _set_base_img_with_EOPImage(self, base_img: EOPImage): base_img = np.copy(base_img.img) base_img = base_img.astype(np.uint8) self.base_img = cv2.cvtColor(base_img, cv2.COLOR_GRAY2BGR)
def _set_img_object(self, img_object: EOPImage): img_object.base_node = img_object self._img_object = img_object
def get_crop_rotate_ring_piece_list(self, angle): ring_img = np.copy(self.cropped_img_object.img) crop_rotate_ring_piece_list = list() abst_center_x = self.cropped_img_object.abst_bbox.center.x abst_center_y = self.cropped_img_object.abst_bbox.center.y # Crop 할 부분 계산 """ @TODO ?? 함수로 뺄 것 """ height, width = ring_img.shape rad = np.pi / 180 crop_center_x = width // 2 crop_center_y = height // 2 gap_x = self.big_rad * np.sin(angle / 2 * rad) gap_y = self.big_rad - self.small_rad * np.cos(angle / 2 * rad) # Crop 된 이미지의 잘라낼 좌표 crop_bbox = BoundingBox(x_st=int(crop_center_x - gap_x), x_ed=int(crop_center_x + gap_x), y_st=0, y_ed=int(gap_y)) # Crop 된 이미지의 절대 좌표 rotate_crop_abst_bbox = BoundingBox(x_st=crop_bbox.x_st, x_ed=crop_bbox.x_ed, y_st=crop_bbox.y_st, y_ed=crop_bbox.y_ed) rotate_crop_abst_bbox.init_with_st_ed( x_st=rotate_crop_abst_bbox.x_st + self.cropped_img_object.abst_bbox.x_st, x_ed=rotate_crop_abst_bbox.x_ed + self.cropped_img_object.abst_bbox.x_st, y_st=rotate_crop_abst_bbox.y_st + self.cropped_img_object.abst_bbox.y_st, y_ed=rotate_crop_abst_bbox.y_ed + self.cropped_img_object.abst_bbox.y_st) # 회전하면서 Cropping for position in range(360 // angle): rotate_img = EOPImage(img=ring_img[crop_bbox.y_st:crop_bbox.y_ed, crop_bbox.x_st:crop_bbox.x_ed], abst_bbox=rotate_crop_abst_bbox) self.cropped_img_object.add_child(rotate_img) # 회전 ring_img = cv2.warpAffine( ring_img, cv2.getRotationMatrix2D((crop_center_x, crop_center_y), angle, scale=1), (width, height)) rotate_crop_abst_bbox = rotate_crop_abst_bbox.get_rotate_bounding_box( center_coord=Coordinate(abst_center_x, abst_center_y), angle=angle) # 조각들의 image list 반환 for ring_piece in self.cropped_img_object.child_list: crop_rotate_ring_piece_list.append(ring_piece) return crop_rotate_ring_piece_list
def crop_rectangle(self, bbox: BoundingBox): self.cropped_img_object = EOPImage( img=self.img_object.img[bbox.y_st:bbox.y_ed, bbox.x_st:bbox.x_ed], abst_bbox=bbox) self.img_object.add_child(self.cropped_img_object)
class RingCropHelper(CropHelper): def __init__(self, img_object: EOPImage, big_rad=None, small_rad=None): super(RingCropHelper, self).__init__(img_object) self._big_rad = big_rad self._small_rad = small_rad @property def big_rad(self): return self._big_rad @big_rad.setter def big_rad(self, big_rad): if isinstance(big_rad, int): self._big_rad = big_rad else: raise ValueError("It must be int") @property def small_rad(self): return self._small_rad @small_rad.setter def small_rad(self, small_rad): if isinstance(small_rad, int): self._small_rad = small_rad else: raise ValueError("It must be int") def get_ring_mask(self, big_rad: int, small_rad: int): center_coord = Coordinate(big_rad, big_rad) frame_size = (big_rad * 2, big_rad * 2) mask_big = np.zeros(frame_size, dtype=np.uint8) mask_small = np.zeros(frame_size, dtype=np.uint8) cv2.circle(mask_big, center_coord.value, big_rad, (255, 255, 255), thickness=-1) cv2.circle(mask_small, center_coord.value, small_rad, (255, 255, 255), thickness=-1) return (mask_big - mask_small) / 255 def crop_ring(self, center_coord: Coordinate): center_x = center_coord.x center_y = center_coord.y crop_abst_bbox = BoundingBox(x_st=center_x - self.big_rad, x_ed=center_x + self.big_rad, y_st=center_y - self.big_rad, y_ed=center_y + self.big_rad) if self.mask is None: self.mask = self.get_ring_mask(big_rad=self.big_rad, small_rad=self.small_rad) self.cropped_img_object = EOPImage( img=self.img_object.img[crop_abst_bbox.y_st:crop_abst_bbox.y_ed, crop_abst_bbox.x_st:crop_abst_bbox.x_ed] * self.mask, abst_bbox=crop_abst_bbox) # image object 에 child 로 저장 self.img_object.add_child(self.cropped_img_object) return self.cropped_img_object def get_crop_rotate_ring_piece_list(self, angle): ring_img = np.copy(self.cropped_img_object.img) crop_rotate_ring_piece_list = list() abst_center_x = self.cropped_img_object.abst_bbox.center.x abst_center_y = self.cropped_img_object.abst_bbox.center.y # Crop 할 부분 계산 """ @TODO ?? 함수로 뺄 것 """ height, width = ring_img.shape rad = np.pi / 180 crop_center_x = width // 2 crop_center_y = height // 2 gap_x = self.big_rad * np.sin(angle / 2 * rad) gap_y = self.big_rad - self.small_rad * np.cos(angle / 2 * rad) # Crop 된 이미지의 잘라낼 좌표 crop_bbox = BoundingBox(x_st=int(crop_center_x - gap_x), x_ed=int(crop_center_x + gap_x), y_st=0, y_ed=int(gap_y)) # Crop 된 이미지의 절대 좌표 rotate_crop_abst_bbox = BoundingBox(x_st=crop_bbox.x_st, x_ed=crop_bbox.x_ed, y_st=crop_bbox.y_st, y_ed=crop_bbox.y_ed) rotate_crop_abst_bbox.init_with_st_ed( x_st=rotate_crop_abst_bbox.x_st + self.cropped_img_object.abst_bbox.x_st, x_ed=rotate_crop_abst_bbox.x_ed + self.cropped_img_object.abst_bbox.x_st, y_st=rotate_crop_abst_bbox.y_st + self.cropped_img_object.abst_bbox.y_st, y_ed=rotate_crop_abst_bbox.y_ed + self.cropped_img_object.abst_bbox.y_st) # 회전하면서 Cropping for position in range(360 // angle): rotate_img = EOPImage(img=ring_img[crop_bbox.y_st:crop_bbox.y_ed, crop_bbox.x_st:crop_bbox.x_ed], abst_bbox=rotate_crop_abst_bbox) self.cropped_img_object.add_child(rotate_img) # 회전 ring_img = cv2.warpAffine( ring_img, cv2.getRotationMatrix2D((crop_center_x, crop_center_y), angle, scale=1), (width, height)) rotate_crop_abst_bbox = rotate_crop_abst_bbox.get_rotate_bounding_box( center_coord=Coordinate(abst_center_x, abst_center_y), angle=angle) # 조각들의 image list 반환 for ring_piece in self.cropped_img_object.child_list: crop_rotate_ring_piece_list.append(ring_piece) return crop_rotate_ring_piece_list