Passing PIL cropped image as stream in Azure Computer Vision

Vedant Modi 1 Reputation point
2021-07-20T12:55:44.937+00:00

I need to pass a cropped image to the Computer Vision client in Azure, either as a stream or a url. I'm using PIL to crop the image based on some bounding box values. img1 stores the cropped image

img1 = img.crop((bbox[i][0], bbox[i][1], bbox[i][4], bbox[i][5]))
img1.show()
with open(img1.name, 'rb') as bg_image1:
     colour_analysis = computervision_client.analyze_image_in_stream(bg_image1, remote_image_features)

OR

img1 = img.crop((bbox[i][0], bbox[i][1], bbox[i][4], bbox[i][5]))
img1.show()
open_image = open(img1.name, "rb")
colour_analysis = computervision_client.analyze_image_in_stream(open_image, remote_image_features)

When I run the code above I get the error: raise AttributeError(name) AttributeError: name

Previously, when i uploaded the image from my files, I used this code which worked well:

with open('filepath\filename.jpg', 'rb') as bg_image:
    colour_analysis = computervision_client.analyze_image_in_stream(bg_image, remote_image_features)

How do I pass the cropped image to the Azure Client?

Azure Computer Vision
Azure Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
371 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ramr-msft 17,736 Reputation points
    2021-07-21T15:50:23.917+00:00

    @Vedant Modi Thanks for the question. Can you please share the image that you are trying and SDK version that you are trying.

    Please follow the below github sample to analyze a local image. See the ComputerVisionClient methods, such as AnalyzeImageInStreamAsync.

    https://github.com/Azure-Samples/cognitive-services-quickstart-code/blob/master/dotnet/ComputerVision/ImageAnalysisQuickstart.cs#L306


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.