Video Cooking #1 -Open, Read frames, Save

  • Open, close Video
  • Read Video Frame
  • Save Video
  • Webcam Open, Play

Open, close Video

Use cv2.VideoCapture function to open a video file or webcam. When reading is end, you must release the resource using VideoCapture::release function.

import cv2
import sys

cap = cv2.VideoCapture('f:\\tmp\\02.mp4')
ret, img = cap.read()
if ret == False:
    print('Video File Read Error')    
    sys.exit(0)
h, w, c = img.shape
print('Video Frame shape H:%d, W:%d, Channel:%d'%(h, w, c))
cap.release()


Read, Play Video Frame

Use VideoCapture::read function to read a frame inside a video.

import cv2
import sys

cap = cv2.VideoCapture('f:\\tmp\\02.mp4')
ret, img = cap.read()
if ret == False:
    print('Video File Read Error')    
    sys.exit(0)
h, w, c = img.shape
print('Video Frame shape H:%d, W:%d, Channel:%d'%(h, w, c))

count = 1
while cap.isOpened():
    ret, img = cap.read()
    if ret == False:
        break
    count += 1
    #Do what you want to do here!         

print('Video File read End. Total Frames are : %d'%(count))
cap.release()


Save Video

Use cv2.VideoWriter to save a video. This code saves a small frame size (1/2) video.
Codecs that you can use.

  • DIVX : VideoWriter_fourcc('D','I','V','X') or VideoWriter_fourcc(*"DIVX")
  • MJPG : VideoWriter_fourcc('M','J','P','G') or VideoWriter_fourcc(*"MJPG")
Becareful : Codecs must be pre compiled into OpenCV when building. Or OpenCV might search the external codec for encoding, decoding. So some codecs might not work properly.



import cv2
import sys

cap = cv2.VideoCapture('f:\\tmp\\02.mp4')
ret, img = cap.read()
if ret == False:
    print('Video File Read Error')    
    sys.exit(0)
h, w, c = img.shape
print('Video Frame shape H:%d, W:%d, Channel:%d'%(h, w, c))

small_size = (int(w/2), int(h/2))
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
out_video = cv2.VideoWriter('/tmp/output.mp4', fourcc, cap.get(cv2.CAP_PROP_FPS), small_size)

count = 1
while cap.isOpened():
    ret, img = cap.read()
    if ret == False:
        break
    count += 1
    #Do what you want to do here!         
    small_img = cv2.resize(img, small_size)
    out_video.write(small_img)


print('Video File read End. Total Frames are : %d'%(count))
cap.release()
out_video.release()
<video1.py>

Webcam Open, Play

Generally the webcam index is 0 for the first webcam(or only one webcam). So cv2.VideoCapture(0) works well.
Use "ls /dev/ | grep video" command to find the index number.
Output "video0" means that the index is 0, and "video1" means index1. If you find both of them, then you have 2 webcams, if nothing, there's no webcam.


import cv2
import sys

print('Webcam Test')
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False): 
  print("Unable to read camera feed")


cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
ret, img = cap.read()
if ret == False:
    print('WebCAM Read Error')    
    sys.exit(0)
h, w, c = img.shape
print('Video Frame shape H:%d, W:%d, Channel:%d'%(h, w, c))


count = 1
while cap.isOpened():
    try:
        ret, img = cap.read()
        if ret == False:
            break
        count += 1
        cv2.waitKey(1)
        cv2.imshow('webcam', img)
    except KeyboardInterrupt:
        print('Ctrl + C')
        break

print('Webcam Frame read End. Total Frames are : %d'%(count))
cap.release()
<webcam.py>

If you want to stop with a specific key('q' in this case) instead of Ctrl+C, change cv2.waitKey(1) as follows.

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


If you want to use the esc button, use the number 27 instead of ord('q'). The number 27 is the ascii code value of the esc key.

if cv2.waitKey(1) & 0xff == 27:
    break



You can download the source codes here(https://github.com/raspberry-pi-maker/OpenCV)



댓글

이 블로그의 인기 게시물

Image Processing #7 - OpenCV Text

Image Processing #5 - WaterMark using alpha channel

OpenCV Installation - Rasbian Buster, Jessie, DietPi Buster