Last active
December 31, 2024 17:44
-
-
Save jamessdixon/c96f6683403bf204ee149990083d8840 to your computer and use it in GitHub Desktop.
pipe-remove-background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#https://learn.microsoft.com/en-us/azure/ai-services/computer-vision/how-to/call-analyze-image-40?pivots=programming-language-csharp\n", | |
"#https://learn.microsoft.com/en-us/azure/ai-services/computer-vision/how-to/background-removal\n", | |
"#https://github.com/danielgatis/rembg/blob/main/USAGE.md" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from azure.ai.vision.imageanalysis import ImageAnalysisClient\n", | |
"from azure.ai.vision.imageanalysis.models import VisualFeatures\n", | |
"from azure.core.credentials import AzureKeyCredential\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"endpoint = \"https://cv-XXX.cognitiveservices.azure.com/\"\n", | |
"credential = AzureKeyCredential(\"XXX\")\n", | |
"\n", | |
"image_file = 'truth_set/updated_2024-12-11T07_25_05_359727.jpg'\n", | |
"with open(image_file, \"rb\") as f:\n", | |
" image_data = f.read()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'modelVersion': '2023-10-01', 'captionResult': {'text': 'a white pipe with a black pipe', 'confidence': 0.5815051794052124}, 'metadata': {'width': 3024, 'height': 4032}, 'readResult': {'blocks': []}}\n" | |
] | |
} | |
], | |
"source": [ | |
"client = ImageAnalysisClient(endpoint=endpoint,credential=credential)\n", | |
"visual_features=[VisualFeatures.CAPTION, VisualFeatures.READ]\n", | |
"result = client.analyze(image_data=image_data,visual_features=visual_features,gender_neutral_caption=True,language=\"en\",)\n", | |
"print(result)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"client = ImageAnalysisClient(endpoint=endpoint,credential=credential)\n", | |
"visual_features=[VisualFeatures.OBJECTS]\n", | |
"result = client.analyze(image_data=image_data,visual_features=visual_features,gender_neutral_caption=True,language=\"en\",)\n", | |
"\n", | |
"if result.objects is not None:\n", | |
" for object in result.objects.list:\n", | |
" print(f\" '{object.tags[0].name}', {object.bounding_box}, Confidence: {object.tags[0].confidence:.4f}\")\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"#backgroundRemoval\n", | |
"# Background removal is only available through direct REST API calls. It is not available through the SDKs.\n", | |
"# Being Removed Jan 2025\n", | |
"\n", | |
"import requests\n", | |
"\n", | |
"endpoint = \"https://cv-XXX.cognitiveservices.azure.com/\"\n", | |
"api_version = \"2023-02-01-preview\"\n", | |
"mode = \"backgroundRemoval\"\n", | |
"key = \"XXX\"\n", | |
"url = \"{}computervision/imageanalysis:segment?api-version={}&mode={}\".format(endpoint, api_version, mode)\n", | |
"headers = {\"Ocp-Apim-Subscription-Key\": key,\"Content-Type\": \"application/octet-stream\"}\n", | |
"body = image_data\n", | |
"\n", | |
"response = requests.post(url, headers=headers, data=body)\n", | |
"image=response.content\n", | |
"with open(\"output/pipe_nobackground.png\", \"wb\") as file:\n", | |
" file.write(image)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Downloading data from 'https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx' to file 'C:\\Users\\dixonjames\\.u2net\\u2net.onnx'.\n", | |
"100%|########################################| 176M/176M [00:00<00:00, 174GB/s]\n" | |
] | |
} | |
], | |
"source": [ | |
"from rembg import remove\n", | |
"from PIL import Image\n", | |
"import io\n", | |
"\n", | |
"output_data = remove(image_data)\n", | |
"output_image_path = 'output/pipe_no_bg_rembg.png'\n", | |
"output_image = Image.open(io.BytesIO(output_data))\n", | |
"output_image.save(output_image_path)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "ModuleNotFoundError", | |
"evalue": "No module named 'rembg.session'", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", | |
"Cell \u001b[1;32mIn[16], line 5\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# To maintain a smooth operation of your models, install the open-source Florence 2 model and use its Region \u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# to segmentation feature, which allows for a similar background removal operation.\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mrembg\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m remove\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mrembg\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01msession\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m new_session\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mPIL\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Image\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mio\u001b[39;00m\n", | |
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'rembg.session'" | |
] | |
} | |
], | |
"source": [ | |
"# To maintain a smooth operation of your models, install the open-source Florence 2 model and use its Region \n", | |
"# to segmentation feature, which allows for a similar background removal operation.\n", | |
"#Note - could not find this session class in the SDK\n", | |
"\n", | |
"from rembg import remove\n", | |
"from rembg.session import new_session\n", | |
"from PIL import Image\n", | |
"import io\n", | |
"\n", | |
"session = new_session(model_name='florence2') # Replace 'u2netp' with 'florence2' if the model is renamed.\n", | |
"output_data = remove(image_data, session=session)\n", | |
"output_image_path = 'output/pipe_no_bg_florence2.png'\n", | |
"output_image = Image.open(io.BytesIO(output_data))\n", | |
"output_image.save(output_image_path)\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": ".venv", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.11.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment