forked from DarthPenguinz/50.021-SignLang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
197 lines (159 loc) · 6.59 KB
/
app.py
File metadata and controls
197 lines (159 loc) · 6.59 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from PyQt5 import QtGui
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QVBoxLayout, QPushButton, QHBoxLayout
from PyQt5.QtGui import QPixmap
import sys
import cv2
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, Qt, QThread
import numpy as np
import cv2
import mediapipe as mp
import os
from torchvision.transforms import transforms
import numpy as np
import torch
import __main__
from collections import Counter
from functions import *
from PointNet import PointNet
model_path = "./saved_models/PointNet-LR0.0001/model_10.pth"
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.5, min_tracking_confidence=0.5)
setattr(__main__, "PointNet", PointNet)
model = torch.load(model_path, map_location=torch.device("cpu"))
# For webcam input:
# For webcam input
cap = cv2.VideoCapture(0)
value = None
class VideoThread(QThread):
change_pixmap_signal = pyqtSignal(np.ndarray)
update_value_signal = pyqtSignal(str)
full_word_signal = pyqtSignal(str)
def run(self):
# capture from web cam
cap = cv2.VideoCapture(0)
while True:
ret, cv_img = cap.read()
if not ret:
continue
cv_img = cv2.cvtColor(cv2.flip(cv_img, 1), cv2.COLOR_BGR2RGB)
label, raw_points = predict(model, cv_img)
if label is not None:
self.update_value_signal.emit(label)
else:
self.update_value_signal.emit("next")
cv_img.flags.writeable = False
results = hands.process(cv_img)
# Draw the hand annotations on the image.
cv_img.flags.writeable = True
cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
cv_img, hand_landmarks, mp_hands.HAND_CONNECTIONS)
self.change_pixmap_signal.emit(cv_img)
def clear(self):
self.stored_word = ""
def del_letter(self):
self.current_letter = "del"
def space(self):
self.current_letter = "space"
class App(QWidget):
def __init__(self):
super().__init__()
self.labels_history = []
self.current_letter = ""
self.stored_word = ""
self.setWindowTitle("Qt live label demo")
self.disply_width = 1000
self.display_height = 480
# create the label that holds the image
self.image_label = QLabel(self)
self.image_label.resize(self.disply_width, self.display_height)
# create a text label
self.textLabel = QLabel('Learn Sign Language')
self.letter = QLabel("")
self.full_word = QLabel("")
self.button_clear = QPushButton('Clear')
self.button_Del = QPushButton('Del')
self.button_Space = QPushButton('Space')
self.button_clear.clicked.connect(self.clear_word)
self.button_Del.clicked.connect(self.del_letter)
self.button_Space.clicked.connect(self.space)
hboxletter = QHBoxLayout()
hboxletter.addWidget(QLabel("Letter:"))
hboxletter.addWidget(self.letter)
hboxword = QHBoxLayout()
hboxword.addWidget(QLabel("Word:"))
hboxword.addWidget(self.full_word)
hbox = QHBoxLayout()
hbox.addWidget(self.button_clear)
hbox.addWidget(self.button_Del)
hbox.addWidget(self.button_Space)
# create a vertical box layout and add the two labels
vbox = QVBoxLayout()
vbox.addWidget(self.image_label)
vbox.addWidget(self.textLabel)
vbox.addLayout(hboxletter)
vbox.addLayout(hboxword)
vbox.addLayout(hbox)
# set the vbox layout as the widgets layout
self.setLayout(vbox)
# # create the video capture thread
self.thread = VideoThread()
# # connect its signal to the update_image slot
self.thread.change_pixmap_signal.connect(self.update_image)
self.thread.update_value_signal.connect(self.update_value)
self.thread.full_word_signal.connect(self.update_word)
# start the thread
self.thread.start()
@pyqtSlot(np.ndarray)
def update_image(self, cv_img):
"""Updates the image_label with a new opencv image"""
qt_img = self.convert_cv_qt(cv_img)
self.image_label.setPixmap(qt_img)
def convert_cv_qt(self, cv_img):
"""Convert from an opencv image to QPixmap"""
rgb_image = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
bytes_per_line = ch * w
convert_to_Qt_format = QtGui.QImage(rgb_image.data, w, h, bytes_per_line, QtGui.QImage.Format_RGB888)
p = convert_to_Qt_format.scaled(self.disply_width, self.display_height, Qt.KeepAspectRatio)
return QPixmap.fromImage(p)
def update_value(self, value):
self.labels_history.append(value)
if len(self.labels_history) > 50:
self.labels_history.pop(0)
if len(self.labels_history) == 50:
most_common_label = Counter(self.labels_history).most_common(1)[0][0]
if most_common_label == "next":
if self.current_letter == "del" and len(self.stored_word) > 0:
self.stored_word = self.stored_word[:-1]
elif self.current_letter == "space" and len(self.stored_word) > 0:
self.stored_word += " "
else:
self.stored_word += self.current_letter
self.labels_history = []
self.current_letter = ""
self.letter.setText("")
self.full_word.setText(self.stored_word)
else:
self.current_letter = most_common_label
self.letter.setText(most_common_label)
def update_word(self, word):
self.full_word.setText(word)
def clear_word(self):
self.stored_word = ""
self.full_word.setText(self.stored_word)
def del_letter(self):
if len(self.stored_word) > 0:
self.stored_word = self.stored_word[:-1]
self.full_word.setText(self.stored_word)
def space(self):
self.stored_word += " "
self.full_word.setText(self.stored_word)
if __name__=="__main__":
app = QApplication(sys.argv)
a = App()
a.show()
sys.exit(app.exec_())