python通过opencv读取相机图像与按键保存

import os
import time
import cv2
import keyboard

def takephoto():
    cap = cv2.VideoCapture(0)
    index = 0
    ret, frame = cap.read()

    if not os.path.exists("./test"):
        os.makedirs("./test")

    while True:
        resize = cv2.resize(frame, (640, 480), interpolation=cv2.INTER_NEAREST)
        # time.sleep(0.5)
        ret, frame = cap.read()
        cv2.imshow('frame', frame)
        cv2.waitKey(1)
        if keyboard.is_pressed('q'):
            cv2.imwrite('./test/part_{}.jpg'.format(index), resize)
            index += 1
            time.sleep(0.5)

    cap.release()
    cv2.destroyAllWindows()
    return 0

if __name__=='__main__':
    print('Begin to take pictures..........')
    takephoto()
 
 

你可能感兴趣的:(opencv,python,计算机视觉)