Python Source code

parent bf1514cc
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import get_file
from tensorflow.keras.utils import load_img
from tensorflow.keras.utils import img_to_array
from tensorflow import expand_dims
from tensorflow.nn import softmax
from numpy import argmax
from numpy import max
from numpy import array
from json import dumps
from uvicorn import run
import os
import requests
import shutil
app = FastAPI()
origins = ["*"]
methods = ["*"]
headers = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins = origins,
allow_credentials = True,
allow_methods = methods,
allow_headers = headers
)
model_dir = "Fish_identificaton.h5"
model = load_model(model_dir)
# Providing the folder path
origin = r"C:\xampp\htdocs\templates\php\files\fishimage.jpg"
target = r"E:\4th Year\1st Sem\Research\Docker\Backend\fishimage.jpg"
target2 = r"E:\4th Year\1st Sem\Research\Docker\Backend\templates\fishimage.jpg"
# Function to get result
# async def get_net_image_prediction(image_link: str = ""):
# if image_link == "":
# print(image_link)
# return {"message": "No image link provided"}
# response = requests.get(image_link)
# filename = "fishimage.jpg"
# with open(filename, "wb") as f:
# f.write(response.content)
# # img_path = get_file(
# # origin = image_link
# # )
def get_net_image_prediction():
# myfile = open('C:/xampp/htdocs/templates/php/files/fishimage.jpg')
# myfile = open('C:\\xampp\\htdocs\\templates\\php\\files\\fishimage.jpg','r')
shutil.copy(origin,target)
shutil.copy(origin,target2)
img_path = "fishimage.jpg"
img = load_img(
img_path,
target_size = (331, 331)
)
img_array = img_to_array(img)
img_array = expand_dims(img_array, 0)
pred = model.predict(img_array)
max_value = max(pred[0])
duplicate_list = list(pred[0])
index = duplicate_list.index(max_value)
lables = ['feed', 'group', 'individual', 'aggresive', 'nervous']
rt= lables[index]
return {
rt
}
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/")
async def root():
return {"message": "Welcome to the Fish Identifications API!"}
# @app.post("/net/image/prediction/")
# def getFishtype():
# rtt = get_net_image_prediction()
# return rtt
@app.get("/getResult")
async def send_image_get_prediction(request: Request):
rtt = get_net_image_prediction()
return templates.TemplateResponse("Saveimage.html", {"request": request, "variable": rtt})
# async def send_image_get_prediction():
# rtt = get_net_image_prediction()
# return rtt
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
run(app, host="0.0.0.0", port=port)
\ 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