File size: 1,251 Bytes
badb447
 
 
 
 
 
 
 
 
909f40a
badb447
909f40a
 
 
 
 
 
 
 
 
badb447
 
 
 
 
 
 
909f40a
 
 
badb447
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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"}