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

関連サイトと資料

サンプル動画

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()