Find a similar image among saved images from blobstorage with Cognitive services - Stack Overflow

I have some set of images in my blob storage. I have a UI where I can upload an image, if I crop one of

I have some set of images in my blob storage. I have a UI where I can upload an image, if I crop one of my saved image and upload it as new, I need to retrieve the original image of that. Is there any service available for this feature.

I tried with Custom Vision API, but it is returning only probability of the image category and tagname. but I also want the original image of the cropped one.

I have some set of images in my blob storage. I have a UI where I can upload an image, if I crop one of my saved image and upload it as new, I need to retrieve the original image of that. Is there any service available for this feature.

I tried with Custom Vision API, but it is returning only probability of the image category and tagname. but I also want the original image of the cropped one.

Share Improve this question asked Nov 19, 2024 at 1:40 AjayAjay 11 bronze badge 2
  • Currently, there is no out-of-the-box Azure service specifically designed to track relationships between cropped images and their originals in Azure Blob Storage. However, you can implement this functionality using a combination of metadata and custom logic. – Venkatesan Commented Nov 19, 2024 at 6:01
  • Check if below provided solution works for you? Let me know if I can be helpful here anyway with further input? – Venkatesan Commented Nov 21, 2024 at 5:12
Add a comment  | 

1 Answer 1

Reset to default 0

Find a similar image among saved images from blobstorage with Cognitive services.

As of now, there is no out-of-the-box Azure service specifically designed to track relationships between cropped images and their originals in Azure Blob Storage. However, you can implement this functionality using a combination of metadata and custom logic.

You can use the below code that compare the cropped image original image using metadata in Azure python sdk.

Code:

import os
from azure.storage.blob import BlobServiceClient
from PIL import Image
from io import BytesIO

connection_string = "xxx"
container_name = "result"

blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)

def upload_image_with_metadata(blob_name, image_path):
    blob_client = container_client.get_blob_client(blob_name)
    
    with open(image_path, "rb") as data:
        blob_client.upload_blob(data, overwrite=True, blob_type="BlockBlob")
    
    blob_metadata = blob_client.get_blob_properties().metadata
    more_blob_metadata = {'docType': 'image', 'docCategory': 'reference'}
    blob_metadata.update(more_blob_metadata)
    blob_client.set_blob_metadata(metadata=blob_metadata)
    print(f"Image {blob_name} uploaded with metadata: {blob_metadata}")
    
def crop_and_upload_image(original_image_blob, crop_box, cropped_image_name):

    original_blob_client = container_client.get_blob_client(original_image_blob)
    original_image_data = original_blob_client.download_blob().readall()
    
    original_image = Image.open(BytesIO(original_image_data))
    cropped_image = original_image.crop(crop_box)
    cropped_image_io = BytesIO()
    cropped_image.save(cropped_image_io, format="JPEG")
    cropped_image_io.seek(0)
    metadata = {
        "original_image": original_image_blob
    }
    blob_client = container_client.get_blob_client(cropped_image_name)
    blob_client.upload_blob(cropped_image_io, overwrite=True, blob_type="BlockBlob")
    
    blob_client.set_blob_metadata(metadata)
    print(f"Cropped image {cropped_image_name} uploaded with metadata: {metadata}")

def get_original_image(cropped_image_blob):
    blob_client = container_client.get_blob_client(cropped_image_blob)
    
    metadata = blob_client.get_blob_properties().metadata
    original_image_blob = metadata.get("original_image")
    
    if original_image_blob:
        print(f"Original image for {cropped_image_blob} is: {original_image_blob}")
        
        original_blob_client = container_client.get_blob_client(original_image_blob)
        original_image_data = original_blob_client.download_blob().readall()
        
        return original_image_data
    else:
        print(f"No original image metadata found for {cropped_image_blob}")
        return None

if __name__ == "__main__":
    original_image_name = "original_image.jpg"
    original_image_path = r"test1.jpg"
    upload_image_with_metadata(original_image_name, original_image_path)
    
    crop_box = (0, 0, 100, 100)  # (left, upper, right, lower)
    cropped_image_name = "cropped_image.jpg"
    crop_and_upload_image(original_image_name, crop_box, cropped_image_name)
    
    original_image_data = get_original_image(cropped_image_name)
    
    if original_image_data:
        with open("retrieved_original_image.jpg", "wb") as f:
            f.write(original_image_data)
        print("Original image saved successfully.")

The above code will to upload an image to Azure Blob Storage with metadata, crop the image and save it back to Blob Storage with metadata linking to the original, and retrieve the original image by querying the metadata of a cropped image. It uses the Azure Blob Storage Python SDK, the PIL library for image processing, and the io library for working with bytes.

Output:

Image original_image.jpg uploaded with metadata: {'docType': 'image', 'docCategory': 'reference'}
Cropped image cropped_image.jpg uploaded with metadata: {'original_image': 'original_image.jpg'}
Original image for cropped_image.jpg is: original_image.jpg
Original image saved successfully.

Portal:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745586928a4634598.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信