-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataset_creator.py
More file actions
150 lines (135 loc) · 3.68 KB
/
dataset_creator.py
File metadata and controls
150 lines (135 loc) · 3.68 KB
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
import os
import cv2
import numpy as np
import feature_extractor as FE
from commonfunctions import *
import csv
def ShowImageCV2(image):
for img in image:
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def createArabicDictionary():
D = {}
D['ا'] = 0
D['ب'] = 1
D['ت'] = 2
D['ث'] = 3
D['ج'] = 4
D['ح'] = 5
D['خ'] = 6
D['د'] = 7
D['ذ'] = 8
D['ر'] = 9
D['ز'] = 10
D['س'] = 11
D['ش'] = 12
D['ص'] = 13
D['ض'] = 14
D['ط'] = 15
D['ظ'] = 16
D['ع'] = 17
D['غ'] = 18
D['ف'] = 19
D['ق'] = 20
D['ك'] = 21
D['ل'] = 22
D['م'] = 23
D['ن'] = 24
D['ه'] = 25
D['و'] = 26
D['ي'] = 27
D['لا'] = 28
return D
def returnToArabicDictionary():
D = {}
D[0] = 'ا'
D[1] = 'ب'
D[2] = 'ت'
D[3] = 'ث'
D[4] = 'ج'
D[5] = 'ح'
D[6] = 'خ'
D[7] = 'د'
D[8] = 'ذ'
D[9] = 'ر'
D[10] = 'ز'
D[11] = 'س'
D[12] = 'ش'
D[13] = 'ص'
D[14] = 'ض'
D[15] = 'ط'
D[16] = 'ظ'
D[17] = 'ع'
D[18] = 'غ'
D[19] = 'ف'
D[20] = 'ق'
D[21] = 'ك'
D[22] = 'ل'
D[23] = 'م'
D[24] = 'ن'
D[25] = 'ه'
D[26] = 'و'
D[27] = 'ي'
D[28] = 'لا'
return D
def saveLettersToImages(letter,label):
hw = FE.height_over_width(letter)
letter = cv2.resize(letter, (28,28), interpolation = cv2.INTER_AREA)
VP_ink,HP_ink = FE.Black_ink_histogram(letter)
Com1,Com2 = FE.Center_of_mass(letter)
CC = FE.Connected_Component(letter)
CH = FE.count_holes(letter,CC)
r1,r2,r3,r4,r5,r6,r7,r8,r9,r10 = FE.ratiosBlackWhite(letter)
HorizontalTransitions,VerticalTransitions = FE.number_of_transitions(letter)
#concat = [*VP_ink, *HP_ink] #28+28 = 56
concat=[]
concat.append(Com1) #1
concat.append(Com2) #1
concat.append(CC) #1
concat.append(r1) #1
concat.append(r2) #1
concat.append(r3) #1
concat.append(r4) #1
concat.append(r5) #1
concat.append(r6) #1
concat.append(r7) #1
concat.append(r8) #1
concat.append(r9) #1
concat.append(r10) #1
concat.append(HorizontalTransitions) #1
concat.append(VerticalTransitions) #1
concat.append(hw) #1
concat.append(CH) #1
concat.append(label)
with open("image_label_pair.csv", 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow(concat)
def checkNumberOfSeparations(wordSeparationList,lettersOfWordList): #Expecting an ndarray and a list
'''
wordSeaparationList = single word segmentation
lettersOfWordList = letters of word from text file
'''
checkBool = False
numberofSegments = len(wordSeparationList)
lettersOfWordList = np.array(lettersOfWordList)
actualNumber = len(lettersOfWordList)
if numberofSegments == actualNumber: # No action needed as number of segments same as number of letters
checkBool = True
return wordSeparationList,checkBool
if numberofSegments < actualNumber: # This means that the BLI value caused an error and the word was under segmented
return wordSeparationList,checkBool
if numberofSegments > actualNumber: #Oversegmented word but may be handled
return wordSeparationList,checkBool
def createDataSet(images,labels):
D = createArabicDictionary()
i = 0
for word in images:
j = 0
segmented_list, no_segmentation_error = checkNumberOfSeparations(word, labels[i])
if no_segmentation_error:
for l in labels[i]:
label = D[l]
saveLettersToImages(word[j], label)
j+=1
i += 1