如何在 Python 中绘制 Google QuickDraw 数据集?
How to plot the Google QuickDraw Dataset in Python?
我已经从 QuickDraw 数据集中提取了笔画数据(JSON 格式),但我不知道如何在 Python 中绘制它。我尝试了几次,但根本不起作用。我需要帮助绘制 Python 中的笔画。以下是我提取笔画数据的代码:
import json
file = open(filepath)
lines = file.readlines()
data = json.loads(lines[0])
strokes = data['drawing']
x = strokes[0][0]
y = strokes[0][1]
以下文本是数据文件的第一行:
{"word":"airplane","countrycode":"US","timestamp":"2017-03-08 21:12:07.26604 UTC","recognized":true,"key_id":"5152802093400064","drawing":[[[167,109,80,69,58,31,57,117,99,52,30,6,1,2,66,98,253,254,246,182,165],[140,194,227,232,229,229,206,124,123,149,157,159,153,110,82,77,74,109,121,127,120]],[[207,207,210,221,238],[74,103,114,128,135]],[[119,107,76,70,49,39,60,93],[72,41,3,0,1,5,38,70]]]}
包含了所有的数据预处理,训练,测试数据。
除了 Swati 的回答之外,还有一个 quickdraw
python package 让这变得简单:
from quickdraw import QuickDrawData
qd = QuickDrawData()
anvil = qd.get_drawing("anvil")
anvil.image.show()
如果您想手动执行,您可以 zip()
所有 x 值和 y 值并渲染(使用 PIL 或其他任何东西):
import json
from PIL import Image, ImageDraw
# read ndjson lines
lines = open('full_simplified_cat.ndjson','r').readlines()
# grab the first line, JSON parse it and fetch the 'drawing' array
raw_drawing = json.loads(lines[0])['drawing']
print('before',raw_drawing)
# zip x,y coordinates for each point in every polyline
polylines = (zip(polyline[0], polyline[1]) for polyline in raw_drawing)
# notice how the data is shuffled to (x1,y1),(x2,y2) order
print('after',polylines)
# make a new image
pil_img = Image.new("RGB", (240, 270), (255,255,255))
# get a drawing context
d = ImageDraw.Draw(pil_img)
# render each polyline
for polyline in polylines:
d.line(polyline,fill=(0, 0, 0),width=3)
# display image
pil_img.show()
我已经从 QuickDraw 数据集中提取了笔画数据(JSON 格式),但我不知道如何在 Python 中绘制它。我尝试了几次,但根本不起作用。我需要帮助绘制 Python 中的笔画。以下是我提取笔画数据的代码:
import json
file = open(filepath)
lines = file.readlines()
data = json.loads(lines[0])
strokes = data['drawing']
x = strokes[0][0]
y = strokes[0][1]
以下文本是数据文件的第一行:
{"word":"airplane","countrycode":"US","timestamp":"2017-03-08 21:12:07.26604 UTC","recognized":true,"key_id":"5152802093400064","drawing":[[[167,109,80,69,58,31,57,117,99,52,30,6,1,2,66,98,253,254,246,182,165],[140,194,227,232,229,229,206,124,123,149,157,159,153,110,82,77,74,109,121,127,120]],[[207,207,210,221,238],[74,103,114,128,135]],[[119,107,76,70,49,39,60,93],[72,41,3,0,1,5,38,70]]]}
包含了所有的数据预处理,训练,测试数据。
除了 Swati 的回答之外,还有一个 quickdraw
python package 让这变得简单:
from quickdraw import QuickDrawData
qd = QuickDrawData()
anvil = qd.get_drawing("anvil")
anvil.image.show()
如果您想手动执行,您可以 zip()
所有 x 值和 y 值并渲染(使用 PIL 或其他任何东西):
import json
from PIL import Image, ImageDraw
# read ndjson lines
lines = open('full_simplified_cat.ndjson','r').readlines()
# grab the first line, JSON parse it and fetch the 'drawing' array
raw_drawing = json.loads(lines[0])['drawing']
print('before',raw_drawing)
# zip x,y coordinates for each point in every polyline
polylines = (zip(polyline[0], polyline[1]) for polyline in raw_drawing)
# notice how the data is shuffled to (x1,y1),(x2,y2) order
print('after',polylines)
# make a new image
pil_img = Image.new("RGB", (240, 270), (255,255,255))
# get a drawing context
d = ImageDraw.Draw(pil_img)
# render each polyline
for polyline in polylines:
d.line(polyline,fill=(0, 0, 0),width=3)
# display image
pil_img.show()