Spaces:
Paused
Paused
VINU NAYAK commited on
Create test_api.py
Browse files- test_api.py +66 -0
test_api.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def remove_background(image_path, output_path=None, api_url="http://localhost:7860/remove-bg"):
|
| 6 |
+
"""
|
| 7 |
+
Remove the background of an image using the API.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
image_path (str): Path to the input image
|
| 11 |
+
output_path (str, optional): Path to save the output image. If None, will use input_name_nobg.png
|
| 12 |
+
api_url (str, optional): URL of the API endpoint
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
str: Path to the saved output image
|
| 16 |
+
"""
|
| 17 |
+
# Check if image exists
|
| 18 |
+
if not os.path.exists(image_path):
|
| 19 |
+
print(f"Error: Image not found at {image_path}")
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
+
# Default output path if not specified
|
| 23 |
+
if output_path is None:
|
| 24 |
+
filename, _ = os.path.splitext(os.path.basename(image_path))
|
| 25 |
+
output_path = f"{filename}_nobg.png"
|
| 26 |
+
|
| 27 |
+
# Prepare the file for upload
|
| 28 |
+
files = {"file": open(image_path, "rb")}
|
| 29 |
+
|
| 30 |
+
print(f"Sending request to remove background from {image_path}...")
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# Send the request
|
| 34 |
+
response = requests.post(api_url, files=files)
|
| 35 |
+
|
| 36 |
+
# Check if request was successful
|
| 37 |
+
if response.status_code == 200:
|
| 38 |
+
# Save the output image
|
| 39 |
+
with open(output_path, "wb") as f:
|
| 40 |
+
f.write(response.content)
|
| 41 |
+
print(f"Background removed successfully! Saved to {output_path}")
|
| 42 |
+
return output_path
|
| 43 |
+
else:
|
| 44 |
+
print(f"Error: API returned status code {response.status_code}")
|
| 45 |
+
print(response.text)
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error: {str(e)}")
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
finally:
|
| 53 |
+
# Close the file
|
| 54 |
+
files["file"].close()
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
# Check if image path is provided as command line argument
|
| 58 |
+
if len(sys.argv) < 2:
|
| 59 |
+
print("Usage: python test_api.py <image_path> [output_path] [api_url]")
|
| 60 |
+
sys.exit(1)
|
| 61 |
+
|
| 62 |
+
image_path = sys.argv[1]
|
| 63 |
+
output_path = sys.argv[2] if len(sys.argv) > 2 else None
|
| 64 |
+
api_url = sys.argv[3] if len(sys.argv) > 3 else "http://localhost:7860/remove-bg"
|
| 65 |
+
|
| 66 |
+
remove_background(image_path, output_path, api_url)
|