-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcanny.py
More file actions
32 lines (23 loc) · 959 Bytes
/
canny.py
File metadata and controls
32 lines (23 loc) · 959 Bytes
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
from scipy.misc import imread, imsave
import torch
from torch.autograd import Variable
from net_canny import Net
def canny(raw_img, use_cuda=False):
img = torch.from_numpy(raw_img.transpose((2, 0, 1)))
batch = torch.stack([img]).float()
net = Net(threshold=3.0, use_cuda=use_cuda)
if use_cuda:
net.cuda()
net.eval()
data = Variable(batch)
if use_cuda:
data = Variable(batch).cuda()
blurred_img, grad_mag, grad_orientation, thin_edges, thresholded, early_threshold = net(data)
imsave('gradient_magnitude.png',grad_mag.data.cpu().numpy()[0,0])
imsave('thin_edges.png', thresholded.data.cpu().numpy()[0, 0])
imsave('final.png', (thresholded.data.cpu().numpy()[0, 0] > 0.0).astype(float))
imsave('thresholded.png', early_threshold.data.cpu().numpy()[0, 0])
if __name__ == '__main__':
img = imread('fb_profile.jpg') / 255.0
# canny(img, use_cuda=False)
canny(img, use_cuda=True)