Commit 3f692d5a authored by supundileepa00's avatar supundileepa00

feat: Make predictions by uploading a image

parent e2e2a81b
...@@ -11,4 +11,7 @@ ...@@ -11,4 +11,7 @@
*.webm *.webm
*.vob *.vob
*.m4v *.m4v
*.ts *.ts
\ No newline at end of file
files/*
!files/
\ No newline at end of file
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
from core import setup_logger from core import setup_logger
...@@ -6,19 +12,53 @@ from core import setup_logger ...@@ -6,19 +12,53 @@ from core import setup_logger
router = APIRouter() router = APIRouter()
logger = setup_logger() logger = setup_logger()
class ImageRequest(BaseModel):
image: UploadFile
# Load your Keras model
model = tf.keras.models.load_model('D:\RP\SL-Detection-Action-Recognition\models\model')
CLASSES = os.listdir('D:\RP\SL-Detection-Action-Recognition\data\Sn_sign_language_dataset') # list of classes
NUM_CLASSES = len(CLASSES) # number of classes
IMG_SIZE = 224 # image size
@router.post("/upload/video") @router.post("/upload/video")
async def upload_video(video: UploadFile = File(...)): async def upload_video(video: UploadFile = File(...)):
try: try:
# Save the uploaded video to the "uploads" folder
logger.info("Received request at root endpoint.")
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())
return {"filename": video.filename} 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, status_code=500,
detail="Failed to upload the video" detail="Failed to upload the video"
)
@router.post('/predict-sign-language')
def predict(image_request: UploadFile = File(...)):
try:
file_location = f"files/{image_request.filename}"
with open(file_location, "wb") as file:
file.write(image_request.file.read())
# Load the saved image using OpenCV
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
# Make prediction
prediction = model.predict(img)
class_name = CLASSES[np.argmax(prediction)]
return {'class_name': class_name}
except Exception as e:
logger.info(f"Failed to make predictions. {e}")
raise HTTPException(
status_code=500,
detail="Failed to make predictions"
) )
\ No newline at end of file
fastapi fastapi
uvicorn uvicorn
python-multipart==0.0.6 python-multipart==0.0.6
\ No newline at end of file tensorflow==2.12.0
opencv-python==4.7.0.72
\ 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