Skip to content

Commit 1ec3326

Browse files
authored
Merge pull request #14 from Capstone-Projects-2022-Spring/ASLIntegration-DynamicPageUpdates
updated model integration
2 parents 5e9ebf9 + cf0ba51 commit 1ec3326

10 files changed

Lines changed: 206 additions & 160 deletions

File tree

478 Bytes
Binary file not shown.

detection.py

Lines changed: 89 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import os
12
import tensorflow as tf
23
from object_detection.utils import config_util
3-
import os
44
from object_detection.utils import label_map_util
55
from object_detection.utils import visualization_utils as viz_utils
66
from object_detection.builders import model_builder
7-
import cv2
7+
import cv2
88
import numpy as np
9+
import keyboard
910
from playsound import playsound
1011

1112
WORKSPACE_PATH = 'RealTimeObjectDetection/Tensorflow/workspace'
@@ -17,7 +18,7 @@
1718
PRETRAINED_MODEL_PATH = WORKSPACE_PATH+'/pre-trained-models'
1819
CONFIG_PATH = MODEL_PATH+'/my_ssd_mobnet/pipeline.config'
1920
CHECKPOINT_PATH = MODEL_PATH+'/my_ssd_mobnet/'
20-
CUSTOM_MODEL_NAME = 'my_ssd_mobnet'
21+
CUSTOM_MODEL_NAME = 'my_ssd_mobnet'
2122
CONFIG_PATH = MODEL_PATH+'/'+CUSTOM_MODEL_NAME+'/pipeline.config'
2223

2324
config = config_util.get_configs_from_pipeline_file(CONFIG_PATH)
@@ -30,10 +31,43 @@
3031
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
3132
print("camera created")
3233

34+
def read_label_map(label_map_path):
35+
item_id = None
36+
item_name = None
37+
items = {}
38+
39+
with open(label_map_path, "r") as file:
40+
for line in file:
41+
line.replace(" ", "")
42+
if line == "item{":
43+
pass
44+
elif line == "}":
45+
pass
46+
elif "id" in line:
47+
item_id = int(line.split(":", 1)[1].strip())-1
48+
elif "name" in line:
49+
item_name = line.split(":", 1)[1].replace("'", "").strip()
50+
51+
if item_id is not None and item_name is not None:
52+
items[item_id] = item_name
53+
item_id = None
54+
item_name = None
55+
56+
return items
57+
3358
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
3459
ckpt.restore(os.path.join(CHECKPOINT_PATH, 'ckpt-6')).expect_partial()
3560
category_index = label_map_util.create_category_index_from_labelmap(ANNOTATION_PATH+'/label_map.pbtxt')
36-
print("checkpoints reached")
61+
label_map = read_label_map(ANNOTATION_PATH+'/label_map.pbtxt')
62+
63+
class myWords:
64+
wordList = []
65+
modelRunning = 0
66+
67+
def addWord(self, word):
68+
self.wordList.append(word)
69+
def resetList(self):
70+
self.wordList = []
3771

3872
def getCamera():
3973
return cap
@@ -45,12 +79,14 @@ def detect_fn(image):
4579
detections = detection_model.postprocess(prediction_dict, shapes)
4680
return detections
4781

48-
def activateModel():
82+
def activateModel(m: myWords):
83+
oldWord = ''
84+
4985
while True:
5086
ret, frame = cap.read()
51-
frame = cv2.flip(frame, 1)
87+
frame2 = cv2.flip(frame, 1)
5288

53-
image_np = np.array(frame)
89+
image_np = np.array(frame2)
5490
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
5591
detections = detect_fn(input_tensor)
5692
num_detections = int(detections.pop('num_detections'))
@@ -60,48 +96,59 @@ def activateModel():
6096
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
6197
label_id_offset = 1
6298
image_np_with_detections = image_np.copy()
63-
6499
res = [(i, j) for i, j in zip(detections['detection_classes'], detections['detection_scores'])]
100+
65101
for i in res:
66102
if i[1] >= .50:
67103
gesture_id = int(i[0])
68-
if gesture_id == 0:
69-
playsound("RealTimeObjectDetection/hello_en.mp3")
70-
if gesture_id == 1:
71-
playsound("RealTimeObjectDetection/help_en.mp3")
72-
if gesture_id == 2:
73-
playsound("RealTimeObjectDetection/yes_en.mp3")
74-
if gesture_id == 3:
75-
playsound("RealTimeObjectDetection/no_en.mp3")
76-
if gesture_id == 4:
77-
playsound("RealTimeObjectDetection/i_en.mp3")
78-
if gesture_id == 5:
79-
playsound("RealTimeObjectDetection/i_love_you_en.mp3")
80-
if gesture_id == 6:
81-
playsound("RealTimeObjectDetection/stand_en.mp3")
82-
if gesture_id == 7:
83-
playsound("RealTimeObjectDetection/telephone_en.mp3")
84-
if gesture_id == 8:
85-
playsound("RealTimeObjectDetection/mom_en.mp3")
86-
if gesture_id == 9:
87-
playsound("RealTimeObjectDetection/thank_you_en.mp3")
104+
105+
newWord = label_map.get(gesture_id)
106+
if oldWord != newWord:
107+
m.addWord(newWord)
108+
oldWord = newWord
109+
110+
111+
# if gesture_id == 0:
112+
# playsound("hello_en.mp3")
113+
# if gesture_id == 1:
114+
# playsound("help_en.mp3")
115+
# if gesture_id == 2:
116+
# playsound("yes_en.mp3")
117+
# if gesture_id == 3:
118+
# playsound("no_en.mp3")
119+
# if gesture_id == 4:
120+
# playsound("i_en.mp3")
121+
# if gesture_id == 5:
122+
# playsound("i_love_you_en.mp3")
123+
# if gesture_id == 6:
124+
# playsound("stand_en.mp3")
125+
# if gesture_id == 7:
126+
# playsound("telephone_en.mp3")
127+
# if gesture_id == 8:
128+
# playsound("mom_en.mp3")
129+
# if gesture_id == 9:
130+
# playsound("thank_you_en.mp3")
88131

89132
viz_utils.visualize_boxes_and_labels_on_image_array(
90-
image_np_with_detections,
91-
detections['detection_boxes'],
92-
detections['detection_classes']+label_id_offset,
93-
detections['detection_scores'],
94-
category_index,
95-
use_normalized_coordinates=True,
96-
max_boxes_to_draw=5,
97-
min_score_thresh=.5,
98-
agnostic_mode=False)
133+
image_np_with_detections,
134+
detections['detection_boxes'],
135+
detections['detection_classes'] + label_id_offset,
136+
detections['detection_scores'],
137+
category_index,
138+
use_normalized_coordinates=True,
139+
max_boxes_to_draw=5,
140+
min_score_thresh=.5,
141+
agnostic_mode=False)
99142

100143
# cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))
101144
ret, buffer = cv2.imencode('.jpg', image_np_with_detections)
102-
frame = buffer.tobytes()
103-
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
145+
frame3 = buffer.tobytes()
146+
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame3 + b'\r\n')
104147

105-
if cv2.waitKey(1) & 0xFF == ord('q'):
106-
cap.release()
107-
break
148+
try:
149+
if keyboard.is_pressed('q'):
150+
print("exiting model")
151+
cap.release()
152+
return m
153+
except:
154+
pass

website/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
db = SQLAlchemy()
66

7+
78
def create_app():
8-
99
# Create the Object of Flask
1010
app = Flask(__name__)
11-
#app.config['SECRET_KEY'] = 'as;dlfkja;sdlkjf'
1211

1312
# Load the Configuration from the config.py file
1413
app.config.from_object(Config)
@@ -17,7 +16,6 @@ def create_app():
1716
db.init_app(app)
1817

1918
with app.app_context():
20-
2119
# Import Views and Auth (Routes)
2220
from .views import views
2321
from .auth import auth
@@ -29,4 +27,4 @@ def create_app():
2927
# Create SQL Tables for our Data Models
3028
db.create_all()
3129

32-
return app
30+
return app
0 Bytes
Binary file not shown.
114 Bytes
Binary file not shown.

website/templates/base.html

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
<!-- video.js base CSS -->
1717
<link href="https://vjs.zencdn.net/7.18.1/video-js.css" rel="stylesheet" />
18-
19-
2018
<style>
2119
::-moz-selection { /* Code for Firefox */
2220
color: white;
@@ -94,11 +92,7 @@
9492

9593

9694
{% endblock %}
97-
<script
98-
src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
99-
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
100-
crossorigin="anonymous"
101-
></script>
95+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
10296

10397

10498
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>

website/templates/from_asl.html

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,97 @@
77
{% endblock %}
88

99
{% block content%}
10-
<div class="row">
11-
<div class="col align-items-start">
12-
<form method="post">
13-
<input type="submit" class="btn btn-success btn-lg" value="Stop Translation" name="stop"/>
14-
</form>
1510

16-
<div class="form-check form-switch">
17-
<input class="form-check-input" type="checkbox" role="switch" id="textSwitch" checked>
18-
<label class="form-check-label" for="textSwitch">Text output</label>
19-
</div>
20-
21-
<div class="form-check form-switch">
22-
<input class="form-check-input" type="checkbox" role="switch" id="audioSwitch">
23-
<label class="form-check-label" for="audioSwitch">Audio output</label>
24-
</div>
25-
</div>
11+
<div class="row">
12+
<div class="col align-items-start"></div>
2613
<div class="col align-items-center">
14+
{% if translating and wordsModel %}
15+
<!-- <h2>Press 'q' to end the translation</h2>-->
2716
<img src="{{ url_for('views.video_feed') }}">
17+
{% elif translating %}
18+
this is the view for the alphabet translator
19+
{% endif %}
20+
2821
</div>
22+
2923
<div class="col align-items-end">
30-
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" disabled readonly>This is where the output text generated by the ASL interpretation will be printed.
31-
</textarea>
24+
{% if translating %}
25+
<h3>Translation output</h3>
26+
<textarea class="form-control" id="words" rows="5" disabled readonly></textarea>
27+
<button type="button" id="audio" class="btn btn-primary" onclick="getAudio()">Play Audio</button>
28+
{% endif %}
3229
</div>
3330
</div>
3431

35-
32+
<div class="container">
33+
<div class="row">
34+
<div class="col text-center">
35+
<form method="POST">
36+
{% if not translating %}
37+
<input type="submit" class="btn btn-lg btn-success" name="translateButton" value="Words">
38+
<input type="submit" class="btn btn-lg btn-success" name="translateButton" value="Alphabet">
39+
{% elif translating %}
40+
<input type="submit" class="btn btn-lg btn-danger" name="translateButton" value="Restart Translation">
41+
{% endif %}
42+
</form>
43+
</div>
44+
</div>
45+
</div>
3646

3747
{% endblock %}
3848

49+
50+
{% block script %}
51+
<script>
52+
$('#launchModal').on('click', function (e) {
53+
54+
$("#stopTranslate").modal('show');
55+
56+
});
57+
58+
var myTimer = setInterval(updateText, 3000);
59+
60+
function updateText(){
61+
var updatedText = $.get("{{ url_for('views.get_words') }}");
62+
63+
updatedText.done(function(results) {
64+
var result = "";
65+
results.results.forEach(item => {
66+
item = item.replaceAll("_", " ");
67+
result = result.concat(" ", item);
68+
});
69+
$('#words').text(result);
70+
});
71+
72+
}
73+
74+
function getAudio(){
75+
var updatedText = $.get("{{ url_for('views.get_words') }}");
76+
77+
updatedText.done(function(results) {
78+
var result = "";
79+
80+
results.results.forEach(item => {
81+
item = item.replaceAll("_", " ");
82+
result = result.concat(" ", item);
83+
});
84+
85+
console.log(result)
86+
var msg = new SpeechSynthesisUtterance('Translated output:'+result);
87+
window.speechSynthesis.speak(msg);
88+
89+
})
90+
}
91+
92+
document.addEventListener('keypress', (event) => {
93+
var name = event.key;
94+
var code = event.code;
95+
// Alert the key name and key code on keydown
96+
console.log(`Key pressed ${name} \r\n Key code value: ${code}`);
97+
if (name === 'q') {
98+
clearInterval(myTimer);
99+
}
100+
}, false);
101+
102+
</script>
103+
{% endblock %}

website/templates/index.html

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,10 @@
2323

2424
{% block content %}
2525
<div class="d-grid gap-4 col-5 mx-auto d-md-block">
26-
<button class="btn btn-primary btn-lg" type="submit" onclick="location.href = '/translate/fromASL';">Translate from ASL</button>
27-
<button class="btn btn-primary btn-lg" type="submit" onclick="location.href = '/translate/toASL';">Translate to ASL</button>
28-
<button class="btn btn-primary btn-lg" type="submit" onclick="location.href = '/translate/audio';">Audio to Text</button>
26+
<button class="btn btn-primary btn-lg" type="submit" onclick="location.href = '/translate/fromASL';">ASL to Text</button>
27+
<button class="btn btn-primary btn-lg" type="submit" onclick="location.href = '/translate/toASL';">Text to ASL</button>
28+
<button class="btn btn-primary btn-lg" type="submit" onclick="location.href = '/translate/audio';">Audio to ASL</button>
2929
<button class="btn btn-primary btn-lg" type="submit" onclick="location.href = '/translate/connect';">Connect Online</button>
3030
</div>
3131

32-
<!--<div class="container">
33-
<div class="row">
34-
<div class="col-lg-8 offset-lg-2">
35-
<form method="post">
36-
<input type="submit" value="Start Translation" />
37-
</form>
38-
<img src="{{ url_for('views.video_feed') }}">
39-
</div>
40-
</div>
41-
</div>-->
4232
{% endblock %}

0 commit comments

Comments
 (0)