ekhatskevich commited on
Commit
72f5484
·
1 Parent(s): 7db0491

add examples

Browse files
example/bear1.png ADDED
example/bear2.png ADDED
example/bear3.png ADDED
example/example_call.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Example script for using the Transparent Background Remover Inference Endpoint.
3
+
4
+ This script:
5
+ - Loads three sample images (sample1.png, sample2.png, sample3.png)
6
+ - Encodes them as base64 data URIs
7
+ - Builds a JSON payload and sends a POST request to the endpoint
8
+ - Receives the processed images (base64-encoded) and saves them as PNG files
9
+
10
+ Requirements:
11
+ - Python 3.10+
12
+ - requests
13
+
14
+ Usage:
15
+ python example.py
16
+ """
17
+
18
+ import requests
19
+ import base64
20
+
21
+
22
+ def encode_image_to_base64(filename):
23
+ """
24
+ Encode an image file to a base64 data URI.
25
+
26
+ Args:
27
+ filename (str): Path to the image file.
28
+
29
+ Returns:
30
+ str: Base64-encoded string with data URI prefix.
31
+ """
32
+ with open(filename, "rb") as f:
33
+ image_bytes = f.read()
34
+ encoded = base64.b64encode(image_bytes).decode("utf-8")
35
+ # Assuming PNG format; adjust MIME type if using another format.
36
+ data_uri = f"data:image/png;base64,{encoded}"
37
+ return data_uri
38
+
39
+
40
+ def save_base64_to_file(data_uri, output_filename):
41
+ """
42
+ Decode a base64 data URI and save it as an image file.
43
+
44
+ Args:
45
+ data_uri (str): The data URI containing the base64-encoded image.
46
+ output_filename (str): The output filename to save the image.
47
+ """
48
+ header, encoded = data_uri.split(",", 1)
49
+ image_data = base64.b64decode(encoded)
50
+ with open(output_filename, "wb") as f:
51
+ f.write(image_data)
52
+ print(f"Saved {output_filename}")
53
+
54
+
55
+ def main():
56
+ # Replace with your actual endpoint URL and API token (if required)
57
+ endpoint_url = "https://endpoints.huggingface.co/your-endpoint-url"
58
+ headers = {
59
+ "Authorization": "Bearer YOUR_API_TOKEN",
60
+ "Content-Type": "application/json"
61
+ }
62
+
63
+ # Encode three sample images.
64
+ sample_images = [
65
+ encode_image_to_base64("bear1.png"),
66
+ encode_image_to_base64("bear2.png"),
67
+ encode_image_to_base64("bear3.png")
68
+ ]
69
+
70
+ # Build the payload according to the handler's expected format.
71
+ payload = {
72
+ "inputs": {
73
+ "images": sample_images,
74
+ "output_type": "rgba", # Choose "rgba", "map", "green", "blur", or "overlay"
75
+ "threshold": 0.1,
76
+ "reverse": False # Set to True if you want to remove the foreground instead
77
+ }
78
+ }
79
+
80
+ print("Sending request to endpoint...")
81
+ response = requests.post(endpoint_url, json=payload, headers=headers)
82
+
83
+ if response.status_code != 200:
84
+ print("Error:", response.status_code, response.text)
85
+ return
86
+
87
+ result = response.json()
88
+ print("Processing Times:\n", result.get("processing_times", "No timing info provided."))
89
+
90
+ # Save each processed image to a file.
91
+ images = result.get("images", [])
92
+ for idx, data_uri in enumerate(images):
93
+ save_base64_to_file(data_uri, f"output_{idx+1}.png")
94
+
95
+
96
+ if __name__ == "__main__":
97
+ main()