Spaces:
Runtime error
Runtime error
Commit
·
71a7417
1
Parent(s):
36e56cd
Dict return
Browse files- .gradio/certificate.pem +31 -0
- gradio.ipynb +215 -55
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
| 6 |
+
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
|
| 7 |
+
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
|
| 8 |
+
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
|
| 9 |
+
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
|
| 10 |
+
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW
|
| 11 |
+
T8KOEUt+zwvo/7V3LvSye0rg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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
{
|
| 13 |
"cell_type": "code",
|
| 14 |
"execution_count": null,
|
| 15 |
"metadata": {},
|
| 16 |
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
{
|
| 18 |
"name": "stderr",
|
| 19 |
"output_type": "stream",
|
| 20 |
"text": [
|
| 21 |
-
"
|
| 22 |
-
"
|
|
|
|
| 23 |
]
|
| 24 |
},
|
| 25 |
{
|
| 26 |
"name": "stdout",
|
| 27 |
"output_type": "stream",
|
| 28 |
"text": [
|
| 29 |
-
"
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"\n",
|
| 32 |
-
"
|
|
|
|
| 33 |
]
|
| 34 |
},
|
| 35 |
{
|
| 36 |
-
"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"<IPython.core.display.HTML object>"
|
| 42 |
-
]
|
| 43 |
-
},
|
| 44 |
-
"metadata": {},
|
| 45 |
-
"output_type": "display_data"
|
| 46 |
},
|
| 47 |
{
|
| 48 |
"name": "stderr",
|
| 49 |
"output_type": "stream",
|
| 50 |
"text": [
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
" File \"e:\\anaconda\\envs\\text-behind-image-env\\Lib\\site-packages\\gradio\\route_utils.py\", line 323, in call_process_api\n",
|
| 56 |
-
" output = await app.get_blocks().process_api(\n",
|
| 57 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 58 |
-
" File \"e:\\anaconda\\envs\\text-behind-image-env\\Lib\\site-packages\\gradio\\blocks.py\", line 2028, in process_api\n",
|
| 59 |
-
" data = await self.postprocess_data(block_fn, result[\"prediction\"], state)\n",
|
| 60 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 61 |
-
" File \"e:\\anaconda\\envs\\text-behind-image-env\\Lib\\site-packages\\gradio\\blocks.py\", line 1784, in postprocess_data\n",
|
| 62 |
-
" self.validate_outputs(block_fn, predictions) # type: ignore\n",
|
| 63 |
-
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 64 |
-
" File \"e:\\anaconda\\envs\\text-behind-image-env\\Lib\\site-packages\\gradio\\blocks.py\", line 1739, in validate_outputs\n",
|
| 65 |
-
" raise ValueError(\n",
|
| 66 |
-
"ValueError: A function (segment_marker) didn't return enough output values (needed: 2, returned: 1).\n",
|
| 67 |
-
" Output components:\n",
|
| 68 |
-
" [image, image]\n",
|
| 69 |
-
" Output values returned:\n",
|
| 70 |
-
" [\"Invalid marker coordinates format. Ensure it's valid JSON.\"]\n"
|
| 71 |
]
|
| 72 |
},
|
| 73 |
{
|
|
@@ -81,9 +182,24 @@
|
|
| 81 |
"name": "stderr",
|
| 82 |
"output_type": "stream",
|
| 83 |
"text": [
|
| 84 |
-
"\n",
|
| 85 |
-
"
|
| 86 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
]
|
| 88 |
}
|
| 89 |
],
|
|
@@ -101,12 +217,6 @@
|
|
| 101 |
"\n",
|
| 102 |
"FAST_SAM = loadModel()\n",
|
| 103 |
"\n",
|
| 104 |
-
"# Helper function to convert base64 to PIL image\n",
|
| 105 |
-
"def base64_to_image(base64_str):\n",
|
| 106 |
-
" image_data = base64.b64decode(base64_str)\n",
|
| 107 |
-
" image = Image.open(BytesIO(image_data))\n",
|
| 108 |
-
" return image\n",
|
| 109 |
-
"\n",
|
| 110 |
"# Main processing function\n",
|
| 111 |
"def segment_marker(img_rgb: Image.Image, marker_coordinates: str):\n",
|
| 112 |
" # Parse marker coordinates from JSON string\n",
|
|
@@ -130,15 +240,15 @@
|
|
| 130 |
" bg_only_removed_img = removeOnlyBg(img_rgb, masks[0])\n",
|
| 131 |
" img_base64_only_bg = convertToBuffer(bg_only_removed_img)\n",
|
| 132 |
"\n",
|
| 133 |
-
" #
|
| 134 |
-
"
|
| 135 |
-
"
|
| 136 |
-
"\n",
|
| 137 |
-
"
|
| 138 |
"\n",
|
| 139 |
" except Exception as e:\n",
|
| 140 |
" print(f\"An error occurred: {str(e)}\")\n",
|
| 141 |
-
" return \"An error occurred while processing the image.\"
|
| 142 |
"\n",
|
| 143 |
"# Set up the Gradio interface\n",
|
| 144 |
"iface = gr.Interface(\n",
|
|
@@ -147,10 +257,7 @@
|
|
| 147 |
" gr.Image(type=\"pil\", label=\"Upload Image\"),\n",
|
| 148 |
" gr.Textbox(label=\"Markers Coordinates (JSON format)\")\n",
|
| 149 |
" ],\n",
|
| 150 |
-
" outputs
|
| 151 |
-
" gr.Image(type=\"pil\", label=\"Background Removed with Segmentation\"),\n",
|
| 152 |
-
" gr.Image(type=\"pil\", label=\"Only Background Removed\")\n",
|
| 153 |
-
" ],\n",
|
| 154 |
" title=\"Image Segmentation with Background Removal\",\n",
|
| 155 |
" description=\"Upload an image and JSON-formatted marker coordinates to perform image segmentation and background removal.\"\n",
|
| 156 |
")\n",
|
|
@@ -160,6 +267,59 @@
|
|
| 160 |
" iface.launch(share=True)\n"
|
| 161 |
]
|
| 162 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
{
|
| 164 |
"cell_type": "code",
|
| 165 |
"execution_count": null,
|
|
@@ -170,7 +330,7 @@
|
|
| 170 |
],
|
| 171 |
"metadata": {
|
| 172 |
"kernelspec": {
|
| 173 |
-
"display_name": "
|
| 174 |
"language": "python",
|
| 175 |
"name": "python3"
|
| 176 |
},
|
|
|
|
| 9 |
"# res = segment_marker(Image.open('notebook/lion.jpg'), '[{\"flag_\":1, \"x_\": 3760.689914766355, \"y_\": 2243.232589377525}]')"
|
| 10 |
]
|
| 11 |
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": null,
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [],
|
| 17 |
+
"source": [
|
| 18 |
+
"[{\"flag_\":1, \"x_\": 3760.689914766355, \"y_\": 2243.232589377525}]"
|
| 19 |
+
]
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"cell_type": "markdown",
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"source": [
|
| 25 |
+
"# Gradio APP"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": null,
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [],
|
| 33 |
+
"source": [
|
| 34 |
+
"import base64\n",
|
| 35 |
+
"from io import BytesIO\n",
|
| 36 |
+
"import gradio as gr\n",
|
| 37 |
+
"from PIL import Image\n",
|
| 38 |
+
"import json\n",
|
| 39 |
+
"\n",
|
| 40 |
+
"from tools.tools import convertToBuffer\n",
|
| 41 |
+
"from visualize.visualize import removeBgFromSegmentImage, removeOnlyBg\n",
|
| 42 |
+
"from models.model import getMask, loadModel\n",
|
| 43 |
+
"from models.preprocess import preprocess\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"FAST_SAM = loadModel()\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"# Helper function to convert base64 to PIL image\n",
|
| 48 |
+
"def base64_to_image(base64_str):\n",
|
| 49 |
+
" image_data = base64.b64decode(base64_str)\n",
|
| 50 |
+
" image = Image.open(BytesIO(image_data))\n",
|
| 51 |
+
" return image\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"# Main processing function\n",
|
| 54 |
+
"def segment_marker(img_rgb: Image.Image, marker_coordinates: str):\n",
|
| 55 |
+
" # Parse marker coordinates from JSON string\n",
|
| 56 |
+
" try:\n",
|
| 57 |
+
" marker_coordinates = json.loads(marker_coordinates)\n",
|
| 58 |
+
" except json.JSONDecodeError:\n",
|
| 59 |
+
" return \"Invalid marker coordinates format. Ensure it's valid JSON.\"\n",
|
| 60 |
+
"\n",
|
| 61 |
+
" try:\n",
|
| 62 |
+
" # Process marker points and labels\n",
|
| 63 |
+
" input_points, input_labels = preprocess(marker_coordinates)\n",
|
| 64 |
+
"\n",
|
| 65 |
+
" print(f\"Processing image with {len(input_points)} marker points...\")\n",
|
| 66 |
+
" # Get mask for segmentation\n",
|
| 67 |
+
" masks = getMask(img_rgb, FAST_SAM, input_points, input_labels)\n",
|
| 68 |
+
"\n",
|
| 69 |
+
" # Generate the segmented images\n",
|
| 70 |
+
" bg_removed_segmented_img = removeBgFromSegmentImage(img_rgb, masks[0])\n",
|
| 71 |
+
" img_base64_bg_segmented = convertToBuffer(bg_removed_segmented_img)\n",
|
| 72 |
+
"\n",
|
| 73 |
+
" bg_only_removed_img = removeOnlyBg(img_rgb, masks[0])\n",
|
| 74 |
+
" img_base64_only_bg = convertToBuffer(bg_only_removed_img)\n",
|
| 75 |
+
"\n",
|
| 76 |
+
" # Convert base64 strings to PIL images for Gradio\n",
|
| 77 |
+
" img_bg_segmented = base64_to_image(img_base64_bg_segmented)\n",
|
| 78 |
+
" img_bg_only_removed = base64_to_image(img_base64_only_bg)\n",
|
| 79 |
+
"\n",
|
| 80 |
+
" return img_bg_segmented, img_bg_only_removed # Return as two separate images\n",
|
| 81 |
+
"\n",
|
| 82 |
+
" except Exception as e:\n",
|
| 83 |
+
" print(f\"An error occurred: {str(e)}\")\n",
|
| 84 |
+
" return \"An error occurred while processing the image.\", None\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"# Set up the Gradio interface\n",
|
| 87 |
+
"iface = gr.Interface(\n",
|
| 88 |
+
" fn=segment_marker,\n",
|
| 89 |
+
" inputs=[\n",
|
| 90 |
+
" gr.Image(type=\"pil\", label=\"Upload Image\"),\n",
|
| 91 |
+
" gr.Textbox(label=\"Markers Coordinates (JSON format)\")\n",
|
| 92 |
+
" ],\n",
|
| 93 |
+
" outputs=[\n",
|
| 94 |
+
" gr.Image(type=\"pil\", label=\"Background Removed with Segmentation\"),\n",
|
| 95 |
+
" gr.Image(type=\"pil\", label=\"Only Background Removed\")\n",
|
| 96 |
+
" ],\n",
|
| 97 |
+
" title=\"Image Segmentation with Background Removal\",\n",
|
| 98 |
+
" description=\"Upload an image and JSON-formatted marker coordinates to perform image segmentation and background removal.\"\n",
|
| 99 |
+
")\n",
|
| 100 |
+
"\n",
|
| 101 |
+
"# Run the Gradio app\n",
|
| 102 |
+
"if __name__ == \"__main__\":\n",
|
| 103 |
+
" iface.launch(share=True)\n"
|
| 104 |
+
]
|
| 105 |
+
},
|
| 106 |
+
{
|
| 107 |
+
"cell_type": "code",
|
| 108 |
+
"execution_count": null,
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"outputs": [],
|
| 111 |
+
"source": []
|
| 112 |
+
},
|
| 113 |
+
{
|
| 114 |
+
"cell_type": "markdown",
|
| 115 |
+
"metadata": {},
|
| 116 |
+
"source": [
|
| 117 |
+
"# APP 2"
|
| 118 |
+
]
|
| 119 |
+
},
|
| 120 |
{
|
| 121 |
"cell_type": "code",
|
| 122 |
"execution_count": null,
|
| 123 |
"metadata": {},
|
| 124 |
"outputs": [
|
| 125 |
+
{
|
| 126 |
+
"name": "stdout",
|
| 127 |
+
"output_type": "stream",
|
| 128 |
+
"text": [
|
| 129 |
+
"Processing image with 1 marker points...\n"
|
| 130 |
+
]
|
| 131 |
+
},
|
| 132 |
{
|
| 133 |
"name": "stderr",
|
| 134 |
"output_type": "stream",
|
| 135 |
"text": [
|
| 136 |
+
"\n",
|
| 137 |
+
"0: 736x1024 17 objects, 5068.7ms\n",
|
| 138 |
+
"Speed: 1549.8ms preprocess, 5068.7ms inference, 5802.7ms postprocess per image at shape (1, 3, 1024, 1024)\n"
|
| 139 |
]
|
| 140 |
},
|
| 141 |
{
|
| 142 |
"name": "stdout",
|
| 143 |
"output_type": "stream",
|
| 144 |
"text": [
|
| 145 |
+
"Processing image with 1 marker points...\n"
|
| 146 |
+
]
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"name": "stderr",
|
| 150 |
+
"output_type": "stream",
|
| 151 |
+
"text": [
|
| 152 |
"\n",
|
| 153 |
+
"0: 736x1024 17 objects, 4238.3ms\n",
|
| 154 |
+
"Speed: 541.0ms preprocess, 4238.3ms inference, 3713.8ms postprocess per image at shape (1, 3, 1024, 1024)\n"
|
| 155 |
]
|
| 156 |
},
|
| 157 |
{
|
| 158 |
+
"name": "stdout",
|
| 159 |
+
"output_type": "stream",
|
| 160 |
+
"text": [
|
| 161 |
+
"Processing image with 1 marker points...\n"
|
| 162 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
},
|
| 164 |
{
|
| 165 |
"name": "stderr",
|
| 166 |
"output_type": "stream",
|
| 167 |
"text": [
|
| 168 |
+
"\n",
|
| 169 |
+
"0: 736x1024 17 objects, 3183.0ms\n",
|
| 170 |
+
"Speed: 475.6ms preprocess, 3183.0ms inference, 2650.1ms postprocess per image at shape (1, 3, 1024, 1024)\n",
|
| 171 |
+
"\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
]
|
| 173 |
},
|
| 174 |
{
|
|
|
|
| 182 |
"name": "stderr",
|
| 183 |
"output_type": "stream",
|
| 184 |
"text": [
|
| 185 |
+
"0: 736x1024 17 objects, 3026.0ms\n",
|
| 186 |
+
"Speed: 74.9ms preprocess, 3026.0ms inference, 2444.8ms postprocess per image at shape (1, 3, 1024, 1024)\n",
|
| 187 |
+
"\n"
|
| 188 |
+
]
|
| 189 |
+
},
|
| 190 |
+
{
|
| 191 |
+
"name": "stdout",
|
| 192 |
+
"output_type": "stream",
|
| 193 |
+
"text": [
|
| 194 |
+
"Processing image with 2 marker points...\n"
|
| 195 |
+
]
|
| 196 |
+
},
|
| 197 |
+
{
|
| 198 |
+
"name": "stderr",
|
| 199 |
+
"output_type": "stream",
|
| 200 |
+
"text": [
|
| 201 |
+
"0: 736x1024 17 objects, 2810.2ms\n",
|
| 202 |
+
"Speed: 12.6ms preprocess, 2810.2ms inference, 1752.6ms postprocess per image at shape (1, 3, 1024, 1024)\n"
|
| 203 |
]
|
| 204 |
}
|
| 205 |
],
|
|
|
|
| 217 |
"\n",
|
| 218 |
"FAST_SAM = loadModel()\n",
|
| 219 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
"# Main processing function\n",
|
| 221 |
"def segment_marker(img_rgb: Image.Image, marker_coordinates: str):\n",
|
| 222 |
" # Parse marker coordinates from JSON string\n",
|
|
|
|
| 240 |
" bg_only_removed_img = removeOnlyBg(img_rgb, masks[0])\n",
|
| 241 |
" img_base64_only_bg = convertToBuffer(bg_only_removed_img)\n",
|
| 242 |
"\n",
|
| 243 |
+
" # Return the images in a dictionary format as base64 strings\n",
|
| 244 |
+
" return {\n",
|
| 245 |
+
" 'bg_removed_segmented_img': f'data:image/png;base64,{img_base64_bg_segmented}',\n",
|
| 246 |
+
" 'bg_only_removed_segmented_img': f'data:image/png;base64,{img_base64_only_bg}'\n",
|
| 247 |
+
" }\n",
|
| 248 |
"\n",
|
| 249 |
" except Exception as e:\n",
|
| 250 |
" print(f\"An error occurred: {str(e)}\")\n",
|
| 251 |
+
" return {'error': \"An error occurred while processing the image.\"}\n",
|
| 252 |
"\n",
|
| 253 |
"# Set up the Gradio interface\n",
|
| 254 |
"iface = gr.Interface(\n",
|
|
|
|
| 257 |
" gr.Image(type=\"pil\", label=\"Upload Image\"),\n",
|
| 258 |
" gr.Textbox(label=\"Markers Coordinates (JSON format)\")\n",
|
| 259 |
" ],\n",
|
| 260 |
+
" outputs=\"json\", # Set output to JSON format to return the dictionary\n",
|
|
|
|
|
|
|
|
|
|
| 261 |
" title=\"Image Segmentation with Background Removal\",\n",
|
| 262 |
" description=\"Upload an image and JSON-formatted marker coordinates to perform image segmentation and background removal.\"\n",
|
| 263 |
")\n",
|
|
|
|
| 267 |
" iface.launch(share=True)\n"
|
| 268 |
]
|
| 269 |
},
|
| 270 |
+
{
|
| 271 |
+
"cell_type": "markdown",
|
| 272 |
+
"metadata": {},
|
| 273 |
+
"source": [
|
| 274 |
+
"# Predict"
|
| 275 |
+
]
|
| 276 |
+
},
|
| 277 |
+
{
|
| 278 |
+
"cell_type": "code",
|
| 279 |
+
"execution_count": null,
|
| 280 |
+
"metadata": {},
|
| 281 |
+
"outputs": [],
|
| 282 |
+
"source": [
|
| 283 |
+
"from gradio_client import Client, handle_file"
|
| 284 |
+
]
|
| 285 |
+
},
|
| 286 |
+
{
|
| 287 |
+
"cell_type": "code",
|
| 288 |
+
"execution_count": null,
|
| 289 |
+
"metadata": {},
|
| 290 |
+
"outputs": [],
|
| 291 |
+
"source": [
|
| 292 |
+
"client = Client(\"Tharuneshwar/Text-Behind-Image\")"
|
| 293 |
+
]
|
| 294 |
+
},
|
| 295 |
+
{
|
| 296 |
+
"cell_type": "code",
|
| 297 |
+
"execution_count": null,
|
| 298 |
+
"metadata": {},
|
| 299 |
+
"outputs": [],
|
| 300 |
+
"source": [
|
| 301 |
+
"result = client.predict(\n",
|
| 302 |
+
"\t\timg_rgb=handle_file('../notebook/lion.jpg'),\n",
|
| 303 |
+
"\t\tmarker_coordinates='[{\"flag_\":1, \"x_\": 3760.689914766355, \"y_\": 2243.232589377525}]',\n",
|
| 304 |
+
"\t\tapi_name=\"/predict\"\n",
|
| 305 |
+
")\n",
|
| 306 |
+
"result"
|
| 307 |
+
]
|
| 308 |
+
},
|
| 309 |
+
{
|
| 310 |
+
"cell_type": "code",
|
| 311 |
+
"execution_count": null,
|
| 312 |
+
"metadata": {},
|
| 313 |
+
"outputs": [],
|
| 314 |
+
"source": []
|
| 315 |
+
},
|
| 316 |
+
{
|
| 317 |
+
"cell_type": "code",
|
| 318 |
+
"execution_count": null,
|
| 319 |
+
"metadata": {},
|
| 320 |
+
"outputs": [],
|
| 321 |
+
"source": []
|
| 322 |
+
},
|
| 323 |
{
|
| 324 |
"cell_type": "code",
|
| 325 |
"execution_count": null,
|
|
|
|
| 330 |
],
|
| 331 |
"metadata": {
|
| 332 |
"kernelspec": {
|
| 333 |
+
"display_name": "tbi-gradio-env",
|
| 334 |
"language": "python",
|
| 335 |
"name": "python3"
|
| 336 |
},
|