hoangdangninh
2/16/2017 - 12:39 AM

init_test_opencv.py

# The following program works for python 2.7, opencv 2.4.9.1
import numpy as np
import cv2

# Testing with your webcam
#cap = cv2.VideoCapture(0)

# Testing with a mp4 video file
cap = cv2.VideoCapture("SampleVideo_1280x720_1mb.mp4")

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
# for that SampleVideo using (1280, 720) resolution
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (1280,720))

# for webcam using (640, 480) resolution
#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
#out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))

# Capture frame by frame
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

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

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()