Skip to content

Commit f7893a4

Browse files
committed
Review- Updating upload_blob function
1 parent 4075fc5 commit f7893a4

1 file changed

Lines changed: 25 additions & 26 deletions

File tree

cloudStorage.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import datetime
33

44
import cv2
5-
import numpy as np
5+
import tempfile
66
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
77

88

@@ -30,35 +30,34 @@ def create_azure_container(container_name = "alerts"):
3030
except Exception as e:
3131
print(f"Error creating container {container_name}: {e}")
3232

33-
def upload_blob(videoArray, videoName, width, height, fps):
33+
def upload_blob(video_array, video_name, width, height, fps):
3434

3535
blob_service_client = init_blob_client()
36-
current_time = datetime.now()
37-
current_day = datetime.today()
38-
current_time = current_time.strftime("%H:%M:%S")
39-
videoName = videoName + " " + str(current_day) + " "+ current_time + ".mp4"
40-
blob_client = blob_service_client.get_blob_client(container="alerts", blob=videoName)
36+
current_time = datetime.now().strftime("%H:%M:%S")
37+
current_day = datetime.today().strftime("%Y-%m-%d")
38+
39+
video_name = f"{video_name} {current_day} {current_time}.mp4"
40+
blob_client = blob_service_client.get_blob_client(container="alerts", blob=video_name)
4141

42-
# Converting videoArray ( numpy array ) into video
43-
# fps = 30 # 25 frames per second
44-
45-
46-
# print(videoName)
47-
output = cv2.VideoWriter(videoName, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height), True)
48-
for i in videoArray:
49-
output.write(i)
50-
output.release()
51-
52-
path = "./" + videoName
53-
# print("path : ", path)
54-
with open(path, "rb") as data :
55-
# Uploading video to the cloud
56-
blob_client.upload_blob(data)
57-
# Delete file after upload
58-
59-
os.remove(path)
42+
try:
43+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_video_file:
44+
output = cv2.VideoWriter(temp_video_file.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height), True)
45+
for i in video_array:
46+
output.write(i)
47+
output.release()
48+
49+
with open(temp_video_file.name, "rb") as data:
50+
# Uploading video to the cloud
51+
blob_client.upload_blob(data)
52+
53+
os.remove(temp_video_file.name) # removing temporary file after upload
54+
return blob_client.url
55+
56+
except Exception as e:
57+
print(f"Error uploading video {video_name}: {e}")
58+
return None
6059

61-
return blob_client.url
60+
6261

6362

6463

0 commit comments

Comments
 (0)