Meng Chen
commited on
Commit
·
1f0d721
1
Parent(s):
d48d98c
update handler
Browse files- elevator.jpg +0 -3
- handler.py +9 -1
- test.ipynb +0 -104
elevator.jpg
DELETED
Git LFS Details
|
handler.py
CHANGED
@@ -56,7 +56,9 @@ class EndpointHandler():
|
|
56 |
|
57 |
seg_image = Image.fromarray(segmentation_mask)
|
58 |
|
59 |
-
|
|
|
|
|
60 |
|
61 |
except Exception as e:
|
62 |
return [{"error": str(e)}]
|
@@ -66,6 +68,12 @@ class EndpointHandler():
|
|
66 |
"""Decodes a base64-encoded image into a PIL image."""
|
67 |
image_bytes = base64.b64decode(image_data)
|
68 |
return Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
def process_depth(self, image):
|
71 |
print("Processing depth")
|
|
|
56 |
|
57 |
seg_image = Image.fromarray(segmentation_mask)
|
58 |
|
59 |
+
seg_image_base64 = self.encode_image(seg_image)
|
60 |
+
|
61 |
+
return [{"seg_image": seg_image_base64}]
|
62 |
|
63 |
except Exception as e:
|
64 |
return [{"error": str(e)}]
|
|
|
68 |
"""Decodes a base64-encoded image into a PIL image."""
|
69 |
image_bytes = base64.b64decode(image_data)
|
70 |
return Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
71 |
+
|
72 |
+
def encode_image(self, image: Image.Image) -> str:
|
73 |
+
"""Encodes a PIL image to a base64 string."""
|
74 |
+
buffered = io.BytesIO()
|
75 |
+
image.save(buffered, format="PNG")
|
76 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
77 |
|
78 |
def process_depth(self, image):
|
79 |
print("Processing depth")
|
test.ipynb
DELETED
@@ -1,104 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "code",
|
5 |
-
"execution_count": 3,
|
6 |
-
"metadata": {},
|
7 |
-
"outputs": [
|
8 |
-
{
|
9 |
-
"name": "stderr",
|
10 |
-
"output_type": "stream",
|
11 |
-
"text": [
|
12 |
-
"Device set to use cpu\n"
|
13 |
-
]
|
14 |
-
},
|
15 |
-
{
|
16 |
-
"name": "stdout",
|
17 |
-
"output_type": "stream",
|
18 |
-
"text": [
|
19 |
-
"[\n",
|
20 |
-
" {\n",
|
21 |
-
" \"error\": \"Torch not compiled with CUDA enabled\"\n",
|
22 |
-
" }\n",
|
23 |
-
"]\n"
|
24 |
-
]
|
25 |
-
}
|
26 |
-
],
|
27 |
-
"source": [
|
28 |
-
"import base64\n",
|
29 |
-
"import json\n",
|
30 |
-
"import io\n",
|
31 |
-
"from PIL import Image\n",
|
32 |
-
"import numpy as np\n",
|
33 |
-
"import matplotlib.pyplot as plt\n",
|
34 |
-
"from handler import EndpointHandler \n",
|
35 |
-
"\n",
|
36 |
-
"def encode_image(image_path):\n",
|
37 |
-
" \"\"\"Encodes an image to base64.\"\"\"\n",
|
38 |
-
" with open(image_path, \"rb\") as image_file:\n",
|
39 |
-
" return base64.b64encode(image_file.read()).decode(\"utf-8\")\n",
|
40 |
-
"\n",
|
41 |
-
"def visualize_mask(mask):\n",
|
42 |
-
" \"\"\"Visualizes the segmentation mask using matplotlib.\"\"\"\n",
|
43 |
-
" mask = np.array(mask)\n",
|
44 |
-
" plt.imshow(mask, cmap=\"jet\", alpha=0.6)\n",
|
45 |
-
" plt.colorbar()\n",
|
46 |
-
" plt.title(\"Segmentation Mask\")\n",
|
47 |
-
" plt.show()\n",
|
48 |
-
"\n",
|
49 |
-
"if __name__ == \"__main__\":\n",
|
50 |
-
" # Initialize handler\n",
|
51 |
-
" handler = EndpointHandler()\n",
|
52 |
-
"\n",
|
53 |
-
" # Provide path to a test image\n",
|
54 |
-
" image_path = \"elevator.jpg\" # Replace with a valid image path\n",
|
55 |
-
"\n",
|
56 |
-
" # Encode image as base64\n",
|
57 |
-
" encoded_image = encode_image(image_path)\n",
|
58 |
-
"\n",
|
59 |
-
" # Define test input\n",
|
60 |
-
" test_input = {\n",
|
61 |
-
" \"image\": encoded_image,\n",
|
62 |
-
" \"text\": \"elevator\"\n",
|
63 |
-
" }\n",
|
64 |
-
"\n",
|
65 |
-
" # Run handler\n",
|
66 |
-
" result = handler(test_input)\n",
|
67 |
-
"\n",
|
68 |
-
" # Print and visualize the result\n",
|
69 |
-
" print(json.dumps(result, indent=2))\n",
|
70 |
-
"\n",
|
71 |
-
" if \"segmentation_mask\" in result[0]:\n",
|
72 |
-
" visualize_mask(result[0][\"segmentation_mask\"])"
|
73 |
-
]
|
74 |
-
},
|
75 |
-
{
|
76 |
-
"cell_type": "code",
|
77 |
-
"execution_count": null,
|
78 |
-
"metadata": {},
|
79 |
-
"outputs": [],
|
80 |
-
"source": []
|
81 |
-
}
|
82 |
-
],
|
83 |
-
"metadata": {
|
84 |
-
"kernelspec": {
|
85 |
-
"display_name": "a11y-agent",
|
86 |
-
"language": "python",
|
87 |
-
"name": "python3"
|
88 |
-
},
|
89 |
-
"language_info": {
|
90 |
-
"codemirror_mode": {
|
91 |
-
"name": "ipython",
|
92 |
-
"version": 3
|
93 |
-
},
|
94 |
-
"file_extension": ".py",
|
95 |
-
"mimetype": "text/x-python",
|
96 |
-
"name": "python",
|
97 |
-
"nbconvert_exporter": "python",
|
98 |
-
"pygments_lexer": "ipython3",
|
99 |
-
"version": "3.11.10"
|
100 |
-
}
|
101 |
-
},
|
102 |
-
"nbformat": 4,
|
103 |
-
"nbformat_minor": 2
|
104 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|