import uvicorn from fastapi import FastAPI, File, UploadFile import cv2 import numpy as np import os app = FastAPI() face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_alt2.xml") anime_cascade = cv2.CascadeClassifier(os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "animu.xml")) @app.post("/process") def process(img_data: bytes = File(None)): try: im = cv2.imdecode(np.frombuffer(img_data, np.uint8), cv2.IMREAD_COLOR) gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) normGray = cv2.equalizeHist(gray) animeFaces = anime_cascade.detectMultiScale(normGray, 1.1, 5, 0, (24, 24)) normalFaces = face_cascade.detectMultiScale(normGray) return { "ok": True, "animeFaces": [{"x": x.item(), "y": y.item(), "w": w.item(), "h": h.item()} for (x, y, w, h) in animeFaces], "normalFaces": [{"x": x.item(), "y": y.item(), "w": w.item(), "h": h.item()} for (x, y, w, h) in normalFaces] } except Exception as err: return { "ok": False, "error": str(err) } if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8081)