flask-ocr-api / test_client.py
vamsi1103's picture
Upload 26 files
0e157b6 verified
Raw History Blame Contribute Delete
1.36 kB
import requests
import sys
import json
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
def test_ocr(image_path):
url = "https://vamsi1103-flask-ocr-api.hf.space/ocr"
print(f"Testing OCR API with image: {image_path}")
try:
with open(image_path, "rb") as file:
response = requests.post(url, files={"file": file}, timeout=60)
if response.status_code == 200:
data = response.json()
with open("hf_api_response.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
print("Successfully received API Response! Saved to hf_api_response.json")
print(f"Date: {data.get('date')}")
print(f"Total items returned: {len(data.get('items', []))}")
for i, it in enumerate(data.get('items', [])[:15]):
print(f" {i+1}. {it.get('name')} -> {it.get('rate') or it.get('available_rates')} (text: {it.get('rate_text')})")
else:
print(f"Error {response.status_code}: {response.text}")
except Exception as e:
print(f"Failed to connect or process response: {e}")
if __name__ == "__main__":
if len(sys.argv) > 1:
test_ocr(sys.argv[1])
else:
print("Usage: python test_client.py <path_to_image>")