Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +3 -0
- app.py +25 -0
- requirements.txt +1 -0
Dockerfile
CHANGED
|
@@ -25,6 +25,9 @@ RUN mkdir -p /root/.u2net && \
|
|
| 25 |
RUN python -c "import easyocr; easyocr.Reader(['es', 'en'], gpu=False)"
|
| 26 |
RUN python -c "from simple_lama_inpainting import SimpleLama; SimpleLama()"
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
COPY app.py .
|
| 29 |
|
| 30 |
EXPOSE 7860
|
|
|
|
| 25 |
RUN python -c "import easyocr; easyocr.Reader(['es', 'en'], gpu=False)"
|
| 26 |
RUN python -c "from simple_lama_inpainting import SimpleLama; SimpleLama()"
|
| 27 |
|
| 28 |
+
# Descarga el modelo de identificación de fuentes (licencia MIT).
|
| 29 |
+
RUN python -c "from transformers import AutoImageProcessor, AutoModelForImageClassification; AutoImageProcessor.from_pretrained('gaborcselle/font-identifier'); AutoModelForImageClassification.from_pretrained('gaborcselle/font-identifier')"
|
| 30 |
+
|
| 31 |
COPY app.py .
|
| 32 |
|
| 33 |
EXPOSE 7860
|
app.py
CHANGED
|
@@ -3,12 +3,14 @@ import base64
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
import easyocr
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
from fastapi.responses import Response
|
| 10 |
from rembg import remove, new_session
|
| 11 |
from simple_lama_inpainting import SimpleLama
|
|
|
|
| 12 |
|
| 13 |
app = FastAPI(title="ZGrafic API")
|
| 14 |
|
|
@@ -31,6 +33,12 @@ ocr_reader = easyocr.Reader(["es", "en"], gpu=False)
|
|
| 31 |
lama = SimpleLama()
|
| 32 |
TEXT_PADDING = 4
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# El alpha matting es pesado: en imágenes grandes puede quedarse sin
|
| 35 |
# memoria o tardar demasiado en el CPU gratuito. Por encima de este
|
| 36 |
# tamaño, lo desactivamos automáticamente aunque el usuario lo pida.
|
|
@@ -224,6 +232,19 @@ def estimate_text_color(image_bgr, x1, y1, x2, y2):
|
|
| 224 |
return "#{:02x}{:02x}{:02x}".format(int(r), int(g), int(b))
|
| 225 |
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
@app.post("/extract-text")
|
| 228 |
async def extract_text(file: UploadFile = File(...)):
|
| 229 |
if not file.content_type or not file.content_type.startswith("image/"):
|
|
@@ -258,6 +279,9 @@ async def extract_text(file: UploadFile = File(...)):
|
|
| 258 |
|
| 259 |
color = estimate_text_color(image_bgr, x1, y1, x2, y2)
|
| 260 |
|
|
|
|
|
|
|
|
|
|
| 261 |
texts.append({
|
| 262 |
"text": text,
|
| 263 |
"x": x1,
|
|
@@ -266,6 +290,7 @@ async def extract_text(file: UploadFile = File(...)):
|
|
| 266 |
"height": y2 - y1,
|
| 267 |
"fontSize": max(8, y2 - y1),
|
| 268 |
"color": color,
|
|
|
|
| 269 |
})
|
| 270 |
|
| 271 |
cv2.rectangle(mask, (x1, y1), (x2, y2), 255, thickness=-1)
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
import easyocr
|
| 6 |
+
import torch
|
| 7 |
from PIL import Image
|
| 8 |
from fastapi import FastAPI, File, UploadFile, HTTPException, Form
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
from fastapi.responses import Response
|
| 11 |
from rembg import remove, new_session
|
| 12 |
from simple_lama_inpainting import SimpleLama
|
| 13 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 14 |
|
| 15 |
app = FastAPI(title="ZGrafic API")
|
| 16 |
|
|
|
|
| 33 |
lama = SimpleLama()
|
| 34 |
TEXT_PADDING = 4
|
| 35 |
|
| 36 |
+
# Identifica la fuente más parecida entre 48 fuentes estándar (licencia
|
| 37 |
+
# MIT, sin restricción de uso comercial).
|
| 38 |
+
font_processor = AutoImageProcessor.from_pretrained("gaborcselle/font-identifier")
|
| 39 |
+
font_model = AutoModelForImageClassification.from_pretrained("gaborcselle/font-identifier")
|
| 40 |
+
font_model.eval()
|
| 41 |
+
|
| 42 |
# El alpha matting es pesado: en imágenes grandes puede quedarse sin
|
| 43 |
# memoria o tardar demasiado en el CPU gratuito. Por encima de este
|
| 44 |
# tamaño, lo desactivamos automáticamente aunque el usuario lo pida.
|
|
|
|
| 232 |
return "#{:02x}{:02x}{:02x}".format(int(r), int(g), int(b))
|
| 233 |
|
| 234 |
|
| 235 |
+
def identify_font(pil_crop):
|
| 236 |
+
"""Devuelve el nombre de la fuente (de un set de 48 fuentes estándar)
|
| 237 |
+
más parecida a la del recorte de texto dado."""
|
| 238 |
+
try:
|
| 239 |
+
inputs = font_processor(images=pil_crop.convert("RGB"), return_tensors="pt")
|
| 240 |
+
with torch.no_grad():
|
| 241 |
+
logits = font_model(**inputs).logits
|
| 242 |
+
predicted_id = logits.argmax(-1).item()
|
| 243 |
+
return font_model.config.id2label[predicted_id]
|
| 244 |
+
except Exception:
|
| 245 |
+
return None
|
| 246 |
+
|
| 247 |
+
|
| 248 |
@app.post("/extract-text")
|
| 249 |
async def extract_text(file: UploadFile = File(...)):
|
| 250 |
if not file.content_type or not file.content_type.startswith("image/"):
|
|
|
|
| 279 |
|
| 280 |
color = estimate_text_color(image_bgr, x1, y1, x2, y2)
|
| 281 |
|
| 282 |
+
crop_rgb = cv2.cvtColor(image_bgr[y1:y2, x1:x2], cv2.COLOR_BGR2RGB)
|
| 283 |
+
font_name = identify_font(Image.fromarray(crop_rgb))
|
| 284 |
+
|
| 285 |
texts.append({
|
| 286 |
"text": text,
|
| 287 |
"x": x1,
|
|
|
|
| 290 |
"height": y2 - y1,
|
| 291 |
"fontSize": max(8, y2 - y1),
|
| 292 |
"color": color,
|
| 293 |
+
"font": font_name,
|
| 294 |
})
|
| 295 |
|
| 296 |
cv2.rectangle(mask, (x1, y1), (x2, y2), 255, thickness=-1)
|
requirements.txt
CHANGED
|
@@ -6,3 +6,4 @@ onnxruntime==1.19.2
|
|
| 6 |
pillow==9.5.0
|
| 7 |
easyocr==1.7.1
|
| 8 |
simple-lama-inpainting==0.1.2
|
|
|
|
|
|
| 6 |
pillow==9.5.0
|
| 7 |
easyocr==1.7.1
|
| 8 |
simple-lama-inpainting==0.1.2
|
| 9 |
+
transformers==4.44.0
|