Ejemplo n.º 1
0
    def add_background(self, back_img, patch_location, mode='resize'):
        '''
		add random background to a specific path in the image

		parameters:
			back_img:				integer image
			patch_location:			[x_left, y_lower, x_right, y_upper], a location of the patch
			mode:					trun, keep, resize
		'''
        if isstring(back_img):
            back_img = load_image(back_img, mode='numpy', debug=self.debug)

        if self.debug:
            assert isuintimage(
                back_img), 'the input background image is not correct'
            assert patch_location[0] >= 0 and patch_location[
                1] >= 0, 'the input patch location is not correct'
            assert patch_location[2] <= self.width and patch_location[
                3] <= self.height, 'the input patch location is not correct'
            assert patch_location[2] >= patch_location[0] and patch_location[
                3] >= patch_location[
                    1], 'the input patch location is not correct'
            assert mode == 'trim' or mode == 'resize', 'the input mode is not correct'

        if mode == 'resize':
            back_img = imresize(back_img,
                                (patch_location[3] - patch_location[1],
                                 patch_location[2] - patch_location[0], 3))
            self.img[patch_location[1]:patch_location[3],
                     patch_location[0]:patch_location[2], :] = np.copy(
                         back_img)
Ejemplo n.º 2
0
def generate_video_from_list(image_list, save_path, framerate=30, downsample=1, warning=True, debug=True):
	'''
	create video from a list of images with a framerate
	note that: the height and widht of the images should be a multiple of 2

	parameters:
		image_list:			a list of image path
		save_path:			the path to save the video file
		framerate:			fps 
	'''
	if debug: 
		assert islistofstring(image_list), 'the input is not correct'
		assert ispositiveinteger(framerate), 'the framerate is a positive integer'
	mkdir_if_missing(save_path)
	inputdict = {'-r': str(framerate)}
	outputdict = {'-r': str(framerate), '-crf': '18', '-vcodec': 'libx264', '-profile:V': 'high', '-pix_fmt': 'yuv420p'}
	video_writer = FFmpegWriter(save_path, inputdict=inputdict, outputdict=outputdict)
	count = 1
	num_images = len(image_list)
	for image_path in image_list:
		print('processing frame %d/%d' % (count, num_images))
		image = load_image(image_path, resize_factor=downsample, warning=warning, debug=debug)

		# make sure the height and width are multiple of 2
		height, width = image.shape[0], image.shape[1]
		if not (height % 2 == 0 and width % 2 == 0):
			height += height % 2
			width += width % 2
			image = image_resize(image, target_size=[height, width], warning=warning, debug=debug)

		video_writer.writeFrame(image)
		count += 1

	video_writer.close()
def test_triangulate_multiple_views():
    calibration_file = '../calibration.txt'
    camera_cluster = load_camera_cluster(calibration_file, warning=False)

    pts1_file = '../20180514--handsy--400015--0800.pts'
    pts_array1 = anno_parser(pts1_file)
    pts2_file = '../20180514--handsy--400053--0800.pts'
    pts_array2 = anno_parser(pts2_file)
    pts3_file = '../20180514--handsy--400025--0800.pts'
    pts_array3 = anno_parser(pts3_file)
    pts4_file = '../20180514--handsy--400027--0800.pts'
    pts_array4 = anno_parser(pts4_file)
    pts_array = np.zeros((4, 3, 21), dtype='float32')
    # print(pts_array)
    pts_array[0, :, :] = pts_array1
    # print(pts_array)
    pts_array[1, :, :] = pts_array2
    # print(pts_array)
    pts_array[2, :, :] = pts_array3
    pts_array[3, :, :] = pts_array4
    # pts_array = np.vstack((pts_array1, pts_array2, pts_array3, pts_array4))
    projection1 = camera_cluster['400015'].get_projection_matrix()
    projection2 = camera_cluster['400053'].get_projection_matrix()
    projection3 = camera_cluster['400025'].get_projection_matrix()
    projection4 = camera_cluster['400027'].get_projection_matrix()
    projection = np.zeros((4, 3, 4), dtype='float32')
    projection[0, :, :] = projection1
    projection[1, :, :] = projection2
    projection[2, :, :] = projection3
    projection[3, :, :] = projection4
    # projection = np.vstack((projection1, projection2, projection3, projection4))
    # print(pts_array.shape)

    # pts_3d, pts_reproj = triangulate_multiple_views(pts_array, projection, scaling_factor=1000)
    pts_3d, pts_reproj = triangulate_multiple_views(pts_array,
                                                    projection,
                                                    scaling_factor=1)
    img_path = '../20180514--handsy--400015--0800.png'
    # img_path = '../20180514--handsy--400053--0800.png'
    # img_path = '../20180514--handsy--400025--0800.png'
    # img_path = '../20180514--handsy--400027--0800.png'
    img = load_image(img_path)
    debug = False
    fig, ax = visualize_image_with_pts(img,
                                       pts_array[0, :, :],
                                       label=True,
                                       debug=debug,
                                       closefig=False,
                                       vis=True)
    fig, ax = visualize_image_with_pts(img,
                                       pts_reproj[0, :, :],
                                       label=True,
                                       debug=debug,
                                       closefig=False,
                                       vis=True)
    # visualize_pts_array(pts_reproj[0, :], fig=fig, ax=ax, color_index=1, pts_size=20, label=True, vis=True, debug=debug)

    print('\n\nDONE! SUCCESSFUL!!\n')
Ejemplo n.º 4
0
    def __init__(self, img, debug=True):
        '''
		generate synthetic data on a given image, the image should be an numpy array
		'''
        if isstring(img):
            img = load_image(img, mode='numpy', debug=debug)

        if debug:
            assert isuintimage(img), 'the input image is not correct'

        self.debug = debug
        self.img = img
        self.width = img.shape[1]
        self.height = img.shape[0]

        self.__original = np.copy(img)
def test_triangulate_two_views():
    calibration_file = '../calibration.txt'
    camera_cluster = load_camera_cluster(calibration_file, warning=False)

    pts1_file = '../20180514--handsy--400015--0800.pts'
    pts_array1 = anno_parser(pts1_file)
    pts2_file = '../20180514--handsy--400053--0800.pts'
    pts_array2 = anno_parser(pts2_file)
    projection1 = camera_cluster['400015'].get_projection_matrix()
    projection2 = camera_cluster['400053'].get_projection_matrix()

    # pts_array1 = np.array([[54], [183], [1]]).astype('float32')
    # projection1 = np.array([[1520.4, 0, 302.3, 0], [0, 1525.9, 246.9, 0], [0, 0, 1, 0]])
    # pts_array2 = np.array([[54], [199], [1]])
    # projection2 = np.array([[1520.9, -27.4, 298.7, 5.3], [-49.3, 1410.6, 630.2, -1488.8], [0, -0.3, 1, 0.2]])

    pts_3d, pts1_reproj, pts2_reproj = triangulate_two_views(pts_array1,
                                                             pts_array2,
                                                             projection1,
                                                             projection2,
                                                             scaling_factor=1)
    img_path = '../20180514--handsy--400015--0800.png'
    img = load_image(img_path)
    debug = False
    fig, ax = visualize_image_with_pts(img,
                                       pts_array1,
                                       label=True,
                                       debug=debug,
                                       closefig=False,
                                       vis=True)
    visualize_image_with_pts(img,
                             pts1_reproj,
                             label=True,
                             debug=debug,
                             closefig=False,
                             vis=True)
    # visualize_pts_array(pts1_reproj, fig=fig, ax=ax, color_index=1, pts_size=20, label=True, vis=True, debug=debug)
    print('\n\nDONE! SUCCESSFUL!!\n')
save_dir = os.path.join(data_dir, 'centered122_flow/ABOUT/train/ABOUT_00001'); mkdir_if_missing(save_dir)

image_list, num_images = load_list_from_folder(images_dir)
print('number of images loaded is %d' % num_images)


test = cv2.imread(image_list[0])
# print(test.dtype)
test = cv2.cvtColor(test, cv2.COLOR_BGR2GRAY)
# print(test.dtype)
# print(test)
# zxc


frame1 = load_image(image_list[0])
prvs = rgb2gray(frame1)         # uint8

# print(prvs)

hsv = np.zeros_like(frame1)
hsv[..., 1] = 255

image_index = 1
while(1):
    image_path = image_list[image_index]
    _, filename, _ = fileparts(image_path)

    frame2 = load_image(image_path)
    next = rgb2gray(frame2)
    # save_image(next, '/home/xinshuo/aa.jpg')
Ejemplo n.º 7
0
##--------------------------------- Testing ----------------------------------##
image_list, num_list = load_list_from_folder(images_dir,
                                             ext_filter=['.png', '.jpg'],
                                             depth=2)
print_log('testing results on %d images' % num_list, log=log_file)
count = 1
timer = Timer()
timer.tic()
for image_file_tmp in image_list:
    parent_dir, filename, _ = fileparts(image_file_tmp)
    video_dir = parent_dir.split('/')[-1]
    # print(video_dir)
    # zxc

    image = load_image(image_file_tmp)
    results = model.detect([image])  # inference, results is a dictionary
    if len(results) == 0:
        count += 1
        print_log('Mask-RCNN demo: testing %d/%d, no detected results!!!!!' %
                  (count, num_list),
                  log=log_file)
        continue

    # visualize and save results
    r = results[0]  # results from the first image
    num_instances = r['masks'].shape[-1]
    bboxes_tmp = r['rois']  # y1, x1, y2, x2 format
    bboxes_tmp[:, [0, 1]] = bboxes_tmp[:, [1, 0]]
    bboxes_tmp[:, [2, 3]] = bboxes_tmp[:, [3, 2]]  # x1, y1, x2, y2 format