Skip to content
Advertisement

How to play video on google colab with opencv?

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement