def images_annotations_info(maskpath): annotations = [] images = [] mask_images_path = loadim(maskpath) for id_number, mask_image_path in enumerate(mask_images_path, 1): file_name = mask_image_path.split(os.path.sep)[-1][:-4] + '.jpg' mask_image = Image.open(mask_image_path) sub_masks = create_sub_masks(mask_image) for color, sub_mask in sub_masks.items(): category_id = category_ids[1][color] # ID number image_id = id_number annotation_id = id_number # image shape width, height = mask_image.size # 'images' info image = create_image_annotation(file_name, height, width, image_id) images.append(image) # 'annotations' info annotation = create_sub_mask_annotation(sub_mask, is_crowd, image_id, category_id, annotation_id) annotations.append(annotation) print('{:.2f}% finished.'.format( (id_number / len(mask_images_path) * 100))) return images, annotations
def anno_export(img_dir): image_path_list = loadim(img_dir) annotations = [] i = 1 print("begin to load.") for image_path in image_path_list: annotation = images_pose_anno(image_path) annotations.append(annotation) if i % 100 == 0: print("{:.2f}% finished.".format(i / len(image_path_list) * 100)) print(annotation) i += 1 print("all images loaded.") pose_annotations['annotations'] = annotations # print(pose_annotations) return pose_annotations
def load_data(): imgs_car_door = {'car_door': [], } latitude_car_door = [] longitude_car_door = [] for w in imgs_car_door.keys(): image_paths = load_image.loadim('/home/hangwu/CyMePro/data/car_door') for image_path in image_paths: resized_img = load_img(image_path) image_name = image_path.split(os.path.sep)[-1] la_cd, lo_cd = get_car_door_pose_from_filename(image_name) imgs_car_door[w].append(resized_img) latitude_car_door.append(la_cd) longitude_car_door.append(lo_cd) return imgs_car_door['car_door'], longitude_car_door
def load_data(): imgs_car_door = { 'car_door': [], } latitude_car_door = [] longitude_car_door = [] counter = 0 for w in imgs_car_door.keys(): image_paths = load_image.loadim('/home/hangwu/Workspace/car_door_half') for image_path in image_paths: resized_img = load_img(image_path) image_name = image_path.split(os.path.sep)[-1] la_cd, lo_cd = get_car_door_pose_from_filename(image_name) imgs_car_door[w].append(resized_img) latitude_car_door.append(la_cd) longitude_car_door.append(lo_cd) if counter % 100 == 0: print("loading {:.2f}%".format(counter / len(image_paths) * 100)) counter += 1 return imgs_car_door['car_door'], latitude_car_door, longitude_car_door
if px[3] !=0: if ymin > i: ymin = i if xmin > j: xmin = j if ymax < i: ymax = i if xmax < j: xmax = j # Bounding box information for .xml bnd_dict['folder'] = impath.split(os.path.sep)[-2] bnd_dict['filename'] = impath.split(os.path.sep)[-1] bnd_dict['path'] = impath bnd_dict['width'] = cols bnd_dict['height'] = rows bnd_dict['depth'] = channels bnd_dict['xmin'] = xmin bnd_dict['ymin'] = ymin bnd_dict['xmax'] = xmax bnd_dict['ymax'] = ymax # IMPORTANT: .COPY() # bnd_position.append(bnd_dict.copy()) return bnd_dict if __name__ == '__main__': # test image_path_list = load_image.loadim('/home/hangwu/Workspace/Car_Door') for image_path in image_path_list: bp = object_dict(image_path) print(bp)
import os import cv2 # EIGEN import load_image def cvt(imgpath): img = cv2.imread(imgpath) img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) raws, cols = img_gray.shape for i in range(raws): for j in range(cols): if img_gray[i][j] != 0: img_gray[i][j] = 255 save_name = imgpath.split(os.path.sep)[-1][:-4] cv2.imwrite('trimaps_with_window/{}.png'.format(save_name), img_gray) if __name__ == '__main__': imglist = load_image.loadim('masks_with_window') for imgfile in imglist: cvt(imgfile)
bnd_pos['ymax'] += move_y # test print(bnd_pos) # Save images cv2.imwrite('../data/images/{}.jpg'.format(save_name), output_image) cv2.imwrite('../data/annotations/masks/{}.png'.format(save_name), object_mask) ''' # Display # cv2.imshow('{}.jpg'.format(save_name), output_image) cv2.imshow('mask', object_mask) cv2.waitKey(0) cv2.destroyAllWindows() return bnd_pos if __name__ == '__main__': fg_list = load_image.loadim('images') print(fg_list) bg_list = load_image.loadim('background', 'jpg', 'Fabrik') print(bg_list) for fg in fg_list: bnd_info = generate_dict.object_dict(fg) fg = cv2.imread(fg, -1) bg_path = random.choice(bg_list) print(bg_path) bg = cv2.imread(bg_path, -1) overlap(bg, fg, bnd_info)
if ymin > i: ymin = i if xmin > j: xmin = j if ymax < i: ymax = i if xmax < j: xmax = j # Bounding box information for .xml bnd_dict['folder'] = impath.split(os.path.sep)[-2] bnd_dict['filename'] = impath.split(os.path.sep)[-1] bnd_dict['path'] = impath bnd_dict['width'] = cols bnd_dict['height'] = rows bnd_dict['depth'] = channels bnd_dict['xmin'] = xmin bnd_dict['ymin'] = ymin bnd_dict['xmax'] = xmax bnd_dict['ymax'] = ymax # IMPORTANT: .COPY() # bnd_position.append(bnd_dict.copy()) return bnd_dict if __name__ == '__main__': # test image_path_list = load_image.loadim('../data/car_door') for image_path in image_path_list: bp = object_dict(image_path) print(bp)
node_ymin = SubElement(node_bndbox, 'ymin') node_ymin.text = str(bndbox['ymin']) node_xmax = SubElement(node_bndbox, 'xmax') node_xmax.text = str(bndbox['xmax']) node_ymax = SubElement(node_bndbox, 'ymax') node_ymax.text = str(bndbox['ymax']) # format display xml = tostring(node_root, pretty_print=True) xml_name = bndbox['filename'][:-4] fp = open('../data/annotations/xmls/{}.xml'.format(xml_name), 'w') fp.write(xml.decode()) fp.close() if __name__ == '__main__': fg_list = load_image.loadim('../data/car_door') bg_list = load_image.loadim('../data/background', 'jpg', 'BG') for fg in fg_list: # IMPORTANT: if you want to resize images, don't forget resize in generate_dict img_scale = 0.4 bnd_info = generate_dict.object_dict(fg, img_scale) fg = cv2.imread(fg, -1) # resize the car door images fg = cv2.resize(fg, (0, 0), fx=img_scale, fy=img_scale, interpolation=cv2.INTER_CUBIC) bg_path = random.choice(bg_list) bg = cv2.imread(bg_path, -1)
mask_bw_dest_path = "/home/hangwu/Repositories/Dataset/dataset/cardoor1/annotation_all/mask_bw" # Car Door Subcategory: 1 or 2, IMPORTANT for naming the training data cd_subcat = 1 # Test test = False if test: fg_path = '/home/hangwu/Repositories/Mask_RCNN/dataset_tools/Test_Workspace/Image_Generation/Foreground' bg_path = '/home/hangwu/Repositories/Mask_RCNN/dataset_tools/Test_Workspace/Image_Generation/Background' xml_dest_path = "/home/hangwu/Repositories/Mask_RCNN/dataset_tools/Test_Workspace/Image_Generation/output/xml" image_dest_path = "/home/hangwu/Repositories/Mask_RCNN/dataset_tools/Test_Workspace/Image_Generation/output/image" mask_dest_path = "/home/hangwu/Repositories/Mask_RCNN/dataset_tools/Test_Workspace/Image_Generation/output/mask" mask_bw_dest_path = "/home/hangwu/Repositories/Mask_RCNN/dataset_tools/Test_Workspace/Image_Generation/output/mask_bw" fg_list = load_image.loadim(fg_path) # print(fg_list[1230:1250]) bg_list = load_image.loadim(bg_path, 'jpg', '0000') # Counter progress_show = 1 for fg_p in fg_list: # IMPORTANT: if you want to resize images, don't forget resize in generate_dict img_scale = 0.4 try: bnd_info = generate_dict.object_dict(fg_p, img_scale) fg = cv2.imread(fg_p, -1) # resize the car door images fg = cv2.resize(fg, (0, 0), fx=img_scale, fy=img_scale,