82 lines
3.6 KiB
Python
82 lines
3.6 KiB
Python
import cv2
|
|
from io import BytesIO
|
|
import base64
|
|
from PIL import Image
|
|
import urllib, requests
|
|
import numpy as np
|
|
|
|
from img_trans.src.text_detection_with_paddle import detect_text
|
|
from img_trans.src.removal import process_image_based_on_text_area
|
|
from img_trans.src.translation import translate_texts_translatepy
|
|
from img_trans.src.text_insertion import insert_text
|
|
from img_trans.src.mini_crop_process import resize_and_crop_image
|
|
from img_trans.src.rotate_img import flip_and_rotate_image
|
|
from img_trans.src.inpainting import inpaint_text # 기존 인페인팅 메서드
|
|
|
|
def image_trans(image_url, convert_type):
|
|
# 이미지를 URL로부터 읽어옵니다.
|
|
image = read_image_from_url(image_url)
|
|
|
|
# 이미지에서 텍스트 인식
|
|
detected_texts = detect_text(image)
|
|
print("Detected texts:", detected_texts)
|
|
|
|
# 처리된 이미지 경로 설정
|
|
img_name = "output"
|
|
thumb_image_path = f'img/{img_name}_thumbnail.jpg'
|
|
translated_image_path = f'img/{img_name}_translated.jpg'
|
|
|
|
# 처리된 이미지 경로 설정
|
|
Thumb_image_path = f'img/{img_name}_Thumb.jpg'
|
|
processed_image_path = f'img/{img_name}_processed.jpg'
|
|
translated_image_path = f'img/{img_name}_translated.jpg'
|
|
inpainted_image_path = f'img/{img_name}_inpainted.jpg'
|
|
|
|
if convert_type == 'thumbnail':
|
|
# 썸네일 생성 로직
|
|
print("썸네일 생성 처리 시작")
|
|
# 크롭과 인페인팅 결정 및 처리 함수 호출
|
|
removal_img = process_image_based_on_text_area(image, detected_texts, processed_image_path, threshold = 0.1)
|
|
# 추가 처리: 이미지 회전 및 좌우 반전
|
|
# removal_img = flip_and_rotate_image(removal_img)
|
|
cv2.imwrite(Thumb_image_path, removal_img)
|
|
print(f"썸네일 이미지 생성완료: {Thumb_image_path}")
|
|
|
|
# 저장된 이미지 파일의 경로를 반환합니다.
|
|
return Thumb_image_path
|
|
|
|
elif convert_type == 'translate':
|
|
# 2단계: 인페이팅 기법으로 텍스트 제거
|
|
inpainted_image = inpaint_text(image, detected_texts)
|
|
cv2.imwrite(inpainted_image_path, inpainted_image)
|
|
# 이미지 번역 처리
|
|
print("이미지 번역 처리 시작")
|
|
# 번역된 텍스트 추출 및 삽입
|
|
texts_to_translate = [text for _, text, _, _, _ in detected_texts]
|
|
print(f"원본 텍스트: {texts_to_translate}")
|
|
translated_texts = translate_texts_translatepy(texts_to_translate)
|
|
print(f"번역 텍스트: {texts_to_translate}")
|
|
translated_image = insert_text(inpainted_image_path, translated_texts, detected_texts)
|
|
cv2.imwrite(translated_image_path, translated_image)
|
|
print(f"번역된 이미지 생성완료 : {translated_image_path}")
|
|
|
|
# 번역된 이미지를 HTML에 삽입 가능한 Base64 인코딩 문자열로 변환합니다.
|
|
pil_img = Image.fromarray(cv2.cvtColor(translated_image, cv2.COLOR_BGR2RGB))
|
|
buffered = BytesIO()
|
|
pil_img.save(buffered, format="JPEG")
|
|
img_str = base64.b64encode(buffered.getvalue()).decode()
|
|
# HTML 코드를 생성하여 반환합니다.
|
|
img_html = f'<img src="data:image/jpeg;base64,{img_str}" />'
|
|
return img_html
|
|
|
|
# URL로부터 이미지를 읽어오는 함수
|
|
def read_image_from_url(image_url):
|
|
resp = urllib.request.urlopen(image_url)
|
|
image = np.asarray(bytearray(resp.read()), dtype="uint8")
|
|
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
|
|
return image
|
|
|
|
# 외부에서 함수를 사용하는 예
|
|
# 이미지 URL과 원하는 변환 유형('thumbnail' 또는 'translate')을 인자로 넘깁니다.
|
|
result = image_trans('http://example.com/image.jpg', 'thumbnail')
|