def video2posevideo(video_name):
    time_start = time.clock()
    import numpy as np
    sys.path.append((os.path.dirname(__file__) + '/../'))
    from scipy.misc import imread, imsave
    from config import load_config
    from dataset.factory import create as create_dataset
    from nnet import predict
    from util import visualize
    from dataset.pose_dataset import data_to_input
    from multiperson.detections import extract_detections
    from multiperson.predict import SpatialModel, eval_graph, get_person_conf_multicut
    from multiperson.visualize import PersonDraw, visualize_detections
    import matplotlib.pyplot as plt
    from PIL import Image, ImageDraw, ImageFont
    font = ImageFont.truetype('./font/NotoSans-Bold.ttf', 24)
    import random
    cfg = load_config('demo/pose_cfg_multi.yaml')
    dataset = create_dataset(cfg)
    sm = SpatialModel(cfg)
    sm.load()
    draw_multi = PersonDraw()
    (sess, inputs, outputs) = predict.setup_pose_prediction(cfg)
    video = read_video(video_name)
    video_frame_number = int((video.duration * video.fps))
    video_frame_ciphers = math.ceil(math.log(video_frame_number, 10))
    pose_frame_list = []
    point_r = 3
    point_min = 10
    part_min = 3
    point_num = 17

    def ellipse_set(person_conf_multi, people_i, point_i):
        return ((person_conf_multi[people_i][point_i][0] - point_r), (person_conf_multi[people_i][point_i][1] - point_r), (person_conf_multi[people_i][point_i][0] + point_r), (person_conf_multi[people_i][point_i][1] + point_r))

    def line_set(person_conf_multi, people_i, point_i, point_j):
        return (person_conf_multi[people_i][point_i][0], person_conf_multi[people_i][point_i][1], person_conf_multi[people_i][point_j][0], person_conf_multi[people_i][point_j][1])

    def draw_ellipse_and_line(draw, person_conf_multi, people_i, a, b, c, point_color):
        draw.ellipse(ellipse_set(person_conf_multi, people_i, a), fill=point_color)
        draw.ellipse(ellipse_set(person_conf_multi, people_i, b), fill=point_color)
        draw.ellipse(ellipse_set(person_conf_multi, people_i, c), fill=point_color)
        draw.line(line_set(person_conf_multi, people_i, a, b), fill=point_color, width=5)
        draw.line(line_set(person_conf_multi, people_i, b, c), fill=point_color, width=5)
    for i in range(0, video_frame_number):
        image = video.get_frame((i / video.fps))
        image_batch = data_to_input(image)
        outputs_np = sess.run(outputs, feed_dict={inputs: image_batch})
        (scmap, locref, pairwise_diff) = predict.extract_cnn_output(outputs_np, cfg, dataset.pairwise_stats)
        detections = extract_detections(cfg, scmap, locref, pairwise_diff)
        (unLab, pos_array, unary_array, pwidx_array, pw_array) = eval_graph(sm, detections)
        person_conf_multi = get_person_conf_multicut(sm, unLab, unary_array, pos_array)
        image_img = Image.fromarray(image)
        draw = ImageDraw.Draw(image_img)
        people_num = 0
        people_real_num = 0
        people_part_num = 0
        people_num = (person_conf_multi.size / (point_num * 2))
        people_num = int(people_num)
        print(('people_num: ' + str(people_num)))
        for people_i in range(0, people_num):
            point_color_r = random.randrange(0, 256)
            point_color_g = random.randrange(0, 256)
            point_color_b = random.randrange(0, 256)
            point_color = (point_color_r, point_color_g, point_color_b, 255)
            point_list = []
            point_count = 0
            point_i = 0
            part_count = 0
            people_x = []
            people_y = []
            for point_i in range(0, point_num):
                if ((person_conf_multi[people_i][point_i][0] + person_conf_multi[people_i][point_i][1]) != 0):
                    point_count = (point_count + 1)
                    point_list.append(point_i)
            if ((5 in point_list) and (7 in point_list) and (9 in point_list)):
                draw_ellipse_and_line(draw, person_conf_multi, people_i, 5, 7, 9, point_color)
                part_count = (part_count + 1)
            if ((6 in point_list) and (8 in point_list) and (10 in point_list)):
                draw_ellipse_and_line(draw, person_conf_multi, people_i, 6, 8, 10, point_color)
                part_count = (part_count + 1)
            if ((11 in point_list) and (13 in point_list) and (15 in point_list)):
                draw_ellipse_and_line(draw, person_conf_multi, people_i, 11, 13, 15, point_color)
                part_count = (part_count + 1)
            if ((12 in point_list) and (14 in point_list) and (16 in point_list)):
                draw_ellipse_and_line(draw, person_conf_multi, people_i, 12, 14, 16, point_color)
                part_count = (part_count + 1)
            if (point_count >= point_min):
                people_real_num = (people_real_num + 1)
                for point_i in range(0, point_num):
                    if ((person_conf_multi[people_i][point_i][0] + person_conf_multi[people_i][point_i][1]) != 0):
                        draw.ellipse(ellipse_set(person_conf_multi, people_i, point_i), fill=point_color)
                        people_x.append(person_conf_multi[people_i][point_i][0])
                        people_y.append(person_conf_multi[people_i][point_i][1])
                draw.rectangle([min(people_x), min(people_y), max(people_x), max(people_y)], fill=point_color, outline=5)
            if (part_count >= part_min):
                people_part_num = (people_part_num + 1)
        draw.text((0, 0), (((('People(by point): ' + str(people_real_num)) + ' (threshold = ') + str(point_min)) + ')'), (0, 0, 0), font=font)
        draw.text((0, 32), (((('People(by line): ' + str(people_part_num)) + ' (threshold = ') + str(part_min)) + ')'), (0, 0, 0), font=font)
        draw.text((0, 64), ((('Frame: ' + str(i)) + '/') + str(video_frame_number)), (0, 0, 0), font=font)
        draw.text((0, 96), (('Total time required: ' + str(round((time.clock() - time_start), 1))) + 'sec'), (0, 0, 0))
        print(('people_real_num: ' + str(people_real_num)))
        print(('people_part_num: ' + str(people_part_num)))
        print(('frame: ' + str(i)))
        image_img_numpy = np.asarray(image_img)
        pose_frame_list.append(image_img_numpy)
    video_pose = ImageSequenceClip(pose_frame_list, fps=video.fps)
    video_pose.write_videofile((('testset/' + video_name) + '_pose.mp4'), fps=video.fps)
    print(('Time(s): ' + str((time.clock() - time_start))))
def video2posevideo(video_name):
    time_start = time.clock()
    import numpy as np
    sys.path.append((os.path.dirname(__file__) + '/../'))
    from scipy.misc import imread, imsave
    from config import load_config
    from dataset.factory import create as create_dataset
    from nnet import predict
    from util import visualize
    from dataset.pose_dataset import data_to_input
    from multiperson.detections import extract_detections
    from multiperson.predict import SpatialModel, eval_graph, get_person_conf_multicut
    from multiperson.visualize import PersonDraw, visualize_detections
    import matplotlib.pyplot as plt
    from PIL import Image, ImageDraw, ImageFont
    font = ImageFont.truetype('./font/NotoSans-Bold.ttf', 24)
    import random
    cfg = load_config('demo/pose_cfg_multi.yaml')
    dataset = create_dataset(cfg)
    sm = SpatialModel(cfg)
    sm.load()
    draw_multi = PersonDraw()
    (sess, inputs, outputs) = predict.setup_pose_prediction(cfg)
    video = read_video(video_name)
    video_frame_number = int((video.duration * video.fps))
    video_frame_ciphers = math.ceil(math.log(video_frame_number, 10))
    pose_frame_list = []
    point_r = 3
    point_min = 10
    part_min = 3
    point_num = 17

    def ellipse_set(person_conf_multi, people_i, point_i):
        return ((person_conf_multi[people_i][point_i][0] - point_r), (person_conf_multi[people_i][point_i][1] - point_r), (person_conf_multi[people_i][point_i][0] + point_r), (person_conf_multi[people_i][point_i][1] + point_r))

    def line_set(person_conf_multi, people_i, point_i, point_j):
        return (person_conf_multi[people_i][point_i][0], person_conf_multi[people_i][point_i][1], person_conf_multi[people_i][point_j][0], person_conf_multi[people_i][point_j][1])

    def draw_ellipse_and_line(draw, person_conf_multi, people_i, a, b, c, point_color):
        draw.ellipse(ellipse_set(person_conf_multi, people_i, a), fill=point_color)
        draw.ellipse(ellipse_set(person_conf_multi, people_i, b), fill=point_color)
        draw.ellipse(ellipse_set(person_conf_multi, people_i, c), fill=point_color)
        draw.line(line_set(person_conf_multi, people_i, a, b), fill=point_color, width=5)
        draw.line(line_set(person_conf_multi, people_i, b, c), fill=point_color, width=5)
    for i in range(0, video_frame_number):
        image = video.get_frame((i / video.fps))
        image_batch = data_to_input(image)
        outputs_np = sess.run(outputs, feed_dict={inputs: image_batch})
        (scmap, locref, pairwise_diff) = predict.extract_cnn_output(outputs_np, cfg, dataset.pairwise_stats)
        detections = extract_detections(cfg, scmap, locref, pairwise_diff)
        (unLab, pos_array, unary_array, pwidx_array, pw_array) = eval_graph(sm, detections)
        person_conf_multi = get_person_conf_multicut(sm, unLab, unary_array, pos_array)
        image_img = Image.fromarray(image)
        draw = ImageDraw.Draw(image_img)
        people_num = 0
        people_real_num = 0
        people_part_num = 0
        people_num = (person_conf_multi.size / (point_num * 2))
        people_num = int(people_num)
        print(('people_num: ' + str(people_num)))
        for people_i in range(0, people_num):
            point_color_r = random.randrange(0, 256)
            point_color_g = random.randrange(0, 256)
            point_color_b = random.randrange(0, 256)
            point_color = (point_color_r, point_color_g, point_color_b, 255)
            point_list = []
            point_count = 0
            point_i = 0
            part_count = 0
            people_x = []
            people_y = []
            for point_i in range(0, point_num):
                if ((person_conf_multi[people_i][point_i][0] + person_conf_multi[people_i][point_i][1]) != 0):
                    point_count = (point_count + 1)
                    point_list.append(point_i)
            if ((5 in point_list) and (7 in point_list) and (9 in point_list)):
                draw_ellipse_and_line(draw, person_conf_multi, people_i, 5, 7, 9, point_color)
                part_count = (part_count + 1)
            if ((6 in point_list) and (8 in point_list) and (10 in point_list)):
                draw_ellipse_and_line(draw, person_conf_multi, people_i, 6, 8, 10, point_color)
                part_count = (part_count + 1)
            if ((11 in point_list) and (13 in point_list) and (15 in point_list)):
                draw_ellipse_and_line(draw, person_conf_multi, people_i, 11, 13, 15, point_color)
                part_count = (part_count + 1)
            if ((12 in point_list) and (14 in point_list) and (16 in point_list)):
                draw_ellipse_and_line(draw, person_conf_multi, people_i, 12, 14, 16, point_color)
                part_count = (part_count + 1)
            if (point_count >= point_min):
                people_real_num = (people_real_num + 1)
                for point_i in range(0, point_num):
                    if ((person_conf_multi[people_i][point_i][0] + person_conf_multi[people_i][point_i][1]) != 0):
                        draw.ellipse(ellipse_set(person_conf_multi, people_i, point_i), fill=point_color)
                        people_x.append(person_conf_multi[people_i][point_i][0])
                        people_y.append(person_conf_multi[people_i][point_i][1])
                draw.rectangle([min(people_x), min(people_y), max(people_x), max(people_y)], fill=point_color, outline=5)
            if (part_count >= part_min):
                people_part_num = (people_part_num + 1)
        draw.text((0, 0), (((('People(by point): ' + str(people_real_num)) + ' (threshold = ') + str(point_min)) + ')'), (0, 0, 0), font=font)
        draw.text((0, 32), (((('People(by line): ' + str(people_part_num)) + ' (threshold = ') + str(part_min)) + ')'), (0, 0, 0), font=font)
        draw.text((0, 64), ((('Frame: ' + str(i)) + '/') + str(video_frame_number)), (0, 0, 0), font=font)
        draw.text((0, 96), (('Total time required: ' + str(round((time.clock() - time_start), 1))) + 'sec'), (0, 0, 0))
        print(('people_real_num: ' + str(people_real_num)))
        print(('people_part_num: ' + str(people_part_num)))
        print(('frame: ' + str(i)))
        image_img_numpy = np.asarray(image_img)
        pose_frame_list.append(image_img_numpy)
    video_pose = ImageSequenceClip(pose_frame_list, fps=video.fps)
    video_pose.write_videofile((('testset/' + video_name) + '_pose.mp4'), fps=video.fps)
    print(('Time(s): ' + str((time.clock() - time_start))))