In the following code, I retrieve a created blob URL which I intend to process. Could anyone suggest a tutorial that steps through how I would download the blob (which is a video), open it, and process each frame when this event is triggered?
Asked
Active
Viewed 403 times
1 Answers
1
You could refer to this article and download_blob
method to download the blob.
And refer to here for processing each frame.
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

unknown
- 6,778
- 1
- 5
- 14