-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
146 lines (72 loc) · 1.75 KB
/
train_model.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import cv2
import numpy as np
import os
import pickle
from sklearn.linear_model import LogisticRegression
# In[2]:
images=[]
labels=[]
n=0
count=0
for i in os.listdir(f'E:/dataset/New folder/English/Fnt'):
for j in os.listdir(f'E:/dataset/New folder/English/Fnt/{i}'):
img=cv2.imread(f'E:/dataset/New folder/English/Fnt/{i}/{j}',cv2.IMREAD_GRAYSCALE)
img=cv2.resize(img,(50,50))
images.append(img)
labels.append(count)
n+=1
if n==700:
break
print(n,end='')
n=0
count+=1
print(count,end=" ")
# In[3]:
labels[1016-1]
#count
# In[4]:
#cv2.imshow('',images[2032])
#cv2.waitKey(0)
#cv2.destroyAllWindows()
# In[5]:
word={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,10:'A',11:'B',
12:'C',13:'D',14:'E',15:'F',16:'G',17:'H',18:'I',19:'J',
20:'K',21:'L',22:'M',23:'N',24:'O',25:'P',26:'Q',27:'R',
28:'S',29:'T',30:'U',31:'V',32:'W',33:'X',34:'Y',35:'Z',
36:'a',37:'b',38:'c',39:'d',40:'e',41:'f',42:'g',43:'h',
44:'i',45:'j',46:'k',47:'l',48:'m',49:'n',50:'o',51:'p',
52:'q',53:'r',54:'s',55:'t',56:'u',57:'v',58:'w',59:'x',
60:'y',61:'z'}
# In[6]:
X=np.array(images)
y=np.array(labels)
# In[7]:
new_x=X.reshape(len(X),-1)
# In[8]:
X.shape
# In[9]:
new_x.shape
# In[10]:
lr=LogisticRegression()
# In[ ]:
# In[11]:
lr.fit(new_x,y)
# In[13]:
test_img=cv2.imread('d:/img2.png')
test_gray=cv2.cvtColor(test_img,cv2.COLOR_BGR2GRAY)
test_gray=cv2.resize(test_gray,(50,50))
test_X=test_gray.reshape(1,-1)
lr.predict(test_X)
# In[14]:
file=open('lr_model.pickle','wb')
# In[15]:
pickle.dump(lr,file)
file.close()
# In[24]:
labels.index(37)
# In[23]:
labels[22400]
# In[ ]: