How to play video on google colab with opencv?

Viewed 16491

I am working on a project related to object detection using Mask RCNN on google colab. I have a video uploaded to my colab. I want to display it as a video while processing it at the runtime using openCV. I want to do what cv2.VideoCapture('FILE_NAME') does on the local machine. Is there any way to do it?

6 Answers

I recently ran into the same issue. Although I do not believe there is a way to show a 'real time' video like you could locally, what you can do instead is to render a single frame at a time and display that in the notebook's output.

Unlike the OP's answer this does not encode the image and it does not clear the previous frame from the console.

from google.colab.patches import cv2_imshow
import cv2

# Download sample video
!curl -o sample.mp4 https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4

cap = cv2.VideoCapture('sample.mp4')
while cap.isOpened():
    ret, image = cap.read()

    if not ret:
      break

    cv2_imshow(image) # Note cv2_imshow, not cv2.imshow

    cv2.waitKey(1) & 0xff

cv2.destroyAllWindows()
cap.release()

This notebook might be helpful, or the code above as a notebook.

Found I way to do it. But it is very slow.

from IPython.display import clear_output, Image
import base64

def arrayShow (imageArray):
    ret, png = cv2.imencode('.png', imageArray)
    encoded = base64.b64encode(png)
    return Image(data=encoded.decode('ascii'))

video_capture = cv2.VideoCapture(VIDEO_SOURCE)
while video_capture.isOpened():
    success, frame = video_capture.read()

    clear_output(wait=True)
    img = arrayShow(frame)
    display(img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

The code does not play video exactly. It just displays the new frame and removes the old frame from output.

You can try:

from google.colab.patches import cv2_imshow
cv2_imshow(img)
from IPython.display import HTML
from base64 import b64encode

video_path = '/content/drive/MyDrive/Data_proc/ball.mp4'

mp4 = open(video_path, "rb").read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
HTML(f"""
<video width=400 controls><source src="{data_url}" type="video/mp4">
</video>""")

I've been programming in python for a few weeks...
I work with industrial automation and in my work
I don't have a development environment for python available, so I use colab the code below was used for me to run video by cv2
I hope I helped someone

import cv2
from google.colab.patches import cv2_imshow
import time

cap = cv2.VideoCapture('video.mp4')

while cap.isOpened():
#while True:
  ok, frame = cap.read()

  if not ok:
    break

  if ok:
    #edit your video size here, to adjust the performance
    largura=frame.shape[1] 
    altura=frame.shape[0]
    lamenor=int(frame.shape[1]/5)
    altmenor=int(frame.shape[0]/5)
    frame = cv2.resize(frame, (lamenor,altmenor))

    # as you read
    clear_output(wait=True)
    cv2_imshow(frame)
    #delay time to update frame
    time.sleep(1.1)

  if cv2.waitKey(1100) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()

...

Edited...

Modified part to make it more fun if you change the source to a webcam

Original:

cap = cv2.VideoCapture('video.mp4')

Modified:

#0 = webcam position in your webcams
cap = cv2.VideoCapture(0)
#or cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

And this part for flip/mirror the frame:

frame2 = cv2.flip(frame, 1)

# as you read
clear_output(wait=True)
cv2_imshow(frame)
cv2_imshow(frame2)

Here is the command on google colab : ret, frame = input_video.read() Wish yhis help you

Related