-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdigit_recogniser.py
71 lines (58 loc) · 2.43 KB
/
digit_recogniser.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
import numpy as np
import cv2
import mnist_test
import keras.backend.tensorflow_backend as tfback
import tensorflow as tf
#code is taken from here : https://github.com/keras-team/keras/issues/13684#issuecomment-595054461
def _get_available_gpus():
"""Get a list of available gpu devices (formatted as strings).
# Returns
A list of available GPU devices.
"""
#global _LOCAL_DEVICES
if tfback._LOCAL_DEVICES is None:
devices = tf.config.list_logical_devices()
tfback._LOCAL_DEVICES = [x.name for x in devices]
return [x for x in tfback._LOCAL_DEVICES if 'device:gpu' in x.lower()]
#experimental_list_devices is deprecated in tf 2.1
def get_img_contour_thresh(img):
x, y, w, h = 0, 0, 300, 300
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (35, 35), 0)
ret, thresh1 = cv2.threshold(blur, 70, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
thresh1 = thresh1[y:y + h, x:x + w]
contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
return img, contours, thresh1
def main():
loaded_model = mnist_test.model()
cap = cv2.VideoCapture(0)
while (cap.isOpened()):
ret, img = cap.read()
img, contours, thresh = get_img_contour_thresh(img)
ans1 = ''
if len(contours) > 0:
contour = max(contours, key=cv2.contourArea)
if cv2.contourArea(contour) > 2500:
x, y, w, h = cv2.boundingRect(contour)
newImage = thresh[y:y + h, x:x + w]
newImage = cv2.resize(newImage, (28, 28))
newImage = np.array(newImage)
newImage = newImage.flatten()
newImage = newImage.reshape(1, 1,28,28)
ans1 = loaded_model.predict(newImage)
ans1=ans1.tolist()
ans1 = ans1[0].index(max(ans1[0]))
x, y, w, h = 0, 0, 300, 300
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(img, " Deep Network : " + str(ans1), (10, 330),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
#change the window size to fit screen properly
#img = cv2.resize(img, (1000, 600))
cv2.imshow("Frame", img)
cv2.imshow("Contours", thresh)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# tfback._get_available_gpus = _get_available_gpus
main()