AICOLDFORANE / main.py
Anni01's picture
Update main.py
f4e1b82 verified
Raw
History Blame Contribute Delete
4.5 kB
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from PIL import Image
import shutil
import os
from moviepy.editor import VideoFileClip
from fastapi.responses import FileResponse
import ultralytics
from ultralytics import RTDETR
import cv2
import torch
import numpy as np
from pathlib import Path
import numpy as np
from numpy import array
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import string
import os
from PIL import Image
import glob
import imageio
import random
import pickle
from pickle import dump, load
from time import time
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import LSTM, Embedding, TimeDistributed, Dense, RepeatVector,\
Activation, Flatten, Reshape, concatenate, Dropout, BatchNormalization
from keras.optimizers import Adam, RMSprop
from keras.layers import Bidirectional
from keras.layers import add
from keras.preprocessing import image
from keras.models import Model
from keras import Input, layers
from keras import optimizers
app = FastAPI()
app.mount("/static", StaticFiles(directory="app/static"), name="static")
templates = Jinja2Templates(directory="app/templates")
@app.get("/", response_class=FileResponse)
async def home(request: dict = {}):
return templates.TemplateResponse("index.html", {"request": request})
@app.post("/upload")
async def create_upload_file(option: str = Form(...), file: UploadFile = File(...), request: dict = {}):
with open(f"app/static/{file.filename}", "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# Placeholder image processing logic using PIL (replace with your actual logic)
processed_filename = f"processed_{file.filename}"
processed_file_path = os.path.join("app/static", processed_filename)
if option == "image":
with Image.open(f"app/static/{file.filename}") as img:
# Resize the image (you can replace this with your actual processing)
img_resized = img.resize((640, 640))
# img_resized.save(processed_file_path)
# processing
<!-- model = RTDETR('app/models/rtdetr_20.pt')
# resized_frame = cv2.resize(frame, (new_width, new_height))
rgb_frame = cv2.cvtColor(np.array(img_resized), cv2.COLOR_BGR2RGB)
tensor_frame = torch.from_numpy(np.expand_dims(np.transpose(rgb_frame, (2, 0, 1)), axis=0)).float()
results = model(tensor_frame)
for r in results:
im_array = r.plot()
annotated_image_array = cv2.cvtColor(im_array, cv2.COLOR_BGR2RGB)
annotated_image = Image.fromarray(annotated_image_array)
annotated_image.save(processed_file_path)-->
output = f"Image Processing on {file.filename} based on {option}"
return templates.TemplateResponse("after.html",{"request": request,"data":generate_caption()})
def from_model():
inputs1 = Input(shape=(2048,))
fe1 = Dropout(0.5)(inputs1)
fe2 = Dense(256, activation='relu')(fe1)
inputs2 = Input(shape=(max_length,))
se1 = Embedding(vocab_size, embedding_dim, mask_zero=True)(inputs2)
se2 = Dropout(0.5)(se1)
se3 = LSTM(256)(se2)
decoder1 = add([fe2, se3])
decoder2 = Dense(256, activation='relu')(decoder1)
outputs = Dense(vocab_size, activation='softmax')(decoder2)
model = Model(inputs=[inputs1, inputs2], outputs=outputs)
model.load_weights('./model_weights/model_30.h5')
return model
def ModeSearch(photo):
in_text = 'startseq'
for i in range(max_length):
sequence = [wordtoix[w] for w in in_text.split() if w in wordtoix]
sequence = pad_sequences([sequence], maxlen=max_length)
model=from_model()
yhat = model.predict([photo, sequence], verbose=0)
yhat = np.argmax(yhat)
word = ixtoword[yhat]
in_text += ' ' + word
if word == 'endseq':
break
final = in_text.split()
final = final[1:-1]
final = ' '.join(final)
return final
def generate_caption():
# Select a random image from the validation dataset
random_image_key = random.choice(list(encoding_test.keys()))
image = encoding_test[random_image_key].reshape((1, 2048))
# Loading and displaying the random im
x = plt.imread(os.path.join(images, random_image_key))
return ModeSearch(image)