-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLiveVideoFeed.py
98 lines (81 loc) · 2.85 KB
/
LiveVideoFeed.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
import sys
import os
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import copy
import cv2
import ImagePreprocessing
#CONSTANTS
NO_OF_CLASSES = 10
IMAGE_HEIGHT = 100
IMAGE_WIDTH = 100
BATCH_SIZE = 30
DATASET_PATH = '../Sign-Language-Digits-Dataset/CannyEdgeDataset/'
EPOCHS = 30
import keras
classifier= keras.models.load_model('working-1.model')
def predict(image_data):
x = classifier.predict(image_data)
x = x[0]
maxi , maxv = 0 , x[0]
for i in range(10):
if(maxv < x[i]):
maxv = x[i]
maxi = i
return (str(maxi) , maxv)
c = 0
cap = cv2.VideoCapture(0)
res, score = '', 0.0
i = 0
mem = ''
consecutive = 0
sequence = ''
img_number = 1
while True:
ret, img = cap.read()
img = cv2.flip(img, 1)
if ret:
x1, y1, x2, y2 = 300, 100, 400, 200
img_c = img[y1:y2, x1:x2]
#img_g = convertToGreyScale(img_c)
c += 1
# image_data = cv2.imencode('.jpg', img_cropped)[1].tostring()
a = cv2.waitKey(1) # waits to see if `esc` is pressed
if i%4 == 0:
# cv2.imwrite('saved/{i}test.jpg',img_c)
cv2.imwrite('./LiveImages/image{}.jpg'.format(img_number) , img_c)
img_c = cv2.imread('./LiveImages/image{}.jpg'.format(img_number) , 0)
cv2.imwrite('./LiveImages/image-gray{}.jpg'.format(img_number) , img_c)
img_c = cv2.imread('./LiveImages/image-gray{}.jpg'.format(img_number))
img_c = cv2.resize(img_c,(IMAGE_HEIGHT , IMAGE_WIDTH))
img_c = img_c.reshape(1 , IMAGE_HEIGHT , IMAGE_WIDTH , 3)
img_number+= 1
res_tmp, score = predict(img_c)
res = res_tmp
if mem == res:
consecutive += 1
else:
consecutive = 0
if consecutive == 2 and res not in ['nothing']:
if res == 'space':
sequence += ' '
elif res == 'del':
sequence = sequence[:-1]
else:
sequence += res
consecutive = 0
i += 1
cv2.putText(img, '%s' % (res.upper()), (100,400), cv2.FONT_HERSHEY_SIMPLEX, 4, (255,255,255), 4)
cv2.putText(img, '(score = %.5f)' % (float(score)), (100,450), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255))
mem = res
cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)
cv2.imshow("img", img)
img_sequence = np.zeros((200,1200,3), np.uint8)
cv2.putText(img_sequence, '%s' % (sequence.upper()), (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
#cv2.imshow('sequence', img_sequence)
if a == 27: # when `esc` is pressed
break
# Following line should... <-- This should work fine now
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()