山内セミナー(2019/05/15)

関連サイトと資料

サンプル動画

test.avi

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

read_camera.py
import cv2
	
	
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:
        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
	
    else:
        break
	
capture.release()
cv2.destroyAllWindows()

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

read_camera_capture.py
import cv2
	
	
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('カメラの起動に失敗しました!')
	
frame_index = 0
	
while capture.isOpened():
    ret, frame = capture.read()
	
    if ret is True:
        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('c'):
            frame_name = 'camera_frame_{0}.png'.format(frame_index)
            gray_frame_name = 'grayscale_camera_frame_{0}.png'.format(frame_index)
	
            cv2.imwrite(frame_name, frame)
            cv2.imwrite(gray_frame_name, gray_frame)
            frame_index += 1
	
        if cv2.waitKey(20) & 0xFF == ord('q'):
            break
	
    else:
        break
	
capture.release()
cv2.destroyAllWindows()

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

read_video_file.py
import cv2
	
	
target = 'test.avi'
capture = cv2.VideoCapture(target)
	
if capture.isOpened() is False:
    print('動画ファイル[{0}]を開くのに失敗しました!'.format(target))
	
while capture.isOpened():
    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)
	
        if cv2.waitKey(20) & 0xFF == ord('q'):
            break
	
    else:
        break
	
capture.release()
cv2.destroyAllWindows()

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

read_ip_camera.py
import cv2
	
	
url = 'http://217.126.89.102:8010/axis-cgi/mjpg/video.cgi'
capture = cv2.VideoCapture(url)
	
if capture.isOpened() is False:
    print('IPカメラ[{0}]を開くのに失敗しました!'.format(url))
	
while capture.isOpened():
    ret, frame = capture.read()
	
    if ret is True:
        cv2.imshow('Club Nàutic Port de la Selva (Girona - Spain)', frame)
	
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('Grayscale Club Nàutic Port de la Selva (Girona - Spain)', 
        gray_frame)
	
        if cv2.waitKey(2000) & 0xFF == ord('q'):
            break
	
    else:
        break
	
capture.release()
cv2.destroyAllWindows()

サンプルプログラム(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()