【问题标题】:Processing created blob using EventGridEvent using python使用 python 使用 EventGridEvent 处理创建的 blob
【发布时间】:2021-03-19 02:21:19
【问题描述】:

在以下代码中,我检索了一个我打算处理的已创建 blob URL。谁能建议一个教程,逐步介绍我如何下载 blob(这是一个视频)、打开它并在触发此事件时处理每一帧?

【问题讨论】:

    标签: opencv azure-functions azure-blob-storage


    【解决方案1】:

    您可以参考articledownload_blob 方法来下载 blob

    参考here处理每一帧

    import json
    import logging
    import cv2
    import azure.functions as func
    
    from azure.storage.blob import BlobServiceClient, generate_blob_sas, AccessPolicy, BlobSasPermissions
    from azure.core.exceptions import ResourceExistsError
    from datetime import datetime, timedelta
    
    def main(event: func.EventGridEvent):
        result = json.dumps({
            'id': event.id,
            'data': event.get_json(),
            'topic': event.topic,
            'subject': event.subject,
            'event_type': event.event_type,
        })
    
        logging.info('Python EventGrid trigger processed an event: %s', result)
    
        connect_string = "connect string of storage"
        DEST_FILE = "path to download the video"
    
        blob_service_client = BlobServiceClient.from_connection_string(connect_string)
    
        blob_url = event.get_json().get('url')
        logging.info('blob URL: %s', blob_url)
    
        blob_name = blob_url.split("/")[-1].split("?")[0]
        container_name = blob_url.split("/")[-2].split("?")[0]
    
        # Download blob to DEST_FILE
        blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
        with open(DEST_FILE, "wb") as my_blob:
           download_stream = blob_client.download_blob()
           my_blob.write(download_stream.readall())
        
        # Process images of a video, frame by frame
        video_path = DEST_FILE + "/" +blob_name
        logging.info('video path: %s', video_path)
        cap = cv2.VideoCapture(video_path)
        count = 0
        while cap.isOpened():
            ret,frame = cap.read()
            cv2.imshow('window-name', frame)
            cv2.imwrite("frame%d.jpg" % count, frame)
            count = count + 1
            if cv2.waitKey(10) & 0xFF == ord('q'):
                break
    
        cap.release()
        cv2.destroyAllWindows() # destroy all opened windows
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 2018-05-08
      • 2019-08-05
      • 2019-05-11
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      相关资源
      最近更新 更多