def augment_data(image, face, landmarks): '''produce 2 type of augumentation: rotation and noising, returns an array of 3 tuple (image, landmarks)''' assert (type(face) is Region) h, w = image.shape[:2] angle = 30 pivot = face.center() # 30 degree rotation A = utils.rotate_image(image, angle, pivot) B = utils.rotate_landmarks(landmarks, pivot, angle) R1 = utils.points_region(B) # -30 degree rotatation C = utils.rotate_image(image, -angle, pivot) D = utils.rotate_landmarks(landmarks, pivot, -angle) R2 = utils.points_region(D) # mirroring E = utils.flip_image(image) F = utils.naive_flip_landmarks(landmarks, w) R3 = face.flip(width=w) return [(A, B, R1), (C, D, R2), (E, F, R3)]
def test_augment(): utils.init_face_detector(True, 321) utils.load_shape_predictor("dlib/shape_predictor_68_face_landmarks.dat") for img in utils.images_inside("trainset"): points = utils.detect_landmarks(img, region(img)) angle = 30 h, w = img.shape[:2] center = (w / 2, h / 2) # 30 degree rotation rot1 = utils.rotate_image(img, angle) rot_pts1 = utils.rotate_landmarks(points, center, angle) # -30 degree rotatation rot2 = utils.rotate_image(img, -angle) rot_pts2 = utils.rotate_landmarks(points, center, -angle) # mirroring mir = utils.flip_image(img) mir_pts = utils.detect_landmarks(mir, region(mir)) utils.draw_points(img, points) utils.draw_points(rot1, rot_pts1, color=Colors.cyan) utils.draw_points(rot2, rot_pts2, color=Colors.purple) utils.draw_points(mir, mir_pts, color=Colors.green) while True: cv2.imshow("image", img) cv2.imshow("mirrored", mir) cv2.imshow("rotated30", rot1) cv2.imshow("rotated-30", rot2) key = cv2.waitKey(20) & 0Xff if key == Keys.ESC: break elif key == Keys.Q: return cv2.destroyAllWindows()
def generate(samples, batch_size): num_samples = len(samples) shuffle(samples) while 1: for offset in range(0, num_samples, batch_size): batch_samples = samples[offset:offset + batch_size][:] images = [] angles = [] for batch_sample in batch_samples: #TODO decompose this stuff into functions #load center img_src = batch_sample[0].strip() center_image = mpimg.imread(img_src) center_image = utils.resize_image(center_image) images.append(center_image) angles.append(str(batch_sample[3])) #add random brightness center_bright = utils.bright_image(center_image) images.append(center_bright) angles.append(str(batch_sample[3])) # flip center if float(batch_sample[3]) != 0: flipped_image = utils.flip_image(center_image) flipped_angle = utils.flip_angle(float(batch_sample[3])) images.append(flipped_image) angles.append(flipped_angle) #################################################### # left img_src = batch_sample[1].strip() left_image = mpimg.imread(img_src) left_image = utils.resize_image(left_image) images.append(left_image) left_angle = float(batch_sample[3]) + .28 angles.append(str(left_angle)) left_bright = utils.bright_image(left_image) images.append(left_bright) angles.append(left_angle) #flip left if float(batch_sample[3]) != 0: left_flipped_image = utils.flip_image(center_image) left_flipped_angle = utils.flip_angle( float(batch_sample[3])) + .28 images.append(left_flipped_image) angles.append(left_flipped_angle) ################################################ #right img_src = batch_sample[2].strip() right_image = mpimg.imread(img_src) right_image = utils.resize_image(right_image) images.append(right_image) right_angle = float(batch_sample[3]) - .28 angles.append(str(right_angle)) right_bright = utils.bright_image(right_image) images.append(right_bright) angles.append(right_angle) #flip right if float(batch_sample[3]) != 0: right_flipped_image = utils.flip_image(center_image) right_flipped_angle = utils.flip_angle( float(batch_sample[3])) - .28 images.append(right_flipped_image) angles.append(right_flipped_angle) x_train = np.array(images) y_train = np.array(angles) yield shuffle(x_train, y_train)
units='dimensionless') # loading the data flips the images vertically! #block = io.read_block() block = neo.Block() seg = neo.Segment(name='segment 0', index=0) block.segments.append(seg) print('vlock', block) print('seg', block.segments[0]) block.segments[0].imagesequences.append(imageSequences) # change data orientation to be top=ventral, right=lateral imgseq = block.segments[0].imagesequences[0] imgseq = flip_image(imgseq, axis=-2) imgseq = rotate_image(imgseq, rotation=-90) block.segments[0].imagesequences[0] = imgseq # Transform into analogsignals block.segments[0].analogsignals = [] block = ImageSequence2AnalogSignal(block) block.segments[0].analogsignals[0] = time_slice( block.segments[0].analogsignals[0], args.t_start, args.t_stop) if args.annotations is not None: block.segments[0].analogsignals[0].annotations.\ update(parse_string2dict(args.annotations)) block.segments[0].analogsignals[0].annotations.update(
print(count + 40 * (x - 1)) count += 1 for i in range(1, 7 + 1):#7 new_image = utils.contras_image(image, random.uniform(80, 200) / 100, random.uniform(0, 50) / 10) new_file = new_dir + "/" + str(count + 40 * (x - 1)) + ".jpg" cv2.imwrite(new_file, new_image) print(count + 40 * (x - 1)) count += 1 for i in range(1, 7 + 1):#7 new_image = utils.random_shadow(image) new_file = new_dir + "/" + str(count + 40 * (x - 1)) + ".jpg" cv2.imwrite(new_file, new_image) print(count + 40 * (x - 1)) count += 1 for i in range(-1, 2):#3 new_image = utils.flip_image(image, i) new_file = new_dir + "/" + str(count + 40 * (x - 1)) + ".jpg" cv2.imwrite(new_file, new_image) print(count + 40 * (x - 1)) count += 1 for i in range(1, 8 + 1):#8 _image = utils.foo(utils.rotate_image(image, random.uniform(0, 360))) new_image = utils.contras_image(_image, random.uniform(80, 200) / 100, random.uniform(0, 50) / 10) new_file = new_dir + "/" + str(count + 40 * (x - 1)) + ".jpg" cv2.imwrite(new_file, new_image) print(count + 40 * (x - 1)) count += 1 for i in range(1, 7 + 1): #7 _image = utils.foo(utils.rotate_image(image, random.uniform(0, 360))) new_image = utils.random_shadow(_image) new_file = new_dir + "/" + str(count + 40 * (x - 1)) + ".jpg"