import requests import json import base64 import os import time API_URL = "http://192.168.0.150:7000/translate_image" # API_URL = "http://127.0.0.1:7000/translate_image" # 이미지 파일을 base64로 변환하는 함수 def image_to_base64(image_path): """이미지 파일을 base64 문자열로 변환""" if not os.path.exists(image_path): raise FileNotFoundError(f"이미지 파일을 찾을 수 없습니다: {image_path}") with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()).decode('utf-8') return encoded_string # base64 데이터를 이미지 파일로 저장하는 함수 def base64_to_image(base64_data, output_path): """base64 문자열을 이미지 파일로 저장""" try: image_data = base64.b64decode(base64_data) with open(output_path, "wb") as image_file: image_file.write(image_data) return True except Exception as e: print(f"이미지 저장 중 오류: {e}") return False # 이미지 파일 경로 image_path = "d:/py/IT_Server/modules/img/6.jpg" print("=== 이미지 번역 API 테스트 시작 ===") start_total_time = time.time() # 이미지를 base64로 변환 print("\n1. 이미지 base64 변환 중...") start_encode_time = time.time() try: image_base64 = image_to_base64(image_path) encode_time = time.time() - start_encode_time print(f" ✓ 이미지 파일 '{image_path}' 를 base64로 변환 완료") print(f" ✓ Base64 길이: {len(image_base64):,} 문자") print(f" ✓ 인코딩 시간: {encode_time:.3f}초") except FileNotFoundError as e: print(f" ✗ 오류: {e}") exit(1) payload = { "image_data": image_base64, # 필드명을 image_data로 수정 "file_prefix": "test", "toggle_states": {"ocr": True}, "unwanted_texts": {"크리스탈": "크리미"}, "watermark_text": "테스트 워터마크", "watermark_opacity": 0.5, "watermark_font_size": 32 } headers = {"Content-Type": "application/json"} print(f"\n2. API 요청 전송 중... (URL: {API_URL})") start_api_time = time.time() response = requests.post(API_URL, data=json.dumps(payload), headers=headers) api_time = time.time() - start_api_time total_time = time.time() - start_total_time print(f"\n=== 처리 결과 ===") print(f"상태 코드: {response.status_code}") print(f"API 처리 시간: {api_time:.3f}초") print(f"전체 처리 시간: {total_time:.3f}초") try: response_data = response.json() print(f"\n응답 내용:") print(json.dumps(response_data, indent=2, ensure_ascii=False)) # 성공적으로 처리된 경우 추가 정보 출력 if response.status_code == 200: print(f"\n=== 성능 요약 ===") print(f"• Base64 인코딩: {encode_time:.3f}초") print(f"• API 서버 처리: {api_time:.3f}초") print(f"• 전체 소요 시간: {total_time:.3f}초") if api_time > 10: print("⚠️ API 처리 시간이 10초를 초과했습니다.") elif api_time > 5: print("⚠️ API 처리 시간이 5초를 초과했습니다.") else: print("✓ 처리 시간이 양호합니다.") # 결과 이미지를 파일로 저장 output_path = "d:/py/IT_Server/modules/translated_result.png" if base64_to_image(response_data["result"], output_path): print(f"처리된 이미지가 저장되었습니다: {output_path}") else: print("이미지 저장에 실패했습니다.") except json.JSONDecodeError: print(f"\nJSON 응답이 아닙니다:") print(response.text)