Ask Your Question
3

How to read/write video with OpenCV in Python?

asked 2012-06-08 08:50:21 -0600

Kirill Kornyakov gravatar image

I want to create a small test application, and I need to input/output video with OpenCV Python API. How can I do that?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
7

answered 2012-06-08 11:01:41 -0600

Andrey Kamaev gravatar image

Here is a minimal example:

import cv2
capture = cv2.VideoCapture("video.avi")
while True:
    ret, img = capture.read()

    result = processFrame(img)

    cv2.imshow('some', result)
    if 0xFF & cv2.waitKey(5) == 27:
        break
cv2.destroyAllWindows()
edit flag offensive delete link more

Comments

Does this still stand with the newest versions of opencv and python? (python 3.6.1 64 bit and opencv 3.2)

FullMetal863 gravatar imageFullMetal863 ( 2017-04-19 14:41:00 -0600 )edit
1

answered 2018-03-05 01:36:31 -0600

saideepthik gravatar image

yes it will work for updated versions also and you can try like this also. import cv2 import numpy as np

cap = cv2.VideoCapture("challenge.mp4") # if you are giving camera as input then give (0) to videoCapture. while(cap.isOpened()):

    # Take each frame
ret, image = cap.read()

if ret:
          # do operations on image as per your application
   else:
    cap.set(cv2.CAP_PROP_POS_FRAMES,0)
cv2.imshow('img',image)

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

cap.release() cv2.destroyAllWindows()

this all things you can get directly from opencv tutorial... try @Kirill

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2012-06-08 08:50:21 -0600

Seen: 10,155 times

Last updated: Mar 05 '18