from fastapi import FastAPI, UploadFile, File from fastapi.responses import JSONResponse from PIL import Image import numpy as np import tensorflow as tf import io app = FastAPI() # Загрузка модели model = tf.keras.models.load_model('fine_tuned_model.h5') class_names = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] # Трансформация изображения def transform_image(image_bytes): image = Image.open(io.BytesIO(image_bytes)).convert("RGB") image = image.resize((32, 32)) image = np.array(image) / 255.0 image = np.expand_dims(image, axis=0) return image @app.get("/healthcheck") def healthcheck(): return JSONResponse(status_code=200, content={}) @app.post("/api/v1/detectObjects") async def detect_objects(file: UploadFile = File(...)): image_bytes = await file.read() image = transform_image(image_bytes) predictions = model.predict(image) predicted_class = class_names[np.argmax(predictions[0])] response = { "class": predicted_class, "confidence": float(np.max(predictions[0])) } return JSONResponse(content=response) @app.get("/") async def main(): return {"message": "FastAPI Object Detection"}