fix: update

desc : update project
parent a35b6b73
import base64 import base64
import os import os
import cv2 import cv2
from fastapi import APIRouter, File, HTTPException,UploadFile from fastapi import APIRouter, File, HTTPException, UploadFile
import numpy as np import numpy as np
from pydantic import BaseModel from pydantic import BaseModel
import tensorflow as tf import tensorflow as tf
...@@ -14,11 +14,14 @@ from utils import mappings ...@@ -14,11 +14,14 @@ from utils import mappings
router = APIRouter() router = APIRouter()
logger = setup_logger() logger = setup_logger()
class ImageRequest(BaseModel): class ImageRequest(BaseModel):
image: UploadFile image: UploadFile
# Load your Keras model # Load your Keras model
model = tf.keras.models.load_model('../ML_Models/sign_language_to_text/models/sign_language_model.h5') # model = tf.keras.models.load_model('../ML_Models/sign_language_to_text/models/sign_language_model.h5')
model = None
CLASSES = mappings.classes CLASSES = mappings.classes
NUM_CLASSES = len(mappings.classes) # number of classes NUM_CLASSES = len(mappings.classes) # number of classes
IMG_SIZE = 224 # image size IMG_SIZE = 224 # image size
...@@ -30,7 +33,6 @@ prediction_service = SignLanguagePredictionService(model, CLASSES, mappings) ...@@ -30,7 +33,6 @@ 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:
file_location = f"files/{video.filename}" file_location = f"files/{video.filename}"
with open(file_location, "wb") as file: with open(file_location, "wb") as file:
file.write(video.file.read()) file.write(video.file.read())
...@@ -38,28 +40,22 @@ async def upload_video(video: UploadFile = File(...)): ...@@ -38,28 +40,22 @@ async def upload_video(video: UploadFile = File(...)):
return {"text": "ආආආආආආආ"} return {"text": "ආආආආආආආ"}
except Exception as e: except Exception as e:
logger.info(f"Failed to upload file. {e}") logger.info(f"Failed to upload file. {e}")
raise HTTPException( raise HTTPException(status_code=500, detail="Failed to upload the video")
status_code=500,
detail="Failed to upload the video"
)
@router.post('/predict-sign-language/image')
@router.post("/predict-sign-language/image")
def predict_using_image(image_request: UploadFile = File(...)): def predict_using_image(image_request: UploadFile = File(...)):
try: try:
return prediction_service.predict_sign_language(image_request) return prediction_service.predict_sign_language(image_request)
except Exception as e: except Exception as e:
logger.info(f"Error. {e}") logger.info(f"Error. {e}")
raise HTTPException( raise HTTPException(status_code=500, detail="Request Failed.")
status_code=500,
detail="Request Failed."
) @router.post("/predict-sign-language/video")
@router.post('/predict-sign-language/video')
def predict_using_video(video_request: UploadFile = File(...)): def predict_using_video(video_request: UploadFile = File(...)):
try: try:
return prediction_service.predict_sign_language_video(video_request) return prediction_service.predict_sign_language_video(video_request)
except Exception as e: except Exception as e:
logger.info(f"Error. {e}") logger.info(f"Error. {e}")
raise HTTPException( raise HTTPException(status_code=500, detail="Request Failed.")
status_code=500,
detail="Request Failed."
)
\ No newline at end of file
...@@ -4,6 +4,22 @@ from fastapi.responses import RedirectResponse ...@@ -4,6 +4,22 @@ from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from core import setup_logger from core import setup_logger
import io
import os
import subprocess
import tkinter as tk
import urllib.parse
from tkinter import filedialog, messagebox, scrolledtext
from typing import List
import moviepy.editor as mp
import requests
import speech_recognition as sr
from bson import ObjectId
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
app = FastAPI() app = FastAPI()
logger = setup_logger() logger = setup_logger()
...@@ -33,94 +49,79 @@ async def root(): ...@@ -33,94 +49,79 @@ async def root():
return RedirectResponse(url) return RedirectResponse(url)
# import io # Define a dictionary to map Sinhala Unicode to integers
# import os unicode_to_int_mapping = {"මම": 1, "හෙට": 2, "යනවා": 3, "මං": 4}
# import subprocess
# import tkinter as tk
# import urllib.parse def translate_text(text, target_language):
# from tkinter import filedialog, messagebox, scrolledtext url = "https://translate.googleapis.com/translate_a/single"
# from typing import List params = {
"client": "gtx",
# import moviepy.editor as mp "sl": "auto",
# import requests "tl": target_language,
# import speech_recognition as sr "dt": "t",
# from bson import ObjectId "q": text,
# from fastapi import FastAPI, File, UploadFile }
# from fastapi.responses import JSONResponse response = requests.get(url, params=params)
translation_data = response.json()
# # Define a dictionary to map Sinhala Unicode to integers translated_text = translation_data[0][0][0]
# unicode_to_int_mapping = {"මම": 1, "හෙට": 2, "යනවා": 3, "මං": 4} return translated_text
# def translate_text(text, target_language): # send to service.py
# url = "https://translate.googleapis.com/translate_a/single" def send_to_service(translated_integer_si):
# params = { url = "http://127.0.0.1:8000/translated_items/"
# "client": "gtx", data = {
# "sl": "auto", "translated_integer_si": translated_integer_si,
# "tl": target_language, }
# "dt": "t", response = requests.post(url, json=data)
# "q": text, if response.status_code == 200:
# } print("Data sent to server successfully")
# response = requests.get(url, params=params) else:
# translation_data = response.json() print("Failed to send data to server")
# translated_text = translation_data[0][0][0]
# return translated_text
@app.get("/rest_pyton/welcome")
async def root():
# # send to service.py return {"message": "Hello, World!"}
# def send_to_service(translated_integer_si):
# url = "http://127.0.0.1:8000/translated_items/"
# data = { @app.post("/rest_pyton/uploaded_video")
# "translated_integer_si": translated_integer_si, async def uploaded_video(file: UploadFile = File(...)):
# } try:
# response = requests.post(url, json=data) # Process the uploaded video
# if response.status_code == 200: video_content = await file.read()
# print("Data sent to server successfully") temp_video_path = "C:/Users/himashara/Documents/SLIIT/1 SEMESTER/Research Project - IT4010/Research Project/2023-029/Project/Backend/Server_Python/resources/temp_video.mp4"
# else: with open(temp_video_path, "wb") as temp_video_file:
# print("Failed to send data to server") temp_video_file.write(video_content)
audio_save_path = "C:/Users/himashara/Documents/SLIIT/1 SEMESTER/Research Project - IT4010/Research Project/2023-029/Project/Backend/Server_Python/resources/audio.wav"
# @app.get("/rest_pyton/welcome") video = mp.VideoFileClip(temp_video_path)
# async def root(): audio = video.audio
# return {"message": "Hello, World!"} audio.write_audiofile(audio_save_path)
r = sr.Recognizer()
# @app.post("/rest_pyton/uploaded_video") with sr.AudioFile(audio_save_path) as source:
# async def uploaded_video(file: UploadFile = File(...)): audio = r.record(source)
# try:
# # Process the uploaded video recognized_text = r.recognize_google(audio, language="si-LK")
# video_content = await file.read() translated_text_si = translate_text(recognized_text, "si")
# temp_video_path = "temp_video.mp4" translated_text_en = translate_text(recognized_text, "en")
# with open(temp_video_path, "wb") as temp_video_file:
# temp_video_file.write(video_content) translated_integer_si = " ".join(
str(unicode_to_int_mapping.get(word, 0))
# audio_save_path = "audio.wav" for word in translated_text_si.split()
# video = mp.VideoFileClip(temp_video_path) )
# audio = video.audio print("Translated Integer (Si):", translated_integer_si)
# audio.write_audiofile(audio_save_path)
# send_to_service(translated_integer_si)
# r = sr.Recognizer()
# with sr.AudioFile(audio_save_path) as source: return JSONResponse(
# audio = r.record(source) content={
"translated_text_si": translated_text_si,
# recognized_text = r.recognize_google(audio, language="si-LK") "translated_text_en": translated_text_en,
# translated_text_si = translate_text(recognized_text, "si") }
# translated_text_en = translate_text(recognized_text, "en") )
# return JSONResponse(content={"translated_text_si": "test"})
# translated_integer_si = " ".join( except Exception as e:
# str(unicode_to_int_mapping.get(word, 0)) return JSONResponse(content={"error": str(e)}, status_code=500)
# for word in translated_text_si.split()
# )
# print("Translated Integer (Si):", translated_integer_si)
# send_to_service(translated_integer_si)
# return JSONResponse(
# content={
# "translated_text_si": translated_text_si,
# "translated_text_en": translated_text_en,
# }
# )
# # return JSONResponse(content={"translated_text_si": "test"})
# except Exception as e:
# return JSONResponse(content={"error": str(e)}, status_code=500)
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