山内セミナーⅠ(2020/06/17)

関連サイトと資料

サンプル動画

test.avi

サンプルプログラム(5)

read_camera_fps.py
import cv2
import time   
    
capture = cv2.VideoCapture(0)
    
frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = capture.get(cv2.CAP_PROP_FPS)
    
print('横幅: {0}'.format(frame_width))
print('高さ: {0}'.format(frame_height))
print('fps: {0}'.format(fps))
    
if capture.isOpened() is False:
    print('カメラの起動に失敗しました!')
    
while capture.isOpened():
    ret, frame = capture.read()
    
    if ret is True:
        processing_start = time.time()
    
        cv2.imshow('Image from Camera', frame)
    
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('Grayscale image from Camera', gray_frame)
    
        if cv2.waitKey(20) & 0xFF == ord('q'):
            break
    
        processing_end = time.time()
        processing_time_frame = processing_end - processing_start
        print('fps: {0}'.format(1.0 / processing_time_frame))
    
    else:
        break
    
capture.release()
cv2.destroyAllWindows()
    

サンプルプログラム(6)

write_video_file.py
import cv2    
    
save_video_filename = 'test2.avi' 
capture = cv2.VideoCapture(0)
    
frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = capture.get(cv2.CAP_PROP_FPS)
    
print('横幅: {0}'.format(frame_width))
print('高さ: {0}'.format(frame_height))
print('fps: {0}'.format(fps))
    
if capture.isOpened() is False:
    print('カメラの起動に失敗しました!')
    
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out_gray = cv2.VideoWriter(save_video_filename, fourcc, int(fps), 
(int(frame_width), int(frame_height)), False)
    
while capture.isOpened():
    ret, frame = capture.read()
    
    if ret is True:
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        out_gray.write(gray_frame)
    
        cv2.imshow('gray', gray_frame)
    
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    else:
        break
    
capture.release()
out_gray.release()
cv2.destroyAllWindows()
    

サンプルプログラム(7)

read_video_file_all_properties.py
import cv2   
    
def decode_fourcc(fourcc):
    fourcc_int = int(fourcc)
    
    print('fourcc(整数値): {0}'.format(fourcc_int))
    
    fourcc_decode = ''
    for i in range(4):
        int_value = fourcc_int >> 8 * i & 0xFF
        print('整数値: {0}'.format(int_value))
        fourcc_decode += chr(int_value)
    
    return fourcc_decode
    
    
target = 'test.avi'
capture = cv2.VideoCapture(target)
    
print('CV_CAP_PROP_FRAME_WIDTH: {0}'.format(capture.get(cv2.CAP_PROP_FRAME_WIDTH)))
print('CV_CAP_PROP_FRAME_HEIGHT : {0}'.format(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print('CAP_PROP_FPS : {0}'.format(capture.get(cv2.CAP_PROP_FPS)))
print('CAP_PROP_POS_MSEC : {0}'.format(capture.get(cv2.CAP_PROP_POS_MSEC)))
print('CAP_PROP_POS_FRAMES : {0}'.format(capture.get(cv2.CAP_PROP_POS_FRAMES)))
print('CAP_PROP_FOURCC  : {0}'.format(decode_fourcc(capture.get(cv2.CAP_PROP_FOURCC))))
print('CAP_PROP_FRAME_COUNT  : {0}'.format(capture.get(cv2.CAP_PROP_FRAME_COUNT)))
print('CAP_PROP_MODE : {0}'.format(capture.get(cv2.CAP_PROP_MODE)))
print('CAP_PROP_BRIGHTNESS : {0}'.format(capture.get(cv2.CAP_PROP_BRIGHTNESS)))
print('CAP_PROP_CONTRAST : {0}'.format(capture.get(cv2.CAP_PROP_CONTRAST)))
print('CAP_PROP_SATURATION : {0}'.format(capture.get(cv2.CAP_PROP_SATURATION)))
print('CAP_PROP_HUE : {0}'.format(capture.get(cv2.CAP_PROP_HUE)))
print('CAP_PROP_GAIN  : {0}'.format(capture.get(cv2.CAP_PROP_GAIN)))
print('CAP_PROP_EXPOSURE : {0}'.format(capture.get(cv2.CAP_PROP_EXPOSURE)))
print('CAP_PROP_CONVERT_RGB : {0}'.format(capture.get(cv2.CAP_PROP_CONVERT_RGB)))
print('CAP_PROP_RECTIFICATION : {0}'.format(capture.get(cv2.CAP_PROP_RECTIFICATION)))
print('CAP_PROP_ISO_SPEED : {0}'.format(capture.get(cv2.CAP_PROP_ISO_SPEED)))
print('CAP_PROP_BUFFERSIZE : {0}'.format(capture.get(cv2.CAP_PROP_BUFFERSIZE)))
    
if capture.isOpened() is False:
    print('動画ファイル[{0}]を開くのに失敗しました!'.format(target))
    
while capture.isOpened():
    ret, frame = capture.read()
    
    if ret is True:
        print('CAP_PROP_POS_FRAMES : {0}'.format(capture.get(cv2.CAP_PROP_POS_FRAMES)))
        print('CAP_PROP_POS_MSEC : {0}'.format(capture.get(cv2.CAP_PROP_POS_MSEC)))
    
        cv2.imshow('Image from Video', frame)
    
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('Grayscale image from Video', gray_frame)
    
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break
    
    else:
        break
    
capture.release()
cv2.destroyAllWindows()
    

サンプルプログラム(8)

read_video_file_backwards.py
import cv2    
    
target = 'test.avi'
capture = cv2.VideoCapture(target)
    
if capture.isOpened() is False:
    print('動画ファイル[{0}]を開くのに失敗しました!'.format(target))
else:
    frame_index = capture.get(cv2.CAP_PROP_FRAME_COUNT) - 1
    print('開始フレーム: {0}'.format(frame_index))
    
while capture.isOpened() and frame_index >= 0:
    capture.set(cv2.CAP_PROP_POS_FRAMES, frame_index)
    ret, frame = capture.read()
    
    if ret is True:
        cv2.imshow('Image from Video', frame)
    
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('Grayscale image from Video', gray_frame)
    
        frame_index = frame_index - 1
        print('次のフレーム: {0}'.format(frame_index))
    
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break
    
    else:
        break
    
capture.release()
cv2.destroyAllWindows()