-
Notifications
You must be signed in to change notification settings - Fork 20
/
uwimg.py
259 lines (194 loc) · 6.93 KB
/
uwimg.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import sys, os
from ctypes import *
import math
import random
lib = CDLL(os.path.join(os.path.dirname(__file__), "libuwimg.so"), RTLD_GLOBAL)
def c_array(ctype, values):
arr = (ctype*len(values))()
arr[:] = values
return arr
class IMAGE(Structure):
_fields_ = [("w", c_int),
("h", c_int),
("c", c_int),
("data", POINTER(c_float))]
def __add__(self, other):
return add_image(self, other)
def __sub__(self, other):
return sub_image(self, other)
class POINT(Structure):
_fields_ = [("x", c_float),
("y", c_float)]
class DESCRIPTOR(Structure):
_fields_ = [("p", POINT),
("n", c_int),
("data", POINTER(c_float))]
class MATRIX(Structure):
_fields_ = [("rows", c_int),
("cols", c_int),
("data", POINTER(POINTER(c_double))),
("shallow", c_int)]
class DATA(Structure):
_fields_ = [("X", MATRIX),
("y", MATRIX)]
class LAYER(Structure):
_fields_ = [("in", MATRIX),
("dw", MATRIX),
("w", MATRIX),
("v", MATRIX),
("out", MATRIX),
("activation", c_int)]
class MODEL(Structure):
_fields_ = [("layers", POINTER(LAYER)),
("n", c_int)]
(LINEAR, LOGISTIC, RELU, LRELU, SOFTMAX) = range(5)
add_image = lib.add_image
add_image.argtypes = [IMAGE, IMAGE]
add_image.restype = IMAGE
sub_image = lib.sub_image
sub_image.argtypes = [IMAGE, IMAGE]
sub_image.restype = IMAGE
make_image = lib.make_image
make_image.argtypes = [c_int, c_int, c_int]
make_image.restype = IMAGE
free_image = lib.free_image
free_image.argtypes = [IMAGE]
get_pixel = lib.get_pixel
get_pixel.argtypes = [IMAGE, c_int, c_int, c_int]
get_pixel.restype = c_float
set_pixel = lib.set_pixel
set_pixel.argtypes = [IMAGE, c_int, c_int, c_int, c_float]
set_pixel.restype = None
rgb_to_grayscale = lib.rgb_to_grayscale
rgb_to_grayscale.argtypes = [IMAGE]
rgb_to_grayscale.restype = IMAGE
copy_image = lib.copy_image
copy_image.argtypes = [IMAGE]
copy_image.restype = IMAGE
rgb_to_hsv = lib.rgb_to_hsv
rgb_to_hsv.argtypes = [IMAGE]
rgb_to_hsv.restype = None
feature_normalize = lib.feature_normalize
feature_normalize.argtypes = [IMAGE]
feature_normalize.restype = None
clamp_image = lib.clamp_image
clamp_image.argtypes = [IMAGE]
clamp_image.restype = None
hsv_to_rgb = lib.hsv_to_rgb
hsv_to_rgb.argtypes = [IMAGE]
hsv_to_rgb.restype = None
shift_image = lib.shift_image
shift_image.argtypes = [IMAGE, c_int, c_float]
shift_image.restype = None
load_image_lib = lib.load_image
load_image_lib.argtypes = [c_char_p]
load_image_lib.restype = IMAGE
def load_image(f):
return load_image_lib(f.encode('ascii'))
save_png_lib = lib.save_png
save_png_lib.argtypes = [IMAGE, c_char_p]
save_png_lib.restype = None
def save_png(im, f):
return save_png_lib(im, f.encode('ascii'))
save_image_lib = lib.save_image
save_image_lib.argtypes = [IMAGE, c_char_p]
save_image_lib.restype = None
def save_image(im, f):
return save_image_lib(im, f.encode('ascii'))
same_image = lib.same_image
same_image.argtypes = [IMAGE, IMAGE]
same_image.restype = c_int
nn_resize = lib.nn_resize
nn_resize.argtypes = [IMAGE, c_int, c_int]
nn_resize.restype = IMAGE
bilinear_resize = lib.bilinear_resize
bilinear_resize.argtypes = [IMAGE, c_int, c_int]
bilinear_resize.restype = IMAGE
make_sharpen_filter = lib.make_sharpen_filter
make_sharpen_filter.argtypes = []
make_sharpen_filter.restype = IMAGE
make_box_filter = lib.make_box_filter
make_box_filter.argtypes = [c_int]
make_box_filter.restype = IMAGE
make_emboss_filter = lib.make_emboss_filter
make_emboss_filter.argtypes = []
make_emboss_filter.restype = IMAGE
make_highpass_filter = lib.make_highpass_filter
make_highpass_filter.argtypes = []
make_highpass_filter.restype = IMAGE
make_gy_filter = lib.make_gy_filter
make_gy_filter.argtypes = []
make_gy_filter.restype = IMAGE
make_gx_filter = lib.make_gx_filter
make_gx_filter.argtypes = []
make_gx_filter.restype = IMAGE
sobel_image = lib.sobel_image
sobel_image.argtypes = [IMAGE]
sobel_image.restype = POINTER(IMAGE)
colorize_sobel = lib.colorize_sobel
colorize_sobel.argtypes = [IMAGE]
colorize_sobel.restype = IMAGE
make_gaussian_filter = lib.make_gaussian_filter
make_gaussian_filter.argtypes = [c_float]
make_gaussian_filter.restype = IMAGE
convolve_image = lib.convolve_image
convolve_image.argtypes = [IMAGE, IMAGE, c_int]
convolve_image.restype = IMAGE
harris_corner_detector = lib.harris_corner_detector
harris_corner_detector.argtypes = [IMAGE, c_float, c_float, c_int, POINTER(c_int)]
harris_corner_detector.restype = POINTER(DESCRIPTOR)
mark_corners = lib.mark_corners
mark_corners.argtypes = [IMAGE, POINTER(DESCRIPTOR), c_int]
mark_corners.restype = None
detect_and_draw_corners = lib.detect_and_draw_corners
detect_and_draw_corners.argtypes = [IMAGE, c_float, c_float, c_int]
detect_and_draw_corners.restype = None
cylindrical_project = lib.cylindrical_project
cylindrical_project.argtypes = [IMAGE, c_float]
cylindrical_project.restype = IMAGE
structure_matrix = lib.structure_matrix
structure_matrix.argtypes = [IMAGE, c_float]
structure_matrix.restype = IMAGE
find_and_draw_matches = lib.find_and_draw_matches
find_and_draw_matches.argtypes = [IMAGE, IMAGE, c_float, c_float, c_int]
find_and_draw_matches.restype = IMAGE
panorama_image_lib = lib.panorama_image
panorama_image_lib.argtypes = [IMAGE, IMAGE, c_float, c_float, c_int, c_float, c_int, c_int]
panorama_image_lib.restype = IMAGE
draw_flow = lib.draw_flow
draw_flow.argtypes = [IMAGE, IMAGE, c_float]
draw_flow.restype = None
box_filter_image = lib.box_filter_image
box_filter_image.argtypes = [IMAGE, c_int]
box_filter_image.restype = IMAGE
optical_flow_images = lib.optical_flow_images
optical_flow_images.argtypes = [IMAGE, IMAGE, c_int, c_int]
optical_flow_images.restype = IMAGE
optical_flow_webcam = lib.optical_flow_webcam
optical_flow_webcam.argtypes = [c_int, c_int, c_int]
optical_flow_webcam.restype = None
def panorama_image(a, b, sigma=2, thresh=5, nms=3, inlier_thresh=2, iters=10000, cutoff=30):
return panorama_image_lib(a, b, sigma, thresh, nms, inlier_thresh, iters, cutoff)
train_model = lib.train_model
train_model.argtypes = [MODEL, DATA, c_int, c_int, c_double, c_double, c_double]
train_model.restype = None
accuracy_model = lib.accuracy_model
accuracy_model.argtypes = [MODEL, DATA]
accuracy_model.restype = c_double
forward_model = lib.forward_model
forward_model.argtypes = [MODEL, MATRIX]
forward_model.restype = MATRIX
load_classification_data = lib.load_classification_data
load_classification_data.argtypes = [c_char_p, c_char_p, c_int]
load_classification_data.restype = DATA
make_layer = lib.make_layer
make_layer.argtypes = [c_int, c_int, c_int]
make_layer.restype = LAYER
def make_model(layers):
m = MODEL()
m.n = len(layers)
m.layers = (LAYER*m.n) (*layers)
return m
if __name__ == "__main__":
im = load_image("data/dog.jpg")
save_image(im, "hey")