-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
96 lines (67 loc) · 2.49 KB
/
main.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
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import pandas as pd
import tensorflow
from gtts import gTTS
model = tensorflow.keras.models.load_model('second_model.model')
#url = 'your IP address here'
lowerbound = np.array([0, 48, 80])
upperbound = np.array([20, 255, 255])
size = 120
kernel_open = np.ones((5, 5))
kernel_close = np.ones((20, 20))
def prepare(frame):
image = cv2.imread('C:\\Users\\Vikas Thapliyal\\Desktop\\final\\full_project\\train_image.jpg', cv2.IMREAD_GRAYSCALE)
image = image/255
image = cv2.resize(image, (size, size))
return image.reshape(-1, size, size, 1)
from urllib.request import urlopen
alpha = [chr(i) for i in range(65, 91)]
alpha.append('nothing')
alpha.append('space')
alpha.append('del')
video = cv2.VideoCapture(0)
video.set(3, 480)
video.set(4, 800)
#video = cv2.VideoCapture('test_video.mp4')
while True:
'''
frame = urlopen(url)
image_np = np.array(bytearray(frame.read()), dtype = np.uint8)
frame = cv2.imdecode(image_np, -1)
'''
ret, frame = video.read()
color_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(color_hsv, lowerbound, upperbound)
maskOpen = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel_open)
maskClose = cv2.morphologyEx(maskOpen, cv2.MORPH_CLOSE, kernel_close)
maskFinal = maskClose
conts, h = cv2.findContours(maskFinal.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
string = ''
string_buffer = []
if len(conts) == 1:
x1, y1, w1, h1 = cv2.boundingRect(conts[0])
cv2.rectangle(frame, (x1-50,y1-50), (x1+w1+50, y1+h1+50), (255, 0, 0), 3)
cv2.imwrite('train_image.jpg', frame[y1:y1+h1+50, x1:x1+w1+50])
try:
train_ = cv2.imread('train_image.jpg', cv2.IMREAD_GRAYSCALE)
cv2.imshow('train_image', train_)
except:
pass
predictions = model.predict([prepare(maskFinal)])
print(predictions)
new_pred= list(predictions[0])
index = new_pred.index(max(new_pred))
if index > .7:
print(index)
cv2.putText(frame, alpha[index], (x1,y1), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
else:
pass
cv2.imshow('frame_window', frame)
cv2.imshow('mask_close', maskClose)
key = cv2.waitKey(1)
if key == 27:
break
cv2.destroyAllWindows()