-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
42 lines (34 loc) · 1.58 KB
/
test.py
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
import requests
import os
# Set your API key (make sure it's the same as the one used in your FastAPI server)
API_KEY = os.getenv("API_KEY")
def annotate_image(file_path):
# Define the URL with the API key as a query parameter
url = f'https://captcha-solver-api-529689135074.us-central1.run.app/process_image?api_key={API_KEY}'
# Extract the original file name without extension
base_name = os.path.basename(file_path)
name, ext = os.path.splitext(base_name)
# Open the image file and send it to the FastAPI server
with open(file_path, 'rb') as f:
files = {'file': f}
# Remove headers as we now pass the API key in the URL
response = requests.post(url, files=files)
# Check if the request was successful
if response.status_code == 200:
# Save the response (annotated image) as name_annotated.jpg
annotated_file_name = f"{name}_annotated.jpg"
with open(annotated_file_name, 'wb') as f:
f.write(response.content)
# Print success message with the new file name
print(f"Annotated image saved as {annotated_file_name}")
else:
# Print an error message if something went wrong
try:
# Attempt to parse JSON response if available
error_details = response.json()
except ValueError:
# Handle case where the response is not JSON (e.g., HTML error pages)
error_details = response.text
print(f"Error: {response.status_code}, {error_details}")
# Test the function with your image file path
annotate_image('test-images/1.png')