-
Notifications
You must be signed in to change notification settings - Fork 0
/
realTimeDigitClassifier.py
62 lines (50 loc) · 1.52 KB
/
realTimeDigitClassifier.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
import cv2
from keras.models import load_model
import numpy as np
########### PARAMETERS ##############
width = 640
height = 480
threshold = 0.65 # MINIMUM PROBABILITY TO CLASSIFY
cameraNo = 0
brightness = 100
#####################################
# CREATE CAMERA OBJECT
cap = cv2.VideoCapture(cameraNo)
cap.set(3, width)
cap.set(4, height)
cap.set(10, brightness)
# LOAD THE TRAINNED MODEL
# To load the model tensorflow_2.1.0 and keras_2.3.1 need to be installed
model = load_model('my_model.h5')
# PREPORCESSING FUNCTION
def preProcessing(img):
'''
Performs preprocessing on the raw image
'''
# converts BGR to GRAYSCALE image
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# increases the contrast by equalizing the histogram
img = cv2.equalizeHist(img)
# normalize
img = img/255
img = cv2.resize(img, (28, 28))
return img
# Loop to display continuous frames
while True:
_, imgOriginal = cap.read()
img = np.asarray(imgOriginal)
img = preProcessing(img)
img = img.reshape(1, 28, 28, 1)
classIndex = int(model.predict_classes(img))
# Probability of predicted class
probVal = np.amax(model.predict(img))
print(classIndex, probVal)
if probVal > threshold:
cv2.putText(imgOriginal, str(classIndex) + " "+str(probVal),
(50, 50), cv2.FONT_HERSHEY_COMPLEX,
1, (0, 0, 255), 1)
cv2.imshow("Original Image", imgOriginal)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()