Commit bd153756 authored by Paranagama R.P.S.D.'s avatar Paranagama R.P.S.D.

changes with large file changes

parent 610ab229
models/*
!models/
\ No newline at end of file
...@@ -14,4 +14,182 @@ ...@@ -14,4 +14,182 @@
*.ts *.ts
files/* files/*
!files/ !files/
\ No newline at end of file
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
### Python Patch ###
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
poetry.toml
# ruff
.ruff_cache/
# LSP config files
pyrightconfig.json
# End of https://www.toptal.com/developers/gitignore/api/python
\ No newline at end of file
# TMP-23-029 # TMP-23-029
SLIIT Final Year Project SLIIT Final Year Project
\ No newline at end of file
Python Environment - Python 3.10.11
Commands
Install libraries
pip install -r requirements.txt
Run application
uvicorn main:app --reload
...@@ -2,3 +2,5 @@ ...@@ -2,3 +2,5 @@
2023-05-19 00:32:23,385 - INFO - Received request at root endpoint. 2023-05-19 00:32:23,385 - INFO - Received request at root endpoint.
2023-05-19 00:32:48,522 - ERROR - Received request at root endpoint. 2023-05-19 00:32:48,522 - ERROR - Received request at root endpoint.
2023-05-19 00:32:48,522 - ERROR - Received request at root endpoint. 2023-05-19 00:32:48,522 - ERROR - Received request at root endpoint.
2023-05-19 23:09:38,565 - INFO - Failed to make predictions. name 'CLASSES' is not defined
2023-05-19 23:09:38,565 - INFO - Failed to make predictions. name 'CLASSES' is not defined
...@@ -7,6 +7,8 @@ from pydantic import BaseModel ...@@ -7,6 +7,8 @@ from pydantic import BaseModel
import tensorflow as tf import tensorflow as tf
from core import setup_logger from core import setup_logger
from services.translate_service import SignLanguagePredictionService
from utils import mappings
router = APIRouter() router = APIRouter()
...@@ -16,11 +18,15 @@ class ImageRequest(BaseModel): ...@@ -16,11 +18,15 @@ class ImageRequest(BaseModel):
image: UploadFile image: UploadFile
# Load your Keras model # Load your Keras model
model = tf.keras.models.load_model('D:\RP\SL-Detection-Action-Recognition\models\model') model = tf.keras.models.load_model('../ML_Models/sign_language_to_text/models/sign_language_model.h5')
CLASSES = os.listdir('D:\RP\SL-Detection-Action-Recognition\data\Sn_sign_language_dataset') # list of classes CLASSES = mappings.classes
NUM_CLASSES = len(CLASSES) # number of classes NUM_CLASSES = len(mappings.classes) # number of classes
IMG_SIZE = 224 # image size IMG_SIZE = 224 # image size
# Instantiate the service class
prediction_service = SignLanguagePredictionService(model, CLASSES, mappings)
@router.post("/upload/video") @router.post("/upload/video")
async def upload_video(video: UploadFile = File(...)): async def upload_video(video: UploadFile = File(...)):
try: try:
...@@ -37,27 +43,23 @@ async def upload_video(video: UploadFile = File(...)): ...@@ -37,27 +43,23 @@ async def upload_video(video: UploadFile = File(...)):
detail="Failed to upload the video" detail="Failed to upload the video"
) )
@router.post('/predict-sign-language') @router.post('/predict-sign-language/image')
def predict(image_request: UploadFile = File(...)): def predict_using_image(image_request: UploadFile = File(...)):
try: try:
file_location = f"files/{image_request.filename}" return prediction_service.predict_sign_language(image_request)
with open(file_location, "wb") as file: except Exception as e:
file.write(image_request.file.read()) logger.info(f"Error. {e}")
raise HTTPException(
# Load the saved image using OpenCV status_code=500,
img = cv2.imread(file_location) detail="Request Failed."
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) )
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)) @router.post('/predict-sign-language/video')
img = np.array([img], dtype=np.float32) / 255.0 def predict_using_video(video_request: UploadFile = File(...)):
try:
# Make prediction return prediction_service.predict_sign_language_video(video_request)
prediction = model.predict(img)
class_name = CLASSES[np.argmax(prediction)]
return {'class_name': class_name}
except Exception as e: except Exception as e:
logger.info(f"Failed to make predictions. {e}") logger.info(f"Error. {e}")
raise HTTPException( raise HTTPException(
status_code=500, status_code=500,
detail="Failed to make predictions" detail="Request Failed."
) )
\ No newline at end of file
...@@ -2,6 +2,10 @@ from fastapi import APIRouter ...@@ -2,6 +2,10 @@ from fastapi import APIRouter
router = APIRouter() router = APIRouter()
@router.get("/ping")
def test():
# Your code here
return {"pong"}
@router.get("/users") @router.get("/users")
def get_users(): def get_users():
......
import os
import cv2
import numpy as np
from fastapi import HTTPException, UploadFile
from typing import Dict
import tensorflow as tf
from core import setup_logger
from utils import mappings
logger = setup_logger()
IMG_SIZE = 224 # image size
class SignLanguagePredictionService:
def __init__(self, model, classes, mappings):
self.model = model
self.classes = classes
self.mappings = mappings
def predict_sign_language(self, image_request: UploadFile) -> Dict[str, str]:
try:
file_location = f"files/{image_request.filename}"
with open(file_location, "wb") as file:
file.write(image_request.file.read())
img = cv2.imread(file_location)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
img = np.array([img], dtype=np.float32) / 255.0
prediction = self.model.predict(img)
class_index = np.argmax(prediction)
class_name = self.classes[class_index]
sinhala_letter = self.mappings.letter_mapping.get(class_name, 'Unknown')
# Delete the image file
os.remove(file_location)
return {'prediction': sinhala_letter}
except Exception as e:
logger.info(f"Failed to make predictions. {e}")
raise HTTPException(
status_code=500,
detail="Failed to make predictions"
)
def predict_sign_language_video(self, video_request: UploadFile) -> 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 = []
frame_count = 0
# Loop through the frames of the video
while frame_count < 20:
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 = 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
video.release()
# Delete the video file
os.remove(video_location)
return {'frame_count': frame_count, 'predictions': predictions}
except Exception as e:
logger.info(f"Failed to make predictions. {e}")
raise HTTPException(
status_code=500,
detail="Failed to make predictions"
)
letter_mapping = {
'Ah': 'අ',
'Aah': 'ආ',
'Aeh': 'ඇ',
'Ee': 'ඉ',
'Eeh': 'ඊ',
'Uh': 'උ',
'Uhh': 'ඌ',
'A': 'එ',
'Ae': 'ඒ',
'O': 'ඔ',
'Ohh': 'ඕ',
'K': 'ක්',
'Ig': 'ග්',
'T': 'ටී'
}
classes =['A',
'Aah',
'Ae',
'Aeh',
'Ah',
'Ee',
'Eeh',
'Ig',
'K',
'O',
'Ohh',
'T',
'Uh',
'Uhh']
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment