def crop_oriented_rect_polar(self, im, center, angle, rx, ry): xc, yc = center # top, left, right, bottom = xc-rx, yc-ry, xc+rx, yc+ry dx, dy = ProcUtils().rotateXY_float(-rx, ry, angle) pt0 = (int(xc + dx), int(yc + dy)) dx, dy = ProcUtils().rotateXY_float(-rx, -ry, angle) pt1 = (int(xc + dx), int(yc + dy)) dx, dy = ProcUtils().rotateXY_float(rx, -ry, angle) pt2 = (int(xc + dx), int(yc + dy)) dx, dy = ProcUtils().rotateXY_float(rx, ry, angle) pt3 = (int(xc + dx), int(yc + dy)) return self.crop_oriented_rect(im, (pt0, pt1, pt2, pt3))
def set_rgb(self, rgb=None, rgb_path=None): """set rgb with given ndarray or rgb file path """ if rgb is not None: self.rgb=np.copy(rgb) else: self.rgb=None self.hasRgb = rgb is not None or ProcUtils().isimpath(rgb_path) if not self.hasRgb: return 0 if ProcUtils().isimpath(rgb_path): self.rgb = cv2.imread(rgb_path)[::-1] self.height, self.width = self.rgb.shape[:2] if self.workspace is not None: self.workspace.correct_ws(max_shape=(self.width,self.height)) else: self.set_workspace() # self.roi = ProcUtils().correct_cropsize(self.crop_size, self.size) self.fix_depth_size()
def save_rgbd(self, rgbd, filename=None, save_dir_name='cam_data'): """ save rgbd object to files :param rgbd: rgbd object :type rgbd: :class:`.RGBD` :param filename: image filename :type filename: str """ subdir = ProcUtils().get_current_time_str('%m%d') if rgbd.hasRgb: filename = ArrayUtils().save_array( array=rgbd.bgr(), folds=[self.args.root_dir, save_dir_name, subdir, 'image'], filename=filename) if rgbd.hasDepth: _ = ArrayUtils().save_array( array=rgbd.depth, folds=[self.args.root_dir, save_dir_name, subdir, 'depth'], filename=filename) # _ = ArrayUtils().save_array(array=rgbd.depth_disp(args=self.args, show_jet=False), # folds=[self.args.data_root, save_dir_name, subdir, 'depth_disp'], # filename=filename) # _ = ArrayUtils().save_array(array=rgbd.depth_disp(args=self.args, show_jet=True), # folds=[self.args.data_root, save_dir_name, subdir, 'depth_disp_jet'], # filename=filename) print('image saved ...') time.sleep(0.03)
def save_array_v2(self, array, fold=None, filename=None, ext='.png'): if filename is None: filename = ProcUtils().get_current_time_str() filepath = filename + ext if fold is not None: if not os.path.exists(fold): os.makedirs(fold) cv2.imwrite(filepath, array) return filename
def process_this(self): in_dir, self.filename = os.path.split(self.rgbd_path[0]) self.filename, self.fileext = os.path.splitext(self.filename) test_some = hasattr(self.args, 'images') test_ignore = hasattr(self.args, 'ignores') if test_some: if self.filename not in self.args.images: print('>>> This image not in test list >>> Skip') ProcUtils().clscr() return False if test_ignore: if self.filename in self.args.ignores: print('>>> This image in ignore list >>> Skip') ProcUtils().clscr() return False return True
def aug_single(self,im_path, im_id, bg_ims=None, angle_step=10, show_step=False): from scipy.ndimage import rotate from shutil import copyfile from ketisdk.utils.proc_utils import ProcUtils # makedir im_dir = os.path.split(im_path)[0] root_dir, dir_name = os.path.split(im_dir) save_dir = os.path.join(root_dir, f'{dir_name}_aug') os.makedirs(save_dir, exist_ok=True) # read image im = cv2.imread(im_path)[:, :, ::-1] im_height, im_width = im.shape[:2] if show_step: cv2.imshow('im', im[:,:,::-1]) # get instances instances = [instance for instance in self.annotation_dict_list if im_id == instance['image_id']] for angle in range(0,360, angle_step): im_out_path = os.path.join(save_dir, ProcUtils().get_current_time_str() + '.png') # make image if angle==0: copyfile(im_path, im_out_path) im_rot = np.copy(im) else: im_rot = np.copy(rotate(im, angle=angle, reshape=False, order=3)) cv2.imwrite(im_out_path,im_rot[:,:,::-1]) self.make_ann_images(self.image_id,im_out_path,(im_width,im_height), self.out_images) # make annotation for instance in instances: mask, bbox = self.segmToMask(instance['segmentation'], h=im_height, w=im_width), instance['bbox'] if angle !=0: mask = rotate(mask,angle=angle, reshape=False, order=0) Y,X = np.where(mask>0) if len(Y)==0: continue x,y = np.amin(X), np.amin(Y) w, h = np.amax(X)-x, np.amax(Y)-y bbox = [x,y,w,h] if show_step: locs = np.where(mask>0) im_rot[locs] = 0.7*im_rot[locs] + (0,75,0) cv2.rectangle(im_rot, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), (255,0,0), 2) # cv2.rectangle(im_rot,(bbox[0], bbox[1]), (bbox[2], bbox[3]), (255,0,0), 2) cv2.imshow('im_rot', im_rot[:,:,::-1]) cv2.waitKey() self.make_annotations(mask, bbox, self.ann_id, self.image_id, instance['category_id'],annotations=self.out_annotations) self.ann_id +=1 self.image_id+=1
def save_array(self, array, folds=None, filename=None, ext='.png'): if filename is None: filename = ProcUtils().get_current_time_str() filepath = filename + ext if folds is not None: folder = '' for fold in folds: folder = os.path.join(folder, fold) if not os.path.exists(folder): os.makedirs(folder) filepath = os.path.join(folder, filepath) cv2.imwrite(filepath, array) return filename
def save(self, ann_path, images=None, annotations=None, categories=None, ann_info=None, ann_license=None): from ketisdk.utils.proc_utils import ProcUtils import json if ann_info is None: ann_info = {"info": { "description": "KETI Dataset", "url": "keti.re.kr", "version": "1.0", "year": int(ProcUtils().get_current_time_str('%Y')), "contributor": "Trung Bui", "data_create": '{}/{}/{}'.format(ProcUtils().get_current_time_str('%Y'), ProcUtils().get_current_time_str('%m'), ProcUtils().get_current_time_str('%d')) }} if ann_license is None: ann_license = {"licenses": [ {"url": "keti.re.kr", "id": "1", "name": "Atribution license" }]} if images is None: images = self.image_dict_list if categories is None: categories = self.categories if annotations is None: annotations = self.annotation_dict_list ann_dict = dict() ann_dict.update(ann_info) ann_dict.update(ann_license) ann_dict.update({"images": images}) ann_dict.update({"categories": categories}) ann_dict.update({"annotations": annotations}) save_dir, _ = os.path.split(ann_path) os.makedirs(save_dir, exist_ok=True) instance_json_obj = open(ann_path, 'w') instance_json_obj.write(json.dumps(ann_dict)) instance_json_obj.close() print('{} {} saved'.format('+'*10, ann_path))
def read_json(self, json_path): """ read json file from path :param json_path: json file path :type json_path: str :return: dict of contents """ assert ProcUtils().isexists(json_path) json_file = open(json_path) json_dict = json.load(json_file) json_file.close() return json_dict
def set_depth(self, depth=None, depth_path=None): """set depth with given ndarray or depth file path""" if depth is not None: self.depth=np.copy(depth) else: self.depth=None self.hasDepth = depth is not None or ProcUtils().isimpath(depth_path) if not self.hasDepth: return 0 if ProcUtils().isimpath(depth_path): self.depth = cv2.imread(depth_path, -1) if self.hasRgb: self.height, self.width = self.rgb.shape[:2] else: self.height, self.width = self.depth.shape[:2] if self.workspace is not None: self.workspace.correct_ws(max_shape=(self.width, self.height)) else: self.set_workspace() self.fix_depth_size() if self.depth_inpaint_rad is not None: depth_crop = self.crop_depth() left, top, right, bottom = self.workspace.bbox self.depth[top:bottom, left:right] = self.inpaint_depth(self.depth_inpaint_rad,depth_crop) if self.depth_denoise_ksize is not None: depth_crop_filtered = cv2.medianBlur(self.crop_depth(), ksize=self.depth_denoise_ksize) left, top, right, bottom = self.workspace.bbox self.depth[top:bottom, left:right] = depth_crop_filtered
def run(): commands = ['run_realsense_gui', 'demo_client_gui'] cmd_dict = ProcUtils().get_command_keys(commands) key = input('select action: ') input_command = cmd_dict[key] if input_command == 'run_sensors': from .run_sensors import run if input_command == 'demo_client_gui': from .demo_client_gui import demo_client_gui as run run()
def get_info_from_json(self, json_path): """ :param json_path: json file path """ ann_dict = BasJson().read_json(json_path) info = ann_dict['Info'] self.classes = ann_dict['Classes'] self.polygons = ann_dict['Polygons'] self.classes_unq = list(np.unique(self.classes)) self.colors = ProcUtils().get_color_list(len(self.classes_unq)) self.color_dict = dict() for cls, color in zip(self.classes_unq, self.colors): self.color_dict.update({cls: color})
def draw_polys_from_json(self, json_path, im=None, im_path=None, text_scale=1.5, text_thick=3, rect=(0, 0, 100, 100), space=10, alpha=0.5, up2down=True): """ :param json_path: json file path :param im: ndarray image :param im_path: image file path :param text_scale: opencv text_scale :param text_thick: opencv text_thick :param rect: rect location of text display :param space: space between text lines :param alpha: text transparency :param up2down: direct to display text lines :returns: ndarray """ assert im is not None or im_path is not None if ProcUtils().isimpath(im_path): im = cv2.imread(im_path) self.get_info_from_json(json_path) out = self.draw_polys(im) if hasattr(self, 'args'): text_scale, text_thick, rect, space, alpha, up2down = self.args.text_scale, self.args.text_thick, \ self.args.text_rect, self.args.text_space, \ self.args.text_alpha, self.args.text_alpha out = VisUtils().draw_texts_blend(out, texts=self.classes_unq, colors=self.colors, scale=text_scale, thick=text_thick, text_rect=rect, space=space, alpha=alpha, up2down=up2down) return out
def run(): commands = [ 'demo_gui', 'demo_vision', 'demo_robot', 'demo_calib', 'demo_workcell' ] cmd_dict = ProcUtils().get_command_keys(commands) key = input('select action: ') input_command = cmd_dict[key] if input_command == 'demo_gui': from ketisdk.gui.gui import GUI GUI() return if input_command == 'demo_vision': from ketisdk.vision.samples.select_sample import run if input_command == 'demo_robot': from ketisdk.robot.samples.select_sample import run if input_command == 'demo_calib': from ketisdk.calib.samples.select_sample import run if input_command == 'demo_workcell': from ketisdk.workcell.samples.select_sample import run run()
def save_array_v3(self, array, filepath=None): if filepath is None: filepath = ProcUtils().get_time_name() + '.png' fold, _ = os.path.split(filepath) if not os.path.exists(fold): os.makedirs(fold) cv2.imwrite(filepath, array)
def set_bg_rgb(self, rgb=None, rgb_path=None): """ set rgb of background""" if rgb is not None or ProcUtils().isimpath(rgb_path): self.has_bg=True if ProcUtils().isimpath(rgb_path): rgb = cv2.imread(rgb_path)[:,:,::-1] self.rgb_bg = cv2.resize(rgb, (self.width, self.height), interpolation=cv2.INTER_CUBIC)
def set_bg_depth(self, depth=None, depth_path=None): """ set depth of background""" if depth is not None or ProcUtils().isimpath(depth_path): self.has_bg = True if ProcUtils().isimpath(depth_path): depth = cv2.imread(depth_path, -1) self.depth_bg = cv2.resize(depth, (self.width, self.height), interpolation=cv2.INTER_CUBIC)