-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_on_image.py
73 lines (55 loc) · 1.95 KB
/
test_on_image.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
'''
Author:Kaustubh Devkar
Code is updated for running on google colab
'''
#importing required modules
import numpy as np,cv2,imutils
from sklearn.externals import joblib
from google.colab.patches import cv2_imshow
#reading image
img = cv2.imread('Handwritten-Digit-Recognition-Using-Deep-Learning/sample_image2.jpg')
#resizing image
img = imutils.resize(img,width=300)
#showing original image
cv2_imshow(img)
#converting image to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#showing grayscale image
cv2_imshow(gray)
#creating a kernel
kernel = np.ones((40,40),np.uint8)
#applying blackhat thresholding
blackhat = cv2.morphologyEx(gray,cv2.MORPH_BLACKHAT,kernel)
#applying OTSU's thresholding
ret,thresh = cv2.threshold(blackhat,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#performing erosion and dilation
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
#finding countours in image
cnts,hie = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#loading our ANN model
model = joblib.load('model.pkl')
for c in cnts:
try:
#creating a mask
mask = np.zeros(gray.shape,dtype="uint8")
(x,y,w,h) = cv2.boundingRect(c)
hull = cv2.convexHull(c)
cv2.drawContours(mask,[hull],-1,255,-1)
mask = cv2.bitwise_and(thresh,thresh,mask=mask)
#Getting Region of interest
roi = mask[y-7:y+h+7,x-7:x+w+7]
roi = cv2.resize(roi,(28,28))
roi = np.array(roi)
#reshaping roi to feed image to our model
roi = roi.reshape(1,784)
#predicting
prediction = model.predict(roi)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),1)
cv2.putText(img,str(int(prediction)),(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,0,0),1)
except Exception as e:
print(e)
img = imutils.resize(img,width=500)
#showing the output
cv2_imshow(img)
cv2.imwrite('result2.jpg',img)