Skip to content

Commit ec48b0a

Browse files
authored
Merge pull request #21 from Capstone-Projects-2022-Spring/ConnectOnline-ChatMessage
Connect online chat message
2 parents 992d1ec + 987dbb0 commit ec48b0a

23 files changed

Lines changed: 779 additions & 226 deletions

__pycache__/camera.cpython-39.pyc

188 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
520 Bytes
Binary file not shown.

camera.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import tensorflow as tf
99
from object_detection.utils import visualization_utils as viz_utils
1010
from alphabet import letterModel
11+
import nltk
1112

1213
def base64ToOpenCV(base64_img):
1314
im_bytes = base64.b64decode(base64_img)
@@ -24,16 +25,17 @@ def openCVTobase64(opencv_img):
2425
return im_b64
2526

2627
class Camera(object):
27-
def __init__(self):
28+
def __init__(self, w: wordsModel, l: letterModel):
2829
self.to_process = []
2930
self.to_output = []
3031
self.wordList = []
3132
self.modelType = 0
33+
self.lastTen = []
3234

3335
# self.cam = cv2.VideoCapture(0)
3436

35-
self.words = wordsModel()
36-
self.letters = letterModel()
37+
self.words = w
38+
self.letters = l
3739

3840
thread = threading.Thread(target=self.keep_processing, args=())
3941
thread.daemon = True
@@ -44,6 +46,7 @@ def restartModel(self):
4446
self.to_process.clear()
4547

4648
def updateModel(self, num):
49+
self.lastTen.clear()
4750
if num < 1:
4851
self.modelType = 0
4952
else:
@@ -190,30 +193,37 @@ def processLettersModel(self, input_img):
190193

191194
newLetter = self.letters.getLetter(lmList, handsType)
192195

193-
if newLetter == '':
194-
pass
195-
elif len(self.wordList) == 0:
196-
# there is nothing in the word list currently - this is the first entry
197-
if (newLetter != ' ') and (newLetter != '*'):
198-
self.wordList.append(newLetter)
199-
else:
200-
oldWord = next(reversed(self.wordList))
201-
if len(oldWord) > 0:
202-
oldLetter = oldWord[-1]
196+
if len(self.lastTen) < 10:
197+
self.lastTen.append(newLetter)
198+
else: # this is the case that it is at 10 frames
199+
frequency_distribution = nltk.FreqDist(self.lastTen)
200+
newLetter = frequency_distribution.max()
201+
self.lastTen.clear()
202+
203+
if newLetter == '':
204+
pass
205+
elif len(self.wordList) == 0:
206+
# there is nothing in the word list currently - this is the first entry
207+
if (newLetter != ' ') and (newLetter != '*'):
208+
self.wordList.append(newLetter)
203209
else:
204-
oldLetter = ''
205-
206-
if oldLetter != newLetter:
207-
string = oldWord+newLetter
208-
if newLetter == '*':
209-
string = string[:-2]
210-
self.wordList[len(self.wordList)-1] = string
211-
212-
if newLetter == ' ':
213-
newLetter = 'SPACE'
214-
elif newLetter == '*':
215-
newLetter = 'DELETE'
216-
217-
cv2.putText(frame, newLetter, (0, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
210+
oldWord = next(reversed(self.wordList))
211+
if len(oldWord) > 0:
212+
oldLetter = oldWord[-1]
213+
else:
214+
oldLetter = ''
215+
216+
if oldLetter != newLetter:
217+
string = oldWord+newLetter
218+
if newLetter == '*':
219+
string = string[:-2]
220+
self.wordList[len(self.wordList)-1] = string
221+
222+
if newLetter == ' ':
223+
newLetter = 'SPACE'
224+
elif newLetter == '*':
225+
newLetter = 'DELETE'
226+
227+
cv2.putText(frame, newLetter, (0, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
218228

219229
return frame

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
#app.run(debug=True)
1212

1313
# Run the Flask Application using SocketIO
14-
socketio.run(app, debug=True)
14+
socketio.run(app, debug=True, port=5000)
1515

text_to_asl.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import json, os, requests, string
2+
from nltk.corpus import wordnet as wn
3+
from nltk.stem.wordnet import WordNetLemmatizer
24
from nltk.tokenize import word_tokenize
5+
from nltk import pos_tag
6+
from collections import defaultdict
37
from bs4 import BeautifulSoup
48

9+
tag_map = defaultdict(lambda: wn.NOUN)
10+
tag_map['J'] = wn.ADJ
11+
tag_map['V'] = wn.VERB
12+
tag_map['R'] = wn.ADV
13+
514
def getVideoPath(s):
615

716
# split input string into tokens
@@ -20,8 +29,15 @@ def getTokensFromString(s):
2029
s = s.translate(translator)
2130
tokenized = word_tokenize(s)
2231

32+
lemmatized = []
33+
34+
lemma_function = WordNetLemmatizer()
35+
for token, tag in pos_tag(tokenized):
36+
lemma = lemma_function.lemmatize(token, tag_map[tag[0]])
37+
lemmatized.append(lemma)
38+
2339
# should remove articles / useless words from list first
24-
return tokenized
40+
return lemmatized
2541

2642
def getVideosFromTokens(tokens):
2743
URL = "https://www.signasl.org/sign/"
144 Bytes
Binary file not shown.
1.57 KB
Binary file not shown.

website/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def check_password(self, password):
8181
# Check Hashed Password
8282
return check_password_hash(self.user_password, password)
8383

84+
def update_username(self, username):
85+
self.username = username
86+
8487
def __repr__(self):
8588

8689
return 'User {}'.format(self.username)

website/static/fromASL_chat.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var myVideo, myCanvas;
2+
var namespace = "/chat";
3+
var localMediaStream = null;
4+
var mySID;
5+
$(document).ready(function(){
6+
7+
var socket = io(location.protocol + '//' + document.domain + ':' + location.port + namespace, {autoConnect: false});
8+
9+
myVideo = document.getElementById("videoStream");
10+
myCanvas = document.getElementById("canvasElement");
11+
var chatLog = document.getElementById('scrollable');
12+
13+
function sendSnapshot() {
14+
if (!localMediaStream) {
15+
return;
16+
}
17+
18+
myCanvas.getContext('2d').drawImage(myVideo, 0, 0, myVideo.videoWidth, myVideo.videoHeight, 0, 0, 300, 150);
19+
20+
let dataURL = myCanvas.toDataURL('image/jpeg');
21+
socket.emit('input image', dataURL);
22+
}
23+
24+
socket.on('connect', function() {
25+
socket.emit('join-room', {});
26+
console.log('Client connected!');
27+
});
28+
29+
socket.on('get-sid', function(data) {
30+
mySID = data.sid;
31+
console.log(mySID);
32+
});
33+
34+
socket.on('status', function(data) {
35+
var newText = document.createTextNode(data.msg);
36+
chatLog.appendChild(newText);
37+
38+
$('#scrollable').animate({ scrollTop: $('#scrollable').prop('scrollHeight')}, 1000);
39+
});
40+
41+
socket.on('output-message', function(data) {
42+
var newText = document.createTextNode(data.msg);
43+
chatLog.appendChild(newText);
44+
45+
$('#scrollable').animate({ scrollTop: $('#scrollable').prop('scrollHeight')}, 1000);
46+
47+
let sid = data.sid;
48+
if (sid !== mySID){
49+
getVideoPlaylist(data.msg);
50+
}
51+
});
52+
53+
var constraints = {
54+
video: {
55+
width: { min: 640 },
56+
height: { min: 480 }
57+
}
58+
};
59+
60+
navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
61+
myVideo.srcObject = stream;
62+
localMediaStream = stream;
63+
socket.connect()
64+
65+
setInterval(function () {
66+
sendSnapshot();
67+
}, 100);
68+
}).catch(function(error) {
69+
console.log(error);
70+
});
71+
72+
$('#send').click(function (){
73+
message = document.getElementById("words").innerText;
74+
clearList();
75+
socket.emit('input-message', {'msg': message});
76+
});
77+
78+
$('#audio').click(function (){
79+
getAudio();
80+
});
81+
82+
$('#clear').click(function (){
83+
clearList();
84+
});
85+
86+
$('#restart').click(function (){
87+
restart();
88+
});
89+
90+
$('#leave').click(function (){
91+
socket.emit('leave-room', {});
92+
setTimeout(() => { location.href = '/'; }, 1);
93+
94+
});
95+
96+
var myTimer = setInterval(updateText, 2000);
97+
98+
99+
100+
});
101+
102+

0 commit comments

Comments
 (0)