Post

05. Video

05. Video

Video


Prerequisites

1
python

1. Video

On the vision field, there has tasks about image or video. Video is just set of images and display or save sequentially.

So What kind of data we use is just depended on what we want to solve and get results.

60 Frames(FPS) is 60 Frames per second. By means that is 60 images showed sequentially every second. So if we want to reduce overhead from video and there’s no reason we should use high FPS, just reducing FPS. But Some tasks are need at least 30 FPS like AGV because the gap of 30/60 second can make big issue.

2. Video Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import cv2 as cv
import os
import ImageUtils

def videoFromWebcam():
    cap = cv.VideoCapture(0)
    
    if not cap.isOpened():
        exit()
        
    while True:
        ret, frame = cap.read()
        
        if ret:
            cv.imshow('Webcam', frame)
            
        if cv.waitKey(1) == ord('q'):
            break
        
    cap.release()
    cv.destroyAllWindows()

def saveVideo(name, folder = 'output'):
    cap = cv.VideoCapture(0)

    if not cap.isOpened():
        print("Cannot open camera")
        return

    output_path = ImageUtils.getDataPathWithFile(f'{folder}/{name}')

    out = cv.VideoWriter(output_path, 
                         cv.VideoWriter_fourcc(*'XVID'), 
                         20.0, 
                         (640, 480))

    while True:
        ret, frame = cap.read()

        if not ret:
            break

        out.write(frame)
        cv.imshow('Webcam', frame)

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

    cap.release()
    out.release()
    cv.destroyAllWindows()

def videoFromFile(name, folder = 'output'):
    video_path = ImageUtils.getDataPathWithFile(f'{folder}/{name}')
    cap = cv.VideoCapture(video_path)
    
    if not cap.isOpened():
        exit()
        
    while True:
        ret, frame = cap.read()
        
        if ret:
            cv.imshow('Video', frame)
            
        if cv.waitKey(30) == ord('q'):
            break
        
    cap.release()
    cv.destroyAllWindows()
1
2
3
4
5
6
7
8
import cv2 as cv
import os
import ImageUtils
import VideoUtils
import MultiImageViewer as view

if __name__ == "__main__":
    VideoUtils.videoFromWebcam()

"opencv-python-05-01.png"

This post is licensed under CC BY 4.0 by the author.