fix: update

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