# Save true bitmap GIF and TXT to directory
            true_txt_url = true_image_url.replace(".gif",".txt")

            true_image_file = "true_img_{:04d}.gif".format(i)
            true_txt_file = "true_img_{:04d}.txt".format(i)
            urllib.urlretrieve (true_image_url, "{}/{}".format(args.output_dir,true_image_file))
            urllib.urlretrieve (true_txt_url, "{}/{}".format(args.output_dir,true_txt_file))

            # Make GIF from draw events
            bmpmaker.clear()
            bmpmaker.process_commands_str(draw_events)
            bitmap = bmpmaker.bitmap
            drawn_image_file = "drawn_img_{:04d}.gif".format(i)
            drawn_image_save_path = "{}/{}".format(args.output_dir,drawn_image_file)
            imgmaker.save_bitmap(bitmap, drawn_image_save_path)

            # Make TXT from draw events
            drawn_txt_file = "drawn_img_{:04d}.txt".format(i)
            drawn_txt_save_path = "{}/{}".format(args.output_dir,drawn_txt_file)
            np.savetxt(drawn_txt_save_path, bitmap, fmt='%d')

            # Save command text and generated events text too
            commands_txt_file = "written_commands_{:04d}.txt".format(i)
            events_txt_file = "draw_events_{:04d}.txt".format(i)
            save_txt_to_file(commands.replace("<br>","\n"), "{}/{}".format(args.output_dir,commands_txt_file))
            save_txt_to_file(draw_events, "{}/{}".format(args.output_dir,events_txt_file))

            
            html_lines.append("<h2>Worker: #{:03d} ({}) (WORKER_{:03d}_NUM_HITS hits) </h2>".format(worker_index,worker_id,worker_index))
            html_lines.append("<b>Image: {}</b> <br>".format(true_image_url))
    parser.add_argument("-dim", type=int, default=5, help="Dimensionality of grid")
    parser.add_argument("-csv", type=str, required=True, help="input CSV file with drawing task results")
    parser.add_argument("-field", type=str, default="Answer.WritingTexts", help="Name of CSV field containing draw events")
    parser.add_argument("-output_dir", type=str, default="output/make_images_from_drawing_task_results", help="output directory for images")
    args = parser.parse_args()

    imgmaker = ImageMaker(args.width, args.padding)
    bmpmaker = BitmapMaker(args.width, args.width)
    np.random.seed(0)

    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)


    with open(args.csv) as fin:
        reader = csv.reader(fin)
        header = next(reader)
        colindex = header.index(args.field)
        for i,row in enumerate(reader):
            commands_str = row[colindex]
            bmpmaker.clear()
            event_sequence = AbsoluteEventSequence.from_mturk_string(commands_str)
            event_sequence = event_sequence.canonicalize()
            canonical_events = event_sequence.events
            bmpmaker.process_commands(canonical_events)
            bitmap = bmpmaker.bitmap
            bitmap_file = "{}/img_{:04d}.gif".format(args.output_dir,i)
            imgmaker.save_bitmap(bitmap, bitmap_file)


Ejemplo n.º 3
0
                        default="Answer.WritingTexts",
                        help="Name of CSV field containing draw events")
    parser.add_argument("-output_dir",
                        type=str,
                        default="output/make_images_from_drawing_task_results",
                        help="output directory for images")
    args = parser.parse_args()

    imgmaker = ImageMaker(args.width, args.padding)
    bmpmaker = BitmapMaker(args.width, args.width)
    np.random.seed(0)

    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    with open(args.csv) as fin:
        reader = csv.reader(fin)
        header = next(reader)
        colindex = header.index(args.field)
        for i, row in enumerate(reader):
            commands_str = row[colindex]
            bmpmaker.clear()
            event_sequence = AbsoluteEventSequence.from_mturk_string(
                commands_str)
            event_sequence = event_sequence.canonicalize()
            canonical_events = event_sequence.events
            bmpmaker.process_commands(canonical_events)
            bitmap = bmpmaker.bitmap
            bitmap_file = "{}/img_{:04d}.gif".format(args.output_dir, i)
            imgmaker.save_bitmap(bitmap, bitmap_file)
            true_image_file = "true_img_{:04d}.gif".format(i)
            true_txt_file = "true_img_{:04d}.txt".format(i)
            urllib.urlretrieve(
                true_image_url, "{}/{}".format(args.output_dir,
                                               true_image_file))
            urllib.urlretrieve(true_txt_url,
                               "{}/{}".format(args.output_dir, true_txt_file))

            # Make GIF from draw events
            bmpmaker.clear()
            bmpmaker.process_commands_str(draw_events)
            bitmap = bmpmaker.bitmap
            drawn_image_file = "drawn_img_{:04d}.gif".format(i)
            drawn_image_save_path = "{}/{}".format(args.output_dir,
                                                   drawn_image_file)
            imgmaker.save_bitmap(bitmap, drawn_image_save_path)

            # Make TXT from draw events
            drawn_txt_file = "drawn_img_{:04d}.txt".format(i)
            drawn_txt_save_path = "{}/{}".format(args.output_dir,
                                                 drawn_txt_file)
            np.savetxt(drawn_txt_save_path, bitmap, fmt='%d')

            # Save command text and generated events text too
            commands_txt_file = "written_commands_{:04d}.txt".format(i)
            events_txt_file = "draw_events_{:04d}.txt".format(i)
            save_txt_to_file(
                commands.replace("<br>", "\n"),
                "{}/{}".format(args.output_dir, commands_txt_file))
            save_txt_to_file(draw_events,
                             "{}/{}".format(args.output_dir, events_txt_file))