File size: 1,515 Bytes
7cf86f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import io
import base64

import numpy as np
from PIL import Image
from ultralytics import YOLO

from utils.predict_bounding_boxes import predict_bounding_boxes
from utils.manga_ocr_utils import get_text_from_image
from utils.translate_manga import translate_manga
from utils.process_contour import process_contour
from utils.write_text_on_image import add_text

MODEL_PATH = "./model_creation/runs/detect/train5/weights/best.pt"
object_detection_model = YOLO(MODEL_PATH)

def extract_text_from_regions(image: np.ndarray, results: list):

	for result in results:
		x1, y1, x2, y2, _, _ = result
		detected_image = image[int(y1):int(y2), int(x1):int(x2)]
		if detected_image.shape[-1] == 4:
			detected_image = detected_image[:, :, :3]
		im = Image.fromarray(np.uint8(detected_image * 255))
		text = get_text_from_image(im)

		processed_image, cont = process_contour(detected_image)
		translated_text = translate_manga(text, source_lang="auto", target_lang="en")
		add_text(processed_image, translated_text, cont)


def convert_image_to_base64(image: Image.Image) -> str:
	buff = io.BytesIO()
	image.save(buff, format="PNG")
	return base64.b64encode(buff.getvalue()).decode("utf-8")


def predict(image: np.ndarray):

	image = Image.fromarray(image)
	image.save("image.png")

	try:
		np_image = np.array(image)

		results = predict_bounding_boxes(object_detection_model, "image.png")
		extract_text_from_regions(np_image, results)

		return np_image

	except Exception as e:
		print(f"Error: {str(e)}")
		return None