import os import uvicorn import cv2 import numpy as np from fastapi import FastAPI, File, UploadFile from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse, HTMLResponse from tensorflow.keras.models import load_model app = FastAPI() # Read allowed origins from env var (comma-separated) ALLOWED_ORIGINS = [ "ALLOWED_ORIGINS", "http://localhost:3000," "https://sih-em37.vercel.app" ] app.add_middleware( CORSMiddleware, allow_origins=ALLOWED_ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Load model model = load_model("my_image_model.h5") class_indices = { 0: "Gir_cow", 1: "Murrah_buffalo", 2: "Red_Sindhi_cow", 3: "Sahiwal_cow", 4: "Tharparkar_cow", 5: "amritmahal_cow", 6: "banni_buffalo", 7: "bhadwari_buffalo", 8: "dharwadi_buffalo", 9: "jafarabadi_buffalo", } # Minimum confidence required CONFIDENCE_THRESHOLD = 0.70 def preprocess_frame(frame, target_size=(224, 224)): img = cv2.resize(frame, target_size) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img.astype("float32") / 255.0 img = np.expand_dims(img, axis=0) return img @app.get("/") async def index(): html_content = """