Upload 3 files
Browse files- app.py +4 -5
- livro.py +17 -19
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
from livro import processar_e_mostrar, verificar_contraste, gerar_preview_kdp
|
| 4 |
|
| 5 |
with gr.Blocks() as demo:
|
| 6 |
with gr.Row():
|
| 7 |
-
imagem_input = gr.Image(label="Imagem Original", type="
|
| 8 |
mascara = gr.ImageEditor(label="Marcar áreas para preservar (opcional)")
|
| 9 |
with gr.Row():
|
| 10 |
idade = gr.Slider(minimum=4, maximum=12, step=1, value=6, label="Idade da criança (ajusta número de pontos)")
|
|
@@ -20,10 +19,10 @@ with gr.Blocks() as demo:
|
|
| 20 |
|
| 21 |
resultado = {}
|
| 22 |
|
| 23 |
-
def gerar(
|
| 24 |
global resultado
|
| 25 |
-
aviso = verificar_contraste(
|
| 26 |
-
resultado = processar_e_mostrar(
|
| 27 |
return aviso, resultado["preview"]
|
| 28 |
|
| 29 |
def salvar_pdf():
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from livro import processar_e_mostrar, verificar_contraste, gerar_preview_kdp
|
| 3 |
|
| 4 |
with gr.Blocks() as demo:
|
| 5 |
with gr.Row():
|
| 6 |
+
imagem_input = gr.Image(label="Imagem Original", type="numpy")
|
| 7 |
mascara = gr.ImageEditor(label="Marcar áreas para preservar (opcional)")
|
| 8 |
with gr.Row():
|
| 9 |
idade = gr.Slider(minimum=4, maximum=12, step=1, value=6, label="Idade da criança (ajusta número de pontos)")
|
|
|
|
| 19 |
|
| 20 |
resultado = {}
|
| 21 |
|
| 22 |
+
def gerar(imagem_array, mask_array, idade):
|
| 23 |
global resultado
|
| 24 |
+
aviso = verificar_contraste(imagem_array)
|
| 25 |
+
resultado = processar_e_mostrar(imagem_array, mask_array, idade)
|
| 26 |
return aviso, resultado["preview"]
|
| 27 |
|
| 28 |
def salvar_pdf():
|
livro.py
CHANGED
|
@@ -1,37 +1,36 @@
|
|
| 1 |
-
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from reportlab.pdfgen import canvas
|
| 5 |
from reportlab.lib.units import inch
|
| 6 |
import tempfile
|
| 7 |
-
import os
|
| 8 |
from PIL import Image, ImageDraw, ImageFont
|
| 9 |
|
| 10 |
PAGE_WIDTH = 8.67 * inch
|
| 11 |
PAGE_HEIGHT = 11.5 * inch
|
| 12 |
MARGIN = 0.5 * inch
|
| 13 |
|
| 14 |
-
def verificar_contraste(
|
| 15 |
-
|
| 16 |
-
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 17 |
contrast = gray.std()
|
| 18 |
if contrast < 30:
|
| 19 |
-
return "⚠️ Baixo contraste detectado! Use uma imagem
|
| 20 |
return "✅ Contraste adequado."
|
| 21 |
|
| 22 |
-
def detectar_pontos(
|
| 23 |
-
|
| 24 |
-
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 25 |
_, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
| 30 |
if not contours:
|
| 31 |
return []
|
| 32 |
|
| 33 |
main_contour = max(contours, key=cv2.contourArea)
|
| 34 |
-
max_points = min(30 + (idade - 4) * 10, 150)
|
| 35 |
approx = [tuple(pt[0]) for pt in main_contour[::max(1, len(main_contour)//max_points)]]
|
| 36 |
return sorted(approx, key=lambda p: (p[1], p[0]))
|
| 37 |
|
|
@@ -54,8 +53,8 @@ def gerar_pdf(pontos):
|
|
| 54 |
c.save()
|
| 55 |
return temp_pdf.name
|
| 56 |
|
| 57 |
-
def processar_e_mostrar(
|
| 58 |
-
pontos = detectar_pontos(
|
| 59 |
preview = gerar_preview_com_pontos(pontos)
|
| 60 |
pdf = gerar_pdf(pontos)
|
| 61 |
return {"preview": preview, "pdf": pdf}
|
|
@@ -65,11 +64,10 @@ def gerar_preview_kdp():
|
|
| 65 |
draw = ImageDraw.Draw(img)
|
| 66 |
font = ImageFont.load_default()
|
| 67 |
draw.rectangle([50, 50, 1450, 950], outline="black", width=4)
|
| 68 |
-
draw.text((600, 100), "
|
| 69 |
-
draw.text((580, 160), "
|
| 70 |
draw.ellipse((1100, 700, 1300, 900), outline="gray", width=3)
|
| 71 |
-
draw.text((1120, 780), "
|
| 72 |
-
|
| 73 |
capa_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
| 74 |
img.save(capa_path)
|
| 75 |
return capa_path
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
from reportlab.pdfgen import canvas
|
| 4 |
from reportlab.lib.units import inch
|
| 5 |
import tempfile
|
|
|
|
| 6 |
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
|
| 8 |
PAGE_WIDTH = 8.67 * inch
|
| 9 |
PAGE_HEIGHT = 11.5 * inch
|
| 10 |
MARGIN = 0.5 * inch
|
| 11 |
|
| 12 |
+
def verificar_contraste(imagem_array):
|
| 13 |
+
gray = cv2.cvtColor(imagem_array, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 14 |
contrast = gray.std()
|
| 15 |
if contrast < 30:
|
| 16 |
+
return "⚠️ Baixo contraste detectado! Use uma imagem com contorno escuro e fundo branco puro."
|
| 17 |
return "✅ Contraste adequado."
|
| 18 |
|
| 19 |
+
def detectar_pontos(imagem_array, mascara_array=None, idade=6):
|
| 20 |
+
gray = cv2.cvtColor(imagem_array, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 21 |
_, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)
|
| 22 |
+
|
| 23 |
+
if mascara_array is not None:
|
| 24 |
+
mask = cv2.cvtColor(mascara_array, cv2.COLOR_BGR2GRAY)
|
| 25 |
+
_, mask_thresh = cv2.threshold(mask, 10, 255, cv2.THRESH_BINARY)
|
| 26 |
+
thresh = cv2.bitwise_and(thresh, mask_thresh)
|
| 27 |
+
|
| 28 |
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
| 29 |
if not contours:
|
| 30 |
return []
|
| 31 |
|
| 32 |
main_contour = max(contours, key=cv2.contourArea)
|
| 33 |
+
max_points = min(30 + (idade - 4) * 10, 150)
|
| 34 |
approx = [tuple(pt[0]) for pt in main_contour[::max(1, len(main_contour)//max_points)]]
|
| 35 |
return sorted(approx, key=lambda p: (p[1], p[0]))
|
| 36 |
|
|
|
|
| 53 |
c.save()
|
| 54 |
return temp_pdf.name
|
| 55 |
|
| 56 |
+
def processar_e_mostrar(imagem_array, mascara_array, idade):
|
| 57 |
+
pontos = detectar_pontos(imagem_array, mascara_array, idade)
|
| 58 |
preview = gerar_preview_com_pontos(pontos)
|
| 59 |
pdf = gerar_pdf(pontos)
|
| 60 |
return {"preview": preview, "pdf": pdf}
|
|
|
|
| 64 |
draw = ImageDraw.Draw(img)
|
| 65 |
font = ImageFont.load_default()
|
| 66 |
draw.rectangle([50, 50, 1450, 950], outline="black", width=4)
|
| 67 |
+
draw.text((600, 100), "Dot-to-Dot Book", fill="black", font=font)
|
| 68 |
+
draw.text((580, 160), "For Kids Ages 4 to 12", fill="black", font=font)
|
| 69 |
draw.ellipse((1100, 700, 1300, 900), outline="gray", width=3)
|
| 70 |
+
draw.text((1120, 780), "Your Art Here", fill="gray", font=font)
|
|
|
|
| 71 |
capa_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
| 72 |
img.save(capa_path)
|
| 73 |
return capa_path
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
|
| 2 |
-
gradio
|
| 3 |
opencv-python
|
| 4 |
numpy
|
| 5 |
reportlab
|
|
|
|
| 1 |
|
| 2 |
+
gradio==4.21.0
|
| 3 |
opencv-python
|
| 4 |
numpy
|
| 5 |
reportlab
|