-
Notifications
You must be signed in to change notification settings - Fork 964
Reconstruct high quality 28x28 .npy files from binary files #19
Comments
The blurring is caused by antialiasing. The drawings were drawn with the follow settings: surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 28, 28)
ctx = cairo.Context(surface)
ctx.set_antialias(cairo.ANTIALIAS_BEST)
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
ctx.set_line_width(16) It was based on the simplified drawings, scaled from size 256x256 to 28x28. Hope this helps |
The full function looks like this: import numpy as np
import cairocffi as cairo
def vector_to_raster(vector_images, side=28, line_diameter=16, padding=16, bg_color=(0,0,0), fg_color=(1,1,1)):
"""
padding and line_diameter are relative to the original 256x256 image.
"""
original_side = 256.
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, side, side)
ctx = cairo.Context(surface)
ctx.set_antialias(cairo.ANTIALIAS_BEST)
ctx.set_line_cap(cairo.LINE_CAP_ROUND)
ctx.set_line_join(cairo.LINE_JOIN_ROUND)
ctx.set_line_width(line_diameter)
# scale to match the new size
# add padding at the edges for the line_diameter
# and add additional padding to account for antialiasing
total_padding = padding * 2. + line_diameter
new_scale = float(side) / float(original_side + total_padding)
ctx.scale(new_scale, new_scale)
ctx.translate(total_padding / 2., total_padding / 2.)
raster_images = []
for vector_image in vector_images:
# clear background
ctx.set_source_rgb(*bg_color)
ctx.paint()
bbox = np.hstack(vector_image).max(axis=1)
offset = ((original_side, original_side) - bbox) / 2.
offset = offset.reshape(-1,1)
centered = [stroke + offset for stroke in vector_image]
# draw strokes, this is the most cpu-intensive part
ctx.set_source_rgb(*fg_color)
for xv, yv in centered:
ctx.move_to(xv[0], yv[0])
for x, y in zip(xv, yv):
ctx.line_to(x, y)
ctx.stroke()
data = surface.get_data()
raster_image = np.copy(np.asarray(data)[::4])
raster_images.append(raster_image)
return raster_images |
Thank you so much! |
Closing issue. Feel free to re-open if there are more questions or issues with this |
hi,man,thank you for your code for reconstruct high quality image. But i don't konw the input parameter vector_images' format,can you help me?@ HalfdanJ |
The vector_images I believe would be an array of drawings from the simplified dataset (the vector strokes). The part that looks like this
|
got it,thank you |
@HalfdanJ I am sorry to trouble you again. when i input the vector strokes,i get output raster_images as a List like [array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
@HalfdanJ Can you please also provide the conversion code for using saved .jpg/.png drawing files just like the above bitmap ? Trained the model using .npy data and testing/eval runs fine but when using saved drawings, predictions are off and I am pretty sure its to do with how I am trying to resize and process the image, since it does not look like the ones in npy Here is what I am using
|
I am still getting error when I pass array from Drawning(simplified ndjson) to vector_images axis 1 is out of bounds for array of dimension 1 |
@maaciekz Were you able to solve this? |
not really:( I have developed my solution. It reads simplified ndjson provided by google and generates image. Hope it will help: import jsonlines file = open('full_simplified_yoga.ndjson',encoding='utf-8') allScratches = jsonlines.Reader(file) def add_points(obj): def paint_lines(obj):
def merge_dots_and_lines(scratch, lines): def save_training_img(final, name): for obj in allScratches: |
Hi so I am getting the following error when trying to run the converter function: I am using the following data for running the functions:
Am I doing something wrong? |
This comment was marked as off-topic.
This comment was marked as off-topic.
@Randulfe @maaciekz
Therefore, passing a single vector image as |
Changed the line diameter to 16 to match the original dataset. Reference: googlecreativelab/quickdraw-dataset#19 (comment)
First off, thank you so much for providing such a helpful dataset, it truly is a goldmine!
I am having trouble reconstructing the 28x28 images from the binary files provided in this dataset.
What are the actual steps in order to reconstruct the 28x28 images in the provided quality from the binary files?
My issue is quite similar to #15 but I managed to get intermediary results.
Here is my current progress:
Using the
examples/binary_file_parser.py
I am able to reconstruct the image in any given size by handling the stroke paths.I am also able to use some blurring to smooth the image.
However the quality of the reconstructed image is nowhere near the 28x28 images dataset provided in this repository.
This is an original image from the 28x28 .npy dataset:
![original_npy](https://user-images.githubusercontent.com/17804379/42219777-56de0c10-7ecd-11e8-8448-8d61867ee9b9.png)
This is my reconstruction using no blurring technique:
![reconstruction_no_blur](https://user-images.githubusercontent.com/17804379/42219832-7e0e7d74-7ecd-11e8-8b9f-5fa665be583e.png)
And this is my reconstruction using a (2, 2) blur kernel in OpenCV:
![reconstruction_blur_2](https://user-images.githubusercontent.com/17804379/42219851-8d29235e-7ecd-11e8-84a8-3238b415dbb2.png)
Any idea on how to reconstruct these images in the quality that is available when downloading the 28x28 .npy files?
Is there some more advanced smoothing and filtering techniques that I have been missing?
The text was updated successfully, but these errors were encountered: