"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

June 26, 2023

Background removal with Azure API

 In Azure Portal


Under cognitive services you have below options

Fetch Endpoint and Keys after deployment

Example code in link

#pip install azure-ai-vision
#https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/how-to/background-removal?tabs=python
import azure.ai.vision as sdk
service_options = sdk.VisionServiceOptions("https://NAMEYOURSERVICE.cognitiveservices.azure.com/",
"PROVIDEKEYS")
analysis_options = sdk.ImageAnalysisOptions()
analysis_options.segmentation_mode = sdk.ImageSegmentationMode.BACKGROUND_REMOVAL
vision_source = sdk.VisionSource(
url="https://learn.microsoft.com/azure/cognitive-services/computer-vision/media/quickstarts/presentation.png")
image_analyzer = sdk.ImageAnalyzer(service_options, vision_source, analysis_options)
result = image_analyzer.analyze()
if result.reason == sdk.ImageAnalysisResultReason.ANALYZED:
image_buffer = result.segmentation_result.image_buffer
print(" Segmentation result:")
print(" Output image buffer size (bytes) = {}".format(len(image_buffer)))
print(" Output image height = {}".format(result.segmentation_result.image_height))
print(" Output image width = {}".format(result.segmentation_result.image_width))
output_image_file = "output.png"
with open(output_image_file, 'wb') as binary_file:
binary_file.write(image_buffer)
print(" File {} written to disk".format(output_image_file));
else:
error_details = sdk.ImageAnalysisErrorDetails.from_result(result)
print(" Analysis failed.")
print(" Error reason: {}".format(error_details.reason))
print(" Error code: {}".format(error_details.error_code))
print(" Error message: {}".format(error_details.message))
print(" Did you set the computer vision endpoint and key?")
import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread('output.png', cv2.IMREAD_UNCHANGED)
cv2_imshow(img)



Keep Exploring!!!

No comments: