-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
199 lines (165 loc) · 6.41 KB
/
utils.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
#!/usr/bin/env python3
"""
Project Utilities.
Usage in python console: "from utils import function_name".
@author: Netanel Azoulay
@author: Roman Koifman
"""
import cv2
import glob
import os
import random
from projectParams import alphaBet
def flipImages(rootDir, imgFormat=None):
"""
Flip all images in given rootDirectory. (Make new copies).
:param rootDir: the folder that contain sub-folders which with images. Ex: "Images/train".
:param imgFormat: 'jpg' or 'png'. If none provided, both will be used.
"""
if imgFormat is None:
flipImages(rootDir, 'jpg')
flipImages(rootDir, 'png')
else:
string = rootDir + "/*/*." + imgFormat
filenames = glob.glob(string)
if len(filenames) == 0:
string = rootDir + "/*." + imgFormat
filenames = glob.glob(string)
for fileName in filenames:
img = cv2.imread(fileName)
if img is not None:
flippedFilename = fileName.replace("." + imgFormat, "_flipped." + imgFormat)
cv2.imwrite(filename=flippedFilename, img=cv2.flip(img, 1))
print("Flipped " + fileName + "as " + flippedFilename)
def binaryMask(img):
"""
Apply binary mask on raw rgb image.
:param img: 3D np array.
:return: processed image. (3D np array).
"""
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.GaussianBlur(img, (7, 7), 3)
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
ret, new = cv2.threshold(img, 25, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
return new
def applyBinaryMasks(rootDir, imgFormat=None):
"""
Apply binary mask on every img in the specified directory.
:param rootDir: English path. must contain sub-folders which contains only images. Ex: "Images/train".
:param imgFormat: 'jpg' or 'png'. If none provided, both will be used.
"""
if imgFormat is None:
applyBinaryMasks(rootDir, 'jpg')
applyBinaryMasks(rootDir, 'png')
else:
string = rootDir + "/*/*." + imgFormat
filenames = glob.glob(string)
if len(filenames) == 0:
string = rootDir + "/*." + imgFormat
filenames = glob.glob(string)
for fileName in filenames:
img = cv2.imread(fileName)
if img is not None:
cv2.imwrite(filename=fileName, img=binaryMask(img))
print("Applied Binary Mask on " + fileName)
def moveRandomFiles(from_dir, to_dir, percent):
"""
Move random files between folders.
:param from_dir: English path. Source directory.
:param to_dir: English path. Destination directory.
:param percent: Percent of files to move out of the source folder.
"""
count = len(os.listdir(from_dir))
numToMove = int(percent * count)
try:
os.makedirs(to_dir)
except OSError as e:
# print(e)
pass
for i in range(numToMove): # [0,numToMove)
fileName = random.choice(os.listdir(from_dir))
os.rename(from_dir + "/" + fileName, to_dir + "/" + fileName)
print("moved file " + fileName + " from " + from_dir + " to " + to_dir)
def moveProjectData(source="captureData/train", dest="captureData/test", percent=0.2):
"""
Move random data between test and train folders. iterate subdirectories.
:param source: English path. Source directory.
:param dest: English path. Destination directory.
:param percent: Percent of files to move out of the source folder.
:raises: OSError if source folder does not exist.
"""
for subdir in os.listdir(source):
moveRandomFiles(from_dir=source + "/" + subdir, to_dir=dest + "/" + subdir, percent=percent)
def convertIndexToHebrewLetter(index):
"""
Convert index to hebrew letter.
:param index: index in the range[0,23]. Out of range index will be converted to blank char.
:return: Hebrew letter.
"""
if index == 23: # deletion
return 'del'
elif 0 <= index <= 22: # 22 = space
return alphaBet[index]
else:
return ''
def convertEnglishToHebrewLetter(englishLetter):
"""
Convert english letter to hebrew letter.
:param englishLetter: English letter.
:return: Hebrew letter.
"""
if englishLetter == ' ' or englishLetter == 'w' or englishLetter == 'W':
return ' '
elif englishLetter == 'x' or englishLetter == 'X':
return 'del'
elif 'a' <= englishLetter <= 'v':
return convertIndexToHebrewLetter(ord(englishLetter) - ord('a'))
elif 'A' <= englishLetter <= 'V':
return convertIndexToHebrewLetter(ord(englishLetter) - ord('A'))
else:
return ''
def convertHebrewLetterToFinal(hebrewLetter):
"""
Convert hebrew letter to final representation. Not will be changed if not convertable.
:param hebrewLetter: Hebrew letter.
:return: Final representation Hebrew letter.
"""
if hebrewLetter == 'כ':
return 'ך'
elif hebrewLetter == 'מ':
return 'ם'
elif hebrewLetter == 'נ':
return 'ן'
elif hebrewLetter == 'פ':
return 'ף'
elif hebrewLetter == 'צ':
return 'ץ'
else:
return hebrewLetter
def finalizeHebrewString(hebrewString):
"""
Convert hebrew string letters to finals if needed (After space).
:param hebrewString: Hebrew sentence.
:return: Valid hebrew sentence with final letters representation.
"""
if type("hebrewString") is not str or len(hebrewString) == 0:
return hebrewString
hebrewString = hebrewString.replace('כ ', 'ך ')
hebrewString = hebrewString.replace('מ ', 'ם ')
hebrewString = hebrewString.replace('נ ', 'ן ')
hebrewString = hebrewString.replace('פ ', 'ף ')
hebrewString = hebrewString.replace('צ ', 'ץ ')
hebrewString = hebrewString[:-1] + convertHebrewLetterToFinal(hebrewString[-1])
return hebrewString
def convertEnglishStringToHebrew(englishString):
"""
Convert english string (representing ids) to hebrew string, finalizing final letters after space.
:param englishString: english sentence.
:return: Valid hebrew sentence with final letters representation.
"""
for c in range(len(alphaBet)):
eng1 = chr(ord('a') + c)
eng2 = chr(ord('A') + c)
englishString = englishString.replace(eng1, alphaBet[c])
englishString = englishString.replace(eng2, alphaBet[c])
return finalizeHebrewString(englishString)