-
Notifications
You must be signed in to change notification settings - Fork 4
/
neosmartpen.py
174 lines (144 loc) · 4.78 KB
/
neosmartpen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os, sys, re, glob
import struct
from zipfile import ZipFile
def parse_pagedata(raw):
'''
Parse the binary page.data format into python structure
'''
# Spec from https://github.com/NeoSmartpen/Documentations/blob/master/NeoNote_data_Eng_V1.0.pdf
# Appears to be little endian
# page.data:
#
# bytes desc
# 3 'neo' (verification string)
# 4 File version
# 4 Note type
# 4 Page Number
# 4 (Float 32) notebook width
# 4 (Float 32) notebook height
# 8 created timestamp (millisecond)
# 8 modified timestamp (millisecond)
# 1 Flag if data modified
# 4 number of strokes
# ... stroke data ...
# 4 Length of guid string
# * Page guid string (for evernote?)
#
# stroke:
# 1 Type (0=stroke, 1=voice memo)
# 4 Color 32 bits RGBA
# 1 Thickness (0,1,2)
# 4 Number of dots
# 8 Start timestamp (millisecond)
# ... dots ...
# 1 Length of extra data (0-255)
# ... extra data ... (Pen type: 0=Neo smart pen, 1=pem from neo notes, 2=highlighter from neo notes)
#
# dot data 13 bytes:
# 4 x (float 32)
# 4 y (Float 32)
# 4 pressure (Float 32)
# 1 time diff from previous dot
#
# x and y where normalised by max(width,height)
o = struct.unpack('<3s3i2f2Q?I', raw[:44])
if o[0]!=b'neo':
raise Exception("Not a valid neopen data file")
data = {'neo':o[0], 'file_version':o[1], 'note_type':o[2], 'page':o[3],
'width':o[4], 'height':o[5], 'ctime':o[6], 'mtime':o[7],
'modified':o[8], 'strokes':[]}
nstrokes = o[9]
start = 44
for n in range(nstrokes):
o = struct.unpack('<BIBIQ', raw[start:start+18])
#import pudb; pudb.set_trace()
if o[0]==1:
# audio registration, not a pen stroke, skip it.
start+=108
continue
col = [(o[1]>>(8*i))&0xff for i in range(3,-1,-1)]
stroke = {'type':o[0], 'color':col, 'thickness':o[2], 'time':o[4]}
ndots = o[3]
start2 = start+18
dots = []
if ndots > 1000:
# something has gone wrong
raise Exception('Something has gone wrong parsing dots in stroke')
for m in range(ndots):
d = struct.unpack('<fffB', raw[start2:start2+13])
dots.append(d) #x,y,pressure,dt
start2 += 13
stroke['dots'] = dots
N = struct.unpack('<B', raw[start2:start2+1])[0]
extra = struct.unpack('<%dc'%(N), raw[start2+1:start2+N+1])
data['strokes'].append(stroke)
start=start2+1+N
return data
def parse_pages(path):
'''
Parse neonotes file and extract stroke data on each page
'''
parsedpages = []
if os.path.isdir(path):
pages = glob.glob(path+'/*/*/page.data')
pagepaths = []
for p in pages:
so = re.search('Data/(\d+)\.page_store', p)
n=int(so.group(1))
pagepaths.append((n,p))
pagepaths.sort(key=lambda x: x[1])
for n,p in pagepaths:
fp = open(p, 'rb')
raw = fp.read()
fp.close()
data = parse_pagedata(raw)
parsedpages.append(data)
else:
# Assumed to be a zip file
zip = ZipFile(path)
pages = [p for p in zip.namelist() if p.endswith('page.data')]
pagepaths = []
for p in zip.namelist():
if p.endswith('page.data'):
so = re.search('Data/(\d+)\.page_store', p)
n=int(so.group(1))
pagepaths.append((n,p))
pagepaths.sort(key=lambda x: x[1])
for n,p in pagepaths:
zp = zip.open(p)
raw = zp.read()
zp.close()
data = parse_pagedata(raw)
parsedpages.append(data)
return parsedpages
if __name__ == "__main__":
pages = parse_pages(sys.argv[1])
for n,data in enumerate(pages):
print('Page ', n+1)
for m,s in enumerate(data['strokes']):
print('\nStroke {}'.format(m))
if s['type']==0:
print('col:',s['color'], ' thickness:', s['thickness'])
for x,y,p,dt in s['dots']:
print('x:{x:.2f}, y:{y:.2f}, p:{p:.2f}, dt:{dt:2d}'.format(**locals()))
else:
print('audio')
def bounding_box(strokes):
'''
Return the bounding box of the strokes
(x_min, y_min, x_max, y_max)
'''
xm,ym=None,None
xM,yM=None,None
for s in strokes:
for x,y,p,dt in s['dots']:
if xm==None:
xm,ym=x,y
xM,yM=x,y
else:
xm,ym=min(xm,x),min(ym,y)
xM,yM=max(xM,x),max(yM,y)
return (xm,ym,xM,yM)
def col2hex(col):
hexcol = "#%02x%02x%02x"%(col[1],col[2],col[3])
return hexcol