Video Translation process

parent 5e201cfa
from fastapi import FastAPI, HTTPException
from pymongo.mongo_client import MongoClient
from pydantic import BaseModel
from typing import List
from bson import ObjectId
app = FastAPI()
# Replace with your MongoDB Atlas credentials
username = "admin"
password = "JppbU6MZeHfOj7sp"
uri = f"mongodb+srv://{username}:{password}@researchmanagement-appl.vzhn4.mongodb.net/?retryWrites=true&w=majority"
client = MongoClient(uri)
db = client["test"]
items_collection = db["items"]
class ItemCreate(BaseModel):
item_name: str
item_description: str
class Item(BaseModel):
item_name: str
item_description: str
_id: str
@app.on_event("startup")
async def startup_db_client():
app.mongodb_client = MongoClient(uri)
try:
app.mongodb_client.admin.command("ping")
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
@app.on_event("shutdown")
async def shutdown_db_client():
app.mongodb_client.close()
@app.get("/")
async def read_root():
return {"message": "FastAPI with MongoDB integration"}
@app.post("/items/", response_model=Item)
async def create_item(item_create: ItemCreate):
item_data = {
"item_name": item_create.item_name,
"item_description": item_create.item_description,
}
result = items_collection.insert_one(item_data)
if result.inserted_id:
return {**item_data, "_id": str(result.inserted_id)}
else:
raise HTTPException(status_code=500, detail="Failed to create item")
@app.put("/items/{item_id}", response_model=Item)
async def update_item(item_id: str, item_update: ItemCreate):
item_object_id = ObjectId(item_id) # Convert item_id to ObjectId
update_data = {
"item_name": item_update.item_name,
"item_description": item_update.item_description,
}
result = items_collection.update_one({"_id": item_object_id}, {"$set": update_data})
if result.modified_count > 0:
updated_item = items_collection.find_one({"_id": item_object_id})
return {**updated_item, "_id": str(updated_item["_id"])}
else:
raise HTTPException(status_code=404, detail="Item not found")
@app.get("/items/", response_model=List[Item])
async def get_all_items():
items = list(items_collection.find())
items_with_string_id = [{**item, "_id": str(item["_id"])} for item in items]
print(items_with_string_id)
return items_with_string_id
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)
......@@ -2,4 +2,8 @@ fastapi
uvicorn
python-multipart==0.0.6
tensorflow==2.12.0
opencv-python==4.7.0.72
\ No newline at end of file
opencv-python==4.7.0.72
moviepy==1.0.3
SpeechRecognition==3.10.0
tk==0.1.0
requests==2.31.0
\ No newline at end of file
import moviepy.editor as mp
import speech_recognition as sr
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import scrolledtext
import requests
# 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
def convert_video():
video_path = filedialog.askopenfilename(filetypes=[("Video Files", "*.mp4")])
if not video_path:
messagebox.showwarning("Warning", "No video file selected.")
return
try:
audio_save_path = "audio.wav"
video = mp.VideoFileClip(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")
# Get the integer values and join them into a single string
translated_integer_si = ' '.join(str(unicode_to_int_mapping.get(word, 0)) for word in translated_text_si.split())
textarea_si.delete('1.0', tk.END)
textarea_en.delete('1.0', tk.END)
textarea_si.insert(tk.END, f"Translated Integer: {translated_integer_si}\n{translated_text_si}")
textarea_en.insert(tk.END, translated_text_en)
except Exception as e:
messagebox.showerror("Error", str(e))
window = tk.Tk()
window.title("Video Translation")
window.geometry("800x600")
title_label = tk.Label(window, text="Video Translation", font=("Arial", 16, "bold"))
title_label.pack(pady=10)
convert_button = tk.Button(window, text="Convert Video", command=convert_video)
convert_button.pack(pady=10)
textarea_si_label = tk.Label(window, text="Sinhala Unicode Translation")
textarea_si_label.pack()
textarea_si = scrolledtext.ScrolledText(window, wrap=tk.WORD, width=80, height=10)
textarea_si.pack()
textarea_en_label = tk.Label(window, text="Singlish Translation")
textarea_en_label.pack()
textarea_en = scrolledtext.ScrolledText(window, wrap=tk.WORD, width=80, height=10)
textarea_en.pack()
window.mainloop()
\ No newline at end of file
......@@ -12,7 +12,9 @@ import {
ShoppingOutlined,
TeamOutlined,
TranslationOutlined,
UserOutlined
UserOutlined,
FastForwardOutlined,
RedditOutlined
} from '@ant-design/icons';
// type
......@@ -29,7 +31,9 @@ const icons = {
LoginOutlined,
RocketOutlined,
BookOutlined,
TranslationOutlined
TranslationOutlined,
FastForwardOutlined,
RedditOutlined
};
// ==============================|| MENU ITEMS - SUPPORT ||============================== //
......@@ -74,6 +78,20 @@ const application: NavItemType = {
icon: icons.TranslationOutlined,
url: '/ssl-translate/process',
},
{
id: 'video-to-sign-language',
title: <FormattedMessage id="video-to-sign-language" />,
type: 'collapse',
icon: icons.RedditOutlined,
children: [
{
id: 'video-translate',
title: <FormattedMessage id="video-translate" />,
type: 'item',
url: '/video-to-sign-language/VideoTranslate',
}
]
},
{
id: 'learning-management',
title: <FormattedMessage id="learning-management" />,
......
// project import
import MainCard from 'components/MainCard';
import ScrollX from 'components/ScrollX';
import axios from 'axios'; // Import the axios library
// ==============================|| List ||============================== //
const VideoTranslate = () => {
const handleConvertClick = async () => {
const videoInput = document.getElementById('video-input') as HTMLInputElement | null;
if (!videoInput) {
alert('Unable to find the video input element.');
return;
}
if (!videoInput.files || !videoInput.files[0]) {
alert('Please select a video file.');
return;
}
const formData = new FormData();
formData.append('video', videoInput.files[0]);
try {
await axios.post('/Backend/Server_Python/services/video_to_sign_language', formData);
alert('Video conversion successful.');
} catch (error) {
alert('Video conversion failed.');
}
};
return (
<MainCard content={false}>
<ScrollX>
<h3> Video Translation here </h3>
<div>
<h4>Sinhala Unicode Translation</h4>
<textarea
// value={sinhalaTranslation}
// onChange={(e) => setSinhalaTranslation(e.target.value)}
/>
</div>
<div>
<h4>Singlish Translation</h4>
<textarea
// value={singlishTranslation}
// onChange={(e) => setSinglishTranslation(e.target.value)}
/>
</div>
<div>
<h4>Upload Video</h4>
<input type="file" accept=".mp4" id="video-input" />
</div>
<div>
<button onClick={handleConvertClick}>Convert Video</button>
</div>
</ScrollX>
</MainCard>
);
};
export default VideoTranslate;
// // project import
// import { useState } from 'react';
// import MainCard from 'components/MainCard';
// import ScrollX from 'components/ScrollX';
// // assets
// //types
// // ==============================|| List ||============================== //
// const VideoTranslate = () => {
// const [sinhalaTranslation, setSinhalaTranslation] = useState('');
// const [singlishTranslation, setSinglishTranslation] = useState('');
// const handleConvertClick = () => {
// // Perform the video translation logic here
// // You can use the values from `sinhalaTranslation` and `singlishTranslation`
// };
// return (
// <MainCard content={false}>
// <ScrollX>
// <h3> Video Translation here </h3>
// <div>
// <h4>Sinhala Unicode Translation</h4>
// <textarea
// value={sinhalaTranslation}
// onChange={(e) => setSinhalaTranslation(e.target.value)}
// />
// </div>
// <div>
// <h4>Singlish Translation</h4>
// <textarea
// value={singlishTranslation}
// onChange={(e) => setSinglishTranslation(e.target.value)}
// />
// </div>
// <div>
// <button onClick={handleConvertClick}>Convert Video</button>
// </div>
// </ScrollX>
// </MainCard>
// );
// };
// export default VideoTranslate;
......@@ -41,6 +41,9 @@ const TutorialManagementList = Loadable(lazy(() => import('pages/parameter/tutor
// render - ssl translate process page
const SSLTranslateProcess = Loadable(lazy(() => import('pages/ssl-translate/process/process')));
// render - Video to Sign Language page
const VideoTranslation = Loadable(lazy(() => import('pages/video-to-sign-language/VideoTranslate/VideoTranslate')));
// render - learning-curriculums-subscribed page
const LearningCurriculumsSubscribed = Loadable(lazy(() => import('pages/learning-management/learning-curriculums-subscribed/list/list')));
......@@ -96,6 +99,15 @@ const MainRoutes = {
}
]
},
{
path: 'video-to-sign-language',
children: [
{
path: 'VideoTranslate',
element: <VideoTranslation />
}
]
},
{
path: 'learning-management',
children: [
......
......@@ -161,5 +161,7 @@
"learning-curriculums-subscribed": "My Courses",
"learning-lead-board": "Lead Board",
"learning-feedback": "Feedback",
"ssl-translate": "SSL Translate"
"ssl-translate": "SSL Translate",
"video-to-sign-language": "Sign Language Translate",
"video-translate": "Video Translator"
}
\ 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