Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,70 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
exec(os.environ.get('CODE'))
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi import FastAPI, File, UploadFile, Form, Request
|
| 3 |
+
from fastapi.responses import HTMLResponse, FileResponse
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
from fastapi.templating import Jinja2Templates
|
| 6 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
+
from fastapi.responses import StreamingResponse
|
| 9 |
+
from fastapi import FastAPI, File, UploadFile
|
| 10 |
+
from fastapi.responses import StreamingResponse
|
| 11 |
+
import cv2
|
| 12 |
+
import numpy as np
|
| 13 |
+
from PIL import Image
|
| 14 |
+
import io
|
| 15 |
+
import os
|
| 16 |
|
| 17 |
+
import onnxruntime as ort
|
| 18 |
+
|
| 19 |
+
_sess_options = ort.SessionOptions()
|
| 20 |
+
_sess_options.intra_op_num_threads = os.cpu_count()
|
| 21 |
+
MODEL_SESS = ort.InferenceSession(
|
| 22 |
+
"cartoonizer.onnx", _sess_options, providers=["CPUExecutionProvider"]
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
def preprocess_image(image: np.ndarray) -> np.ndarray:
|
| 26 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 27 |
+
|
| 28 |
+
h, w, c = np.shape(image)
|
| 29 |
+
if min(h, w) > 720:
|
| 30 |
+
if h > w:
|
| 31 |
+
h, w = int(720 * h / w), 720
|
| 32 |
+
else:
|
| 33 |
+
h, w = 720, int(720 * w / h)
|
| 34 |
+
image = cv2.resize(image, (w, h), interpolation=cv2.INTER_AREA)
|
| 35 |
+
h, w = (h // 8) * 8, (w // 8) * 8
|
| 36 |
+
image = image[:h, :w, :]
|
| 37 |
+
image = image.astype(np.float32) / 127.5 - 1
|
| 38 |
+
return np.expand_dims(image, axis=0)
|
| 39 |
+
|
| 40 |
+
def inference(image: np.ndarray) -> np.ndarray:
|
| 41 |
+
image = preprocess_image(image)
|
| 42 |
+
results = MODEL_SESS.run(None, {"input_photo:0": image})
|
| 43 |
+
output = (np.squeeze(results[0]) + 1.0) * 127.5
|
| 44 |
+
output = np.clip(output, 0, 255).astype(np.uint8)
|
| 45 |
+
output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB) # Convert BGR to RGB
|
| 46 |
+
return output
|
| 47 |
+
|
| 48 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 49 |
+
|
| 50 |
+
app.add_middleware(
|
| 51 |
+
CORSMiddleware,
|
| 52 |
+
allow_origins=["*"], # Adjust as needed, '*' allows requests from any origin
|
| 53 |
+
allow_credentials=True,
|
| 54 |
+
allow_methods=["*"],
|
| 55 |
+
allow_headers=["*"],
|
| 56 |
+
)
|
| 57 |
+
app = FastAPI()
|
| 58 |
+
|
| 59 |
+
@app.post("/cartoonize/")
|
| 60 |
+
async def cartoonize_image(file: UploadFile = File(...)):
|
| 61 |
+
contents = await file.read()
|
| 62 |
+
image = Image.open(io.BytesIO(contents))
|
| 63 |
+
image = np.array(image)
|
| 64 |
+
cartoonized_image = inference(image)
|
| 65 |
+
|
| 66 |
+
# Convert BGR to RGB format
|
| 67 |
+
cartoonized_image_rgb = cv2.cvtColor(cartoonized_image, cv2.COLOR_BGR2RGB)
|
| 68 |
+
|
| 69 |
+
return StreamingResponse(io.BytesIO(cv2.imencode('.jpg', cartoonized_image_rgb)[1]), media_type="image/jpeg")
|
| 70 |
|
|
|