-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
132 lines (104 loc) · 4.06 KB
/
model.py
File metadata and controls
132 lines (104 loc) · 4.06 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import cv2
import numpy as np
import mediapipe as mp
import tensorflow as tf
from tensorflow.keras.models import load_model
import time
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate',125)
voices = engine.getProperty('voice')
engine.setProperty('voice', "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\TTS_MS_EN-GB_HAZEL_11.0")
class hand_detection:
def __init__(self, string):
self.string = string
def gesture_detect(self):
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1, min_detection_confidence=0.7)
mpDraw = mp.solutions.drawing_utils
prev_classname = ""
# Load the gesture recognizer model
model = load_model("Gesture Recognition\mp_hand_gesture")
# Load class names
f = open('Gesture Recognition\gesture.names', 'r')
classNames = f.read().split('\n')
f.close()
print(classNames)
cap = cv2.VideoCapture(0)
while True:
# Read each frame from the webcam
_, frame = cap.read()
x, y, c = frame.shape
# Flip the frame vertically
frame = cv2.flip(frame, 1)
framergb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Get hand landmark prediction
result = hands.process(framergb)
className = ''
# post process the result
if result.multi_hand_landmarks:
landmarks = []
for handslms in result.multi_hand_landmarks:
for lm in handslms.landmark:
# print(id, lm)
lmx = int(lm.x * x)
lmy = int(lm.y * y)
landmarks.append([lmx, lmy])
# Drawing landmarks on frames
mpDraw.draw_landmarks(frame, handslms, mpHands.HAND_CONNECTIONS)
# Predict gesture
prediction = model.predict([landmarks])
# print(prediction)
classID = np.argmax(prediction)
# prev_classname = className
className = classNames[classID]
# show the prediction on the frame
# cv2.putText(frame, className, (10, 50), cv2.FONT_HERSHEY_SIMPLEX,
# 1, (0,0,255), 2, cv2.LINE_AA)
# Show the final output
cv2.imshow("Output", frame)
print("Prev_classname is: ", prev_classname)
print("Current Classname is: ", className)
if prev_classname != className:
self.string = self.string + className + " "
prev_classname = className
# cv2.putText(frame, self.string, (10, 50), cv2.FONT_HERSHEY_SIMPLEX,
# 1, (0,0,255), 2, cv2.LINE_AA)
print("Current string is: " +self.string)
if cv2.waitKey(1) == ord('q'):
break
time.sleep(0.1)
# release the webcam and destroy all active windows
cap.release()
cv2.destroyAllWindows()
self.speak()
def speak(self):
engine.say(self.string)
engine.runAndWait()
import io
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
# print(op_str)
class string_to_symbol:
def __init__(self, string):
self.string = string
self.op_str = ""
self.stop_words = set(stopwords.words('english'))
def removing_stop_words(self):
words = self.string.split()
for word in words:
if not word in self.stop_words:
self.op_str += word + " "
self.speak_words()
def speak_words(self):
if self.op_str == "":
print("Empty string!!")
engine.say("Empty string")
else:
print(self.op_str)
engine.say(self.op_str)
engine.runAndWait()
# obj2 = string_to_symbol("I strongly suggest that one reasds this book")
# obj2.removing_stop_words()
# hand_object = hand_detection("")
# hand_object.gesture_detect()