def rotate_images_and_save(angles,data_dir): jpg_path=os.path.join(data_dir,"JPEGImages") annot_path=os.path.join(data_dir,"Annotations") for angle in angles: io_utils.mkdir(os.path.join( data_dir+'_rotate_{}'.format(angle),'JPEGImages')) print(angle) for i in os.listdir(jpg_path): print(i) a, b = os.path.splitext(i) if b == ".jpg": img_path = os.path.join(jpg_path, i) img = cv2.imread(img_path) xml_path = os.path.join(annot_path, a + ".xml") object_infos = xml_store.get_object_infos_from_xml(xml_path) show_object_cv_box(object_infos, img) rotated_img = rotate_image(img, angle) object_infos_rotate=rotate_xml(object_infos,img,angle,rotated_img) show_object_cv_box(object_infos_rotate, rotated_img) break new_img_name=a + "_rotate_" + str(angle) + ".jpg" new_img_path=os.path.join( data_dir+'_rotate_{}'.format(angle),'JPEGImages', new_img_name) # print(object_infos) # show_object_cv_box(object_infos, rotated_img) # print(rotated_img.shape) im_info=xml_utils.create_image_info(new_img_name,new_img_path,rotated_img.shape[1],rotated_img.shape[0],rotated_img.shape[2]) new_xml_path=os.path.join( data_dir+'_rotate_{}'.format(angle),'Annotations') cv2.imwrite(new_img_path, rotated_img) xml_store.save_annotations(new_xml_path, im_info, object_infos_rotate)
def bright_adjuest_images_and_save(bright_adjuest, data_dir): jpg_path = os.path.join(data_dir, "JPEGImages") annot_path = os.path.join(data_dir, "Annotations") for bright in bright_adjuest: # io_utils.mkdir(os.path.join( data_dir+'_ver_flipped','JPEGImages')) for i in os.listdir(jpg_path): a, b = os.path.splitext(i) if b == ".jpg": img_path = os.path.join(jpg_path, i) img = cv2.imread(img_path) xml_path = os.path.join(annot_path, a + ".xml") object_infos = xml_store.get_object_infos_from_xml(xml_path) show_object_cv_box(object_infos, img) hor_flipped_img = bright_adjuest_image(img, bright) show_object_cv_box(object_infos, hor_flipped_img) break new_img_name = a + "_bright_adjuest_{}.jpg", format(bright) new_img_path = os.path.join(data_dir + "_bright_adjuest_{}", format(bright), 'JPEGImages', new_img_name) im_info = xml_utils.create_image_info(new_img_name, new_img_path, hor_flipped_img.shape[1], hor_flipped_img.shape[0], hor_flipped_img.shape[2]) new_xml_path = os.path.join(data_dir + "_bright_adjuest_{}", format(bright), 'Annotations') cv2.imwrite(new_img_path, hor_flipped_img) xml_store.save_annotations(new_xml_path, im_info, object_infos)
def ver_flipped_images_and_save(use_hor_flipped, data_dir): jpg_path = os.path.join(data_dir, "JPEGImages") annot_path = os.path.join(data_dir, "Annotations") if use_hor_flipped: # io_utils.mkdir(os.path.join( data_dir+'_ver_flipped','JPEGImages')) for i in os.listdir(jpg_path): a, b = os.path.splitext(i) if b == ".jpg": img_path = os.path.join(jpg_path, i) img = cv2.imread(img_path) xml_path = os.path.join(annot_path, a + ".xml") object_infos = xml_store.get_object_infos_from_xml(xml_path) show_object_cv_box(object_infos, img) hor_flipped_img = hor_flipped_image(img) new_object_infos = hor_flipped_xml(object_infos, img) show_object_cv_box(new_object_infos, hor_flipped_img) break new_img_name = a + "_hor_flipped.jpg" new_img_path = os.path.join(data_dir + '_hor_flipped', 'JPEGImages', new_img_name) im_info = xml_utils.create_image_info(new_img_name, new_img_path, hor_flipped_img.shape[1], hor_flipped_img.shape[0], hor_flipped_img.shape[2]) new_xml_path = os.path.join(data_dir + '_hor_flipped', 'Annotations') cv2.imwrite(new_img_path, hor_flipped_img) xml_store.save_annotations(new_xml_path, im_info, new_object_infos)
def shift_and_save_images(offset, data_dir): jpg_path = os.path.join(data_dir, "JPEGImages") annot_path = os.path.join(data_dir, "Annotations") # io_utils.mkdir(os.path.join(data_dir + '_shift_{}_{}'.format(offset[0],offset[1]), 'JPEGImages')) for i in os.listdir(jpg_path): print(i) a, b = os.path.splitext(i) if b == ".jpg": img_path = os.path.join(jpg_path, i) img = cv2.imread(img_path) xml_path = os.path.join(annot_path, a + ".xml") object_infos = xml_store.get_object_infos_from_xml(xml_path) show_object_cv_box(object_infos, img) img_shift = translateit(img, offset) new_obj_infos = adjuest_obj_info(object_infos, offset, img.shape[:2]) show_object_cv_box(new_obj_infos, img_shift) new_img_name = a + "_shift_{}_{}.jpg".format(offset[0], offset[1]) new_img_path = os.path.join( data_dir + '_shift_{}_{}'.format(offset[0], offset[1]), 'JPEGImages', new_img_name) im_info = xml_utils.create_image_info(img_shift, new_img_path, img_shift.shape[1], img_shift.shape[0], img_shift.shape[2]) new_xml_path = os.path.join( data_dir + '_shift_{}_{}'.format(offset[0], offset[1]), 'Annotations') cv2.imwrite(new_img_path, img_shift) xml_store.save_annotations(new_xml_path, im_info, new_obj_infos)