-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreatetd_cascade.py
90 lines (77 loc) · 2.07 KB
/
createtd_cascade.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
import cv2
import numpy as np
import glob
import os
patch_dimension = 128
MAXSIZE = 300000
totalarray = np.zeros([MAXSIZE, patch_dimension, patch_dimension])
patch_size = patch_dimension*patch_dimension
totalpatches = 0
#imagedir = "/home/manoj/OCR_train"
imagedir = "Res-1"
dirsave = "Data/"
totalcnt = 0
harray=[]
warray=[]
countperimage = []
names = []
lim=0
def getPatches(image, height, width):
i=0
cnt=0
global lim
global totalarray
while (i<height):
j=0
while (j<width):
if i+patch_dimension <= height-1 and j+patch_dimension <= width-1:
rs=i
re = i+patch_dimension
cs = j
ce = j+patch_dimension
if i+patch_dimension >= height and j+patch_dimension <=width-1:
rs = height-(patch_dimension)
re = height
cs = j
ce = j+patch_dimension
if i+patch_dimension <= height-1 and j+patch_dimension >=width:
rs = i
re = i+patch_dimension
cs = width - (patch_dimension)
ce = width
if i+patch_dimension >= height and j+patch_dimension >=width:
rs = height-(patch_dimension)
re = height
cs = width - (patch_dimension)
ce = width
cropimage = image[rs:re, cs:ce]
cnt = cnt+1
temparray = cropimage
totalarray[lim] = temparray
lim = lim+1
j=j+patch_dimension
i=i+patch_dimension
return cnt
for img_from_folder in sorted(glob.glob(imagedir+"/*.png")):
img = cv2.imread(img_from_folder, cv2.IMREAD_UNCHANGED)
fname=img_from_folder
fname = os.path.basename(fname)
print fname + str(totalcnt)
#img = cv2.resize(img, (96,48))
height = img.shape[0]
harray = np.append(harray, height)
width = img.shape[1]
warray = np.append(warray, width)
names = np.append(names, fname)
#print img.shape
count = getPatches(img, height,width)
countperimage = np.append(countperimage, count)
totalcnt += count
totalarray=totalarray[0:lim]
print totalarray.shape
np.save(dirsave+'testSR_2X.npy', totalarray)
np.save(dirsave+'names_2X.npy', names)
np.save(dirsave+'width_testimages_2X.npy', warray)
np.save(dirsave+"height_testimages_2X.npy", harray)
np.save(dirsave+'countperimage_2X.npy', countperimage)
print totalcnt