How to write video then download using cv2 in Google colab?

Viewed 143

I am trying to do some image processing on a video, then save the resulting video using opencv on colab. However, I am not able to access the resulting video file that I am writing to.

import cv2
from google.colab.patches import cv2_imshow
import numpy as np

fourcc = cv2.VideoWriter_fourcc(*'H264')
cap = cv2.VideoCapture(vid_file)
out = cv2.VideoWriter('output.mp4',fourcc,30.0,(1124,1080))
cnt = 0
ret = True
while(ret):

  ret,frame = cap.read()
  print(cnt,end=' ')

  # check if prey was tracked on this frame
  match = np.where(prey_frames==cnt)[0]
  if match:
    prey_frame = match[0]
    # print(prey_frame)
    image = cv2.circle(frame,(int(prey_px[prey_frame].x),95+int(prey_px[prey_frame].y)),
                       radius=5,color=(255,0,255),thickness=2)
  else:
    image = frame

  out.write(image)
  cnt += 1

out.release()
cap.release()
cv2.destroyAllWindows()

From what I understand, this should write to a file called 'output.mp4'. This code runs without error, but there is no file in the current directory, and no file of that name available to download (using files.download('output.mp4') returns 'cannot find file' error).

Any help would be appreciated!

0 Answers
Related