Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2023-029
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2023-029
2023-029
Commits
48efc63a
Commit
48efc63a
authored
Jul 12, 2023
by
Paranagama R.P.S.D.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat : Make predictions with different speed levels
parent
958b0f37
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
101 additions
and
4 deletions
+101
-4
Project/Backend/Server_Python/app.log
Project/Backend/Server_Python/app.log
+15
-0
Project/Backend/Server_Python/controllers/translate_controler.py
.../Backend/Server_Python/controllers/translate_controler.py
+14
-2
Project/Backend/Server_Python/services/translate_service.py
Project/Backend/Server_Python/services/translate_service.py
+62
-1
Project/Backend/Server_Python/utils/mappings.py
Project/Backend/Server_Python/utils/mappings.py
+10
-1
No files found.
Project/Backend/Server_Python/app.log
View file @
48efc63a
...
...
@@ -16,3 +16,18 @@
2023-07-12 05:50:25,202 - INFO - Error. 'SignLanguagePredictionService' object has no attribute 'predict_sign_language_video2'
2023-07-12 05:50:25,202 - INFO - Error. 'SignLanguagePredictionService' object has no attribute 'predict_sign_language_video2'
2023-07-12 05:50:25,202 - INFO - Error. 'SignLanguagePredictionService' object has no attribute 'predict_sign_language_video2'
2023-07-12 06:33:48,435 - INFO - Error. SignLanguagePredictionService.predict_sign_language_video_new2() missing 1 required positional argument: 'speed'
2023-07-12 06:33:48,435 - INFO - Error. SignLanguagePredictionService.predict_sign_language_video_new2() missing 1 required positional argument: 'speed'
2023-07-12 06:33:48,435 - INFO - Error. SignLanguagePredictionService.predict_sign_language_video_new2() missing 1 required positional argument: 'speed'
2023-07-12 06:34:27,777 - INFO - Error. SignLanguagePredictionService.predict_sign_language_video_new2() missing 1 required positional argument: 'speed'
2023-07-12 06:34:27,777 - INFO - Error. SignLanguagePredictionService.predict_sign_language_video_new2() missing 1 required positional argument: 'speed'
2023-07-12 06:34:27,777 - INFO - Error. SignLanguagePredictionService.predict_sign_language_video_new2() missing 1 required positional argument: 'speed'
2023-07-12 06:34:33,502 - INFO - Error. SignLanguagePredictionService.predict_sign_language_video_new2() missing 1 required positional argument: 'speed'
2023-07-12 06:34:33,502 - INFO - Error. SignLanguagePredictionService.predict_sign_language_video_new2() missing 1 required positional argument: 'speed'
2023-07-12 06:34:33,502 - INFO - Error. SignLanguagePredictionService.predict_sign_language_video_new2() missing 1 required positional argument: 'speed'
2023-07-12 06:39:07,178 - INFO - Failed to make predictions. local variable 'threshold_percentage' referenced before assignment
2023-07-12 06:39:07,178 - INFO - Failed to make predictions. local variable 'threshold_percentage' referenced before assignment
2023-07-12 06:39:07,178 - INFO - Failed to make predictions. local variable 'threshold_percentage' referenced before assignment
2023-07-12 06:39:07,180 - INFO - Error.
2023-07-12 06:39:07,180 - INFO - Error.
2023-07-12 06:39:07,180 - INFO - Error.
Project/Backend/Server_Python/controllers/translate_controler.py
View file @
48efc63a
from
fastapi
import
APIRouter
,
File
,
HTTPException
,
UploadFile
from
fastapi
import
APIRouter
,
File
,
HTTPException
,
Query
,
UploadFile
from
pydantic
import
BaseModel
import
tensorflow
as
tf
from
core.logger
import
setup_logger
...
...
@@ -19,9 +19,10 @@ model = tf.keras.models.load_model('../ML_Models/sign_language_to_text/models/si
CLASSES
=
mappings
.
classes
NUM_CLASSES
=
len
(
mappings
.
classes
)
# number of classes
IMG_SIZE
=
224
# image size
speed_levels
=
mappings
.
speed_levels
# Instantiate the service class
prediction_service
=
SignLanguagePredictionService
(
model
,
CLASSES
,
mappings
)
prediction_service
=
SignLanguagePredictionService
(
model
,
CLASSES
,
mappings
,
speed_levels
)
@
router
.
post
(
"/upload/video"
,
tags
=
[
"Sign Language"
])
...
...
@@ -54,6 +55,17 @@ def predict_using_image(image_request: UploadFile = File(...)):
def
predict_using_video
(
video_request
:
UploadFile
=
File
(
...
)):
try
:
return
prediction_service
.
predict_sign_language_video_new
(
video_request
)
except
Exception
as
e
:
logger
.
info
(
f
"Error. {e}"
)
raise
HTTPException
(
status_code
=
500
,
detail
=
"Request Failed."
)
@
router
.
post
(
'/predict-sign-language/video/speed_levels'
,
tags
=
[
"Sign Language"
])
def
predict_using_video
(
video_request
:
UploadFile
=
File
(
...
),
speed
:
int
=
Query
(
...
)):
try
:
return
prediction_service
.
predict_sign_language_video_with_speed_levels
(
video_request
,
speed
=
speed
)
except
Exception
as
e
:
logger
.
info
(
f
"Error. {e}"
)
raise
HTTPException
(
...
...
Project/Backend/Server_Python/services/translate_service.py
View file @
48efc63a
...
...
@@ -13,10 +13,11 @@ logger = setup_logger()
IMG_SIZE
=
224
# image size
class
SignLanguagePredictionService
:
def
__init__
(
self
,
model
,
classes
,
mappings
):
def
__init__
(
self
,
model
,
classes
,
mappings
,
speed_levels
):
self
.
model
=
model
self
.
classes
=
classes
self
.
mappings
=
mappings
self
.
speed_levels
=
speed_levels
def
predict_sign_language
(
self
,
image_request
:
UploadFile
)
->
Dict
[
str
,
str
]:
try
:
...
...
@@ -149,6 +150,66 @@ class SignLanguagePredictionService:
status_code
=
500
,
detail
=
"Failed to make predictions"
)
def
predict_sign_language_video_with_speed_levels
(
self
,
video_request
:
UploadFile
,
speed
:
int
)
->
Dict
[
str
,
str
]:
try
:
# Create a temporary file to save the video
video_location
=
f
"files/{video_request.filename}"
with
open
(
video_location
,
"wb"
)
as
file
:
file
.
write
(
video_request
.
file
.
read
())
# Read the video using OpenCV
video
=
cv2
.
VideoCapture
(
video_location
)
predictions
=
[]
final_predictions
=
[]
frame_count
=
0
# Determine the number of frames per sign based on the speed level
frames_per_sign
=
self
.
speed_levels
.
get
(
speed
,
50
)
# Default to level 1 if speed level is not provided
# Loop through the frames of the video
while
frame_count
<
50
:
success
,
frame
=
video
.
read
()
if
not
success
:
break
# Preprocess the frame
frame
=
cv2
.
cvtColor
(
frame
,
cv2
.
COLOR_BGR2RGB
)
frame
=
cv2
.
resize
(
frame
,
(
IMG_SIZE
,
IMG_SIZE
))
frame
=
extract_hand_shape
(
frame
)
frame
=
np
.
array
([
frame
],
dtype
=
np
.
float32
)
/
255.0
# Make prediction
prediction
=
self
.
model
.
predict
(
frame
)
class_index
=
np
.
argmax
(
prediction
)
class_name
=
self
.
classes
[
class_index
]
sinhala_letter
=
self
.
mappings
.
letter_mapping
.
get
(
class_name
,
'Unknown'
)
# Store the prediction for the frame
predictions
.
append
(
sinhala_letter
)
frame_count
+=
1
threshold_percentage
=
60
# Check if the required number of frames per sign has been reached
if
frame_count
%
frames_per_sign
==
0
:
predictions
=
get_predicted_percentage
(
predictions
,
threshold_percentage
)
final_predictions
=
final_predictions
+
predictions
predictions
=
[]
video
.
release
()
# Delete the video file
os
.
remove
(
video_location
)
return
{
'frame_count'
:
frame_count
,
'predictions'
:
final_predictions
}
except
Exception
as
e
:
logger
.
info
(
f
"Failed to make predictions. {e}"
)
raise
HTTPException
(
status_code
=
500
,
detail
=
"Failed to make predictions"
)
def
extract_hand_shape
(
image
):
gray
=
cv2
.
cvtColor
(
image
,
cv2
.
COLOR_RGB2GRAY
)
...
...
Project/Backend/Server_Python/utils/mappings.py
View file @
48efc63a
...
...
@@ -27,4 +27,13 @@ classes =['A',
'Ohh'
,
'T'
,
'Uh'
,
'Uhh'
]
\ No newline at end of file
'Uhh'
]
speed_levels
=
{
1
:
50
,
# 10 frames per sign for level 1
2
:
40
,
# 20 frames per sign for level 2
3
:
30
,
# 30 frames per sign for level 3
4
:
20
,
# 40 frames per sign for level 4
5
:
10
# 50 frames per sign for level 5
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment