Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
from fastapi import FastAPI, Response
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
from PIL import Image
|
| 5 |
import io
|
| 6 |
import base64
|
| 7 |
import re
|
| 8 |
import numpy as np
|
| 9 |
-
import cv2
|
| 10 |
|
| 11 |
-
# Pydantic model to expect two base64 strings
|
| 12 |
class InpaintRequest(BaseModel):
|
| 13 |
image_data: str
|
| 14 |
mask_data: str
|
|
@@ -23,40 +21,34 @@ app.add_middleware(
|
|
| 23 |
allow_headers=["*"],
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# --- NO HEAVY MODEL TO LOAD! THE APP STARTS INSTANTLY. ---
|
| 27 |
print("OpenCV Magic Eraser API is ready!")
|
| 28 |
|
| 29 |
def base64_to_cv2_image(base64_string):
|
| 30 |
-
"""Decodes a base64 string into an OpenCV image (numpy array)."""
|
| 31 |
base64_data = re.sub('^data:image/.+;base64,', '', base64_string)
|
| 32 |
img_data = base64.b64decode(base64_data)
|
| 33 |
np_arr = np.frombuffer(img_data, np.uint8)
|
| 34 |
-
# cv2.imdecode reads an image from the buffer
|
| 35 |
return cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 36 |
|
| 37 |
@app.post("/inpaint")
|
| 38 |
async def inpaint_image(request: InpaintRequest):
|
| 39 |
try:
|
| 40 |
-
# 1. Decode base64 strings into OpenCV images
|
| 41 |
init_image = base64_to_cv2_image(request.image_data)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# We'll make any non-black pixel white
|
| 49 |
-
_, mask = cv2.threshold(mask_image_gray, 1, 255, cv2.THRESH_BINARY)
|
| 50 |
|
| 51 |
print("Received images. Starting high-speed inpainting...")
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
#
|
| 55 |
-
inpainted_image = cv2.inpaint(init_image,
|
| 56 |
|
| 57 |
-
print("Inpainting complete
|
| 58 |
|
| 59 |
-
#
|
| 60 |
_, buffer = cv2.imencode('.png', inpainted_image)
|
| 61 |
img_str = base64.b64encode(buffer).decode("utf-8")
|
| 62 |
|
|
|
|
| 1 |
from fastapi import FastAPI, Response
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
|
|
|
| 4 |
import io
|
| 5 |
import base64
|
| 6 |
import re
|
| 7 |
import numpy as np
|
| 8 |
+
import cv2
|
| 9 |
|
|
|
|
| 10 |
class InpaintRequest(BaseModel):
|
| 11 |
image_data: str
|
| 12 |
mask_data: str
|
|
|
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
|
|
|
| 24 |
print("OpenCV Magic Eraser API is ready!")
|
| 25 |
|
| 26 |
def base64_to_cv2_image(base64_string):
|
|
|
|
| 27 |
base64_data = re.sub('^data:image/.+;base64,', '', base64_string)
|
| 28 |
img_data = base64.b64decode(base64_data)
|
| 29 |
np_arr = np.frombuffer(img_data, np.uint8)
|
|
|
|
| 30 |
return cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
| 31 |
|
| 32 |
@app.post("/inpaint")
|
| 33 |
async def inpaint_image(request: InpaintRequest):
|
| 34 |
try:
|
|
|
|
| 35 |
init_image = base64_to_cv2_image(request.image_data)
|
| 36 |
|
| 37 |
+
# --- SIMPLIFIED MASK HANDLING ---
|
| 38 |
+
# The mask is now perfectly black and white from the frontend.
|
| 39 |
+
# We just need to convert it to grayscale for the algorithm.
|
| 40 |
+
mask_color = base64_to_cv2_image(request.mask_data)
|
| 41 |
+
mask_gray = cv2.cvtColor(mask_color, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
print("Received images. Starting high-speed inpainting...")
|
| 44 |
|
| 45 |
+
# Run the high-speed OpenCV inpainting algorithm
|
| 46 |
+
# The mask is already perfect, no thresholding needed.
|
| 47 |
+
inpainted_image = cv2.inpaint(init_image, mask_gray, 3, cv2.INPAINT_NS)
|
| 48 |
|
| 49 |
+
print("Inpainting complete.")
|
| 50 |
|
| 51 |
+
# Convert the result back to a base64 string
|
| 52 |
_, buffer = cv2.imencode('.png', inpainted_image)
|
| 53 |
img_str = base64.b64encode(buffer).decode("utf-8")
|
| 54 |
|