Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
37 |
+
def gerar_preview_com_pontos(pontos):
|
38 |
+
preview = np.ones((int(PAGE_HEIGHT), int(PAGE_WIDTH), 3), dtype=np.uint8) * 255
|
39 |
+
for i, (x, y) in enumerate(pontos):
|
40 |
+
cv2.circle(preview, (x, y), 4, (0, 0, 0), -1)
|
41 |
+
cv2.putText(preview, str(i+1), (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0), 1)
|
42 |
+
preview_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
43 |
+
cv2.imwrite(preview_path, preview)
|
44 |
+
return preview_path
|
45 |
+
|
46 |
+
def gerar_pdf(pontos):
|
47 |
+
temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
48 |
+
c = canvas.Canvas(temp_pdf.name, pagesize=(PAGE_WIDTH, PAGE_HEIGHT))
|
49 |
+
for i, (x, y) in enumerate(pontos):
|
50 |
+
c.circle(x, PAGE_HEIGHT - y, 2, fill=1)
|
51 |
+
c.setFont("Helvetica", 8)
|
52 |
+
c.drawString(x + 3, PAGE_HEIGHT - y + 3, str(i + 1))
|
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}
|
61 |
+
|
62 |
+
def gerar_preview_kdp():
|
63 |
+
img = Image.new("RGB", (1500, 1000), color=(250, 240, 210))
|
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
|