-
Notifications
You must be signed in to change notification settings - Fork 9
/
normaliser.py
81 lines (67 loc) · 2.23 KB
/
normaliser.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
import numpy as np
import os
from PIL import Image, ImageFilter
import sys
import cv2
import glob
def load_paths(directory):
paths = []
for files in os.listdir(directory):
if (files != ".DS_Store"):
paths.append(directory+'/'+files)
return paths
def blur(image):
kernel = np.ones((5,5),np.float32)/25
dst = cv2.fastNlMeansDenoisingColored(image,None,10,48,7,5)
dst = cv2.filter2D(dst,-1,kernel)
#blur = cv2.bilateralFilter(image,9,75,75)
#blur = cv2.blur(image,(5,5))
#blur = cv2.GaussianBlur(image,(5,5),0)
return dst
def resize_and_save(path,size,directory):
try:
img = Image.open(path)
img = img.resize(size)
img.save(path)
im = cv2.imread(path)
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
blurred = blur(im)
img = Image.fromarray(blurred)
#img = img.filter(ImageFilter.SHARPEN)
img.save(directory)
except IOError:
print "cannot create thumbnail for '%s'" % path
for i in range(1,11):
directory = 'val_set_blurred_'+str(i)
if (not os.path.exists('val_set_blurred_'+str(i))):
for sign in load_paths("Traffic_Signs_Templates/Images"):
head,tail = sign.split('.')
name = []
name = head.split('/')
os.makedirs('val_set_blurred_'+str(i)+'/'+name[-1])
paths = load_paths("val_set_"+str(i))
for path in paths:
temp = load_paths(path)
for p in temp:
elements = p.split('/')
d = 'val_set_blurred_'+str(i)+'/'+elements[-2]+"/"+elements[-1]
resize_and_save(p,(48,48), d)
"""
directory = 'SGTSD/Images_blurred/'
if (not os.path.exists('SGTSD/Images_blurred/')):
for sign in load_paths("Traffic_Signs_Templates/Images"):
head,tail = sign.split('.')
name = []
name = head.split('/')
os.makedirs('SGTSD/Images_blurred/'+name[-1])
paths = load_paths("SGTSD/Images/")
classi = 0
for path in paths:
print ("Processing class: "+str(float(classi)/len(paths)))
classi = classi + 1
temp = load_paths(path)
for p in temp:
elements = p.split('/')
d = 'SGTSD/Images_blurred/'+elements[-2]+"/"+elements[-1]
resize_and_save(p,(48,48), d)
"""