-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathocr_hog+lreg.py
137 lines (105 loc) · 4.13 KB
/
ocr_hog+lreg.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
# -*- coding: utf-8 -*-
"""OCR HOG+LREG.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1YVf4pmUbLUT4kOyII2cjc0L6GHIWR4Jx
"""
import numpy as np
import os
from skimage.feature import hog
import joblib
import cv2
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import pandas as pd
# train_dir = r'/content/drive/MyDrive/YourFolderName/'
train_dir = r'ocr_data/train/'
labels_list = [i for i in os.listdir(train_dir)]
pathimg = [os.listdir(train_dir + i) for i in labels_list]
# Visualize HOG for letter A
im_test = cv2.imread('ocr_data/train/A/roi107644.jpg',0)
_,hog_img= hog(im_test,orientations=9,pixels_per_cell=(8,8), cells_per_block=(1, 1),visualize=True)
plt.imshow(hog_img,cmap='gray')
# cv2.imwrite('AHog.jpg',hog_img)
# extract the hog for each image and store it in a list with its
# corresponding label
features = []
labels = []
for i,j in enumerate(zip(pathimg,labels_list)):
imgs,label = j
for img in imgs:
img = cv2.imread(train_dir+label+'/'+img)
img_res=cv2.resize(img,(64,128),interpolation=cv2.INTER_AREA)
img_gray= cv2.cvtColor(img_res,cv2.COLOR_BGR2GRAY)
hog_img= hog(img_gray,orientations=9,pixels_per_cell=(8,8), cells_per_block=(1, 1))
features.append(hog_img)
labels.append(label)
print(len(pd.DataFrame(np.array(features))))
print(len(pd.DataFrame(np.array(labels))))
df = pd.DataFrame(np.array(features))
df['target'] = labels
df
df['target'].unique()
# df.target.value_counts()
sns.countplot(x='target', data=df)
"""#Training"""
x = np.array(df.iloc[:,:-1])
y = np.array(df['target'])
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=0.20,
random_state=42)
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=0)
sm_x,sm_y=sm.fit_sample(x_train, y_train)
bal_df = pd.DataFrame(sm_x)
bal_df['target']=pd.DataFrame(sm_y)
sns.countplot(x='target', data=bal_df)
bal_df['target'].value_counts()
lreg = LogisticRegression()
clf=lreg.fit(sm_x, sm_y)
y_pred = clf.predict(x_test)
print('Accuracy {:.2f}'.format(clf.score(x_test, y_test)))
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
# save trained model
joblib.dump(clf, r'ocr_data/models/hog_lreg_model_3.pkl')
from google.colab.patches import cv2_imshow
"""#Testing"""
# Load the classifier
clf = joblib.load("ocr_data/models/hog_lreg_model_3.pkl")
# Read the input image
im = cv2.imread("ocr_data/licenseplates/licplate4.jpg")
# Convert to grayscale
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# Threshold the image in order to find contours
ret, im_th = cv2.threshold(im_gray, 120, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Find contours in the image
ctrs,hier = cv2.findContours(im_th, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
bboxes=[cv2.boundingRect(c) for c in ctrs]
sorted_bboxes = sorted(bboxes,key=lambda b:b[0])
# For each rectangular region, calculate HOG features and predict
# the digit using our logistic regression model.
plate_char=[]
for num,i_bboxes in enumerate(sorted_bboxes):
[x,y,w,h]=i_bboxes
if h>100 and w < 100:
# Make the rectangular region around the digit
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),1)
roi=im_gray[y:y+h,x:x+w]
# Resize the image
roi = cv2.resize(roi, (64, 128), interpolation=cv2.INTER_AREA)
# Calculate the HOG features
# use the same parameters used for training
roi_hog_fd = hog(roi, orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(1, 1))
nbr = clf.predict(np.array([roi_hog_fd]))
cv2.putText(im, str((nbr[0])), (x,y+h),cv2.FONT_HERSHEY_SIMPLEX,
2, (0, 200, 250), 3)
plate_char.append(str(nbr[0]))
print(''.join(plate_char))
cv2.imshow('result',im)