maggidev commited on
Commit
add03f2
·
verified ·
1 Parent(s): 4d840e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -23
app.py CHANGED
@@ -11,69 +11,107 @@ import gradio as gr
11
 
12
  MODEL = "model.pt"
13
 
 
14
  manga_translator = MangaTranslator()
15
  mocr = MangaOcr()
16
 
17
 
18
  def predict(img, translation_method, font):
19
- image = np.array(img)
 
 
 
 
20
 
21
- results = detect_bubbles(MODEL, image)
22
 
23
- page_mask = np.zeros(image.shape[:2], dtype=np.uint8)
24
  bubbles_data = []
25
 
26
- # 1️⃣ Extrai TODAS as máscaras primeiro
 
 
27
  for x1, y1, x2, y2, score, class_id in results:
28
  x1, y1, x2, y2 = map(int, (x1, y1, x2, y2))
29
- crop = image[y1:y2, x1:x2]
30
 
31
- mask, contour = extract_bubble_mask(crop)
 
 
 
 
 
 
32
  if mask is None:
33
  continue
34
 
35
  page_mask[y1:y2, x1:x2][mask == 255] = 255
36
- bubbles_data.append((x1, y1, x2, y2, crop, contour))
37
 
38
- # 2️⃣ Limpa a página UMA VEZ
39
- image[page_mask == 255] = (255, 255, 255)
40
-
41
- # 3️⃣ OCR + Tradução + Escrita
42
- for x1, y1, x2, y2, crop, contour in bubbles_data:
43
- pil_crop = Image.fromarray(crop.astype(np.uint8))
44
- text = mocr(pil_crop)
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  translated = manga_translator.translate(
47
- text, method=translation_method
 
48
  )
49
 
50
- image[y1:y2, x1:x2] = add_text(
51
- image[y1:y2, x1:x2],
52
  translated,
53
  font,
54
  contour
55
  )
56
 
57
- return Image.fromarray(image)
 
58
 
 
 
 
59
 
60
  demo = gr.Interface(
61
  fn=predict,
62
  inputs=[
63
  "image",
64
  gr.Dropdown(
65
- [("Google", "google"), ("HF", "hf")],
 
 
 
 
 
66
  value="google",
67
- label="Translation"
68
  ),
69
  gr.Dropdown(
70
- [("WildWords", "fonts/WildWordsRoman.ttf")],
 
 
 
 
 
71
  value="fonts/WildWordsRoman.ttf",
72
  label="Font"
73
  )
74
  ],
75
  outputs=gr.Image(),
76
- title="Manga Translator"
 
77
  )
78
 
79
- demo.launch()
 
11
 
12
  MODEL = "model.pt"
13
 
14
+ # Modelos carregados uma única vez
15
  manga_translator = MangaTranslator()
16
  mocr = MangaOcr()
17
 
18
 
19
  def predict(img, translation_method, font):
20
+ # =========================
21
+ # Imagens separadas por função
22
+ # =========================
23
+ original_image = np.array(img).copy() # usada SOMENTE para OCR
24
+ render_image = np.array(img).copy() # usada para limpeza + escrita
25
 
26
+ results = detect_bubbles(MODEL, original_image)
27
 
28
+ page_mask = np.zeros(original_image.shape[:2], dtype=np.uint8)
29
  bubbles_data = []
30
 
31
+ # =========================
32
+ # 1) OCR + extração de máscara (SEM modificar imagem)
33
+ # =========================
34
  for x1, y1, x2, y2, score, class_id in results:
35
  x1, y1, x2, y2 = map(int, (x1, y1, x2, y2))
 
36
 
37
+ crop_original = original_image[y1:y2, x1:x2]
38
+
39
+ # OCR SEMPRE na imagem original
40
+ pil_crop = Image.fromarray(crop_original.astype(np.uint8))
41
+ text = mocr(pil_crop)
42
+
43
+ mask, contour = extract_bubble_mask(crop_original)
44
  if mask is None:
45
  continue
46
 
47
  page_mask[y1:y2, x1:x2][mask == 255] = 255
 
48
 
49
+ bubbles_data.append({
50
+ "box": (x1, y1, x2, y2),
51
+ "text": text,
52
+ "contour": contour
53
+ })
54
+
55
+ # =========================
56
+ # 2) Limpeza global da página (UMA VEZ)
57
+ # =========================
58
+ render_image[page_mask == 255] = (255, 255, 255)
59
+
60
+ # =========================
61
+ # 3) Tradução + escrita
62
+ # =========================
63
+ for bubble in bubbles_data:
64
+ x1, y1, x2, y2 = bubble["box"]
65
+ text = bubble["text"]
66
+ contour = bubble["contour"]
67
 
68
  translated = manga_translator.translate(
69
+ text,
70
+ method=translation_method
71
  )
72
 
73
+ render_image[y1:y2, x1:x2] = add_text(
74
+ render_image[y1:y2, x1:x2],
75
  translated,
76
  font,
77
  contour
78
  )
79
 
80
+ return Image.fromarray(render_image)
81
+
82
 
83
+ # =========================
84
+ # Interface Gradio
85
+ # =========================
86
 
87
  demo = gr.Interface(
88
  fn=predict,
89
  inputs=[
90
  "image",
91
  gr.Dropdown(
92
+ [
93
+ ("Google", "google"),
94
+ ("HuggingFace", "hf"),
95
+ ("Bing", "bing"),
96
+ ("Sogou", "sogou")
97
+ ],
98
  value="google",
99
+ label="Translation Method"
100
  ),
101
  gr.Dropdown(
102
+ [
103
+ ("Wild Words Roman", "fonts/WildWordsRoman.ttf"),
104
+ ("Anime Ace Italic", "fonts/animeace_i.ttf"),
105
+ ("Mangati", "fonts/mangati.ttf"),
106
+ ("Komika Axis", "fonts/KOMIKAX_.ttf")
107
+ ],
108
  value="fonts/WildWordsRoman.ttf",
109
  label="Font"
110
  )
111
  ],
112
  outputs=gr.Image(),
113
+ title="Manga Translator",
114
+ description="Translate manga speech bubbles safely and correctly"
115
  )
116
 
117
+ demo.launch(debug=False, share=False)