TransWorker/ImageTransWorker/test_translate_api.py

80 lines
2.7 KiB
Python

import requests
import json
import base64
import os
import cv2
import numpy as np
API_URL = "http://127.0.0.1:7000/translate_image"
# 방법 1: 로컬 이미지 경로 사용 (기존 방식)
def test_with_path():
payload = {
"local_image_path": "/home/ckh08045/work/IT_Server/modules/img/6.jpg",
"file_prefix": "test_path",
"toggle_states": {"ocr": True},
"unwanted_texts": {"크리스탈": "크리미"},
"watermark_text": "테스트 워터마크",
"watermark_opacity": 0.5,
"watermark_font_size": 32
}
headers = {"Content-Type": "application/json"}
response = requests.post(API_URL, data=json.dumps(payload), headers=headers)
print("=== 경로 사용 테스트 ===")
print("응답 결과:")
print(response.status_code)
# print(response.json())
# 결과 이미지를 temp_image 폴더에 PNG로 저장
result = response.json()
# 예시: result["image"]에 base64 인코딩된 이미지가 있다고 가정
image_b64 = result.get("image")
if image_b64:
os.makedirs("temp_image", exist_ok=True)
image_bytes = base64.b64decode(image_b64)
nparr = np.frombuffer(image_bytes, np.uint8)
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img_np is not None:
out_path = os.path.join("temp_image", "result_path.png")
cv2.imwrite(out_path, img_np)
print(f"이미지 저장 완료: {out_path}")
else:
print("이미지 디코딩 실패")
else:
print("응답에 이미지 데이터가 없습니다.")
# 방법 2: base64 이미지 데이터 사용 (새로운 방식)
def test_with_base64():
# 이미지 파일을 base64로 인코딩
image_path = "/home/ckh08045/work/IT_Server/modules/img/6.jpg"
try:
with open(image_path, "rb") as f:
image_bytes = f.read()
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
except FileNotFoundError:
print(f"이미지 파일을 찾을 수 없습니다: {image_path}")
return
payload = {
"image_data": image_base64, # base64 데이터 사용
"file_prefix": "test_base64",
"toggle_states": {"ocr": True},
"unwanted_texts": {"크리스탈": "크리미"},
"watermark_text": "테스트 워터마크",
"watermark_opacity": 0.5,
"watermark_font_size": 32
}
headers = {"Content-Type": "application/json"}
response = requests.post(API_URL, data=json.dumps(payload), headers=headers)
print("\n=== base64 데이터 사용 테스트 ===")
print("응답 결과:")
print(response.status_code)
print(response.json())
if __name__ == "__main__":
test_with_path()
# test_with_base64()