Windows python 读取键盘值且不产生回显(不打印,不等待)

一定要在终端运行改脚本;

 使用 msvcrt.getch() ,按下 键盘上的 "Q" ,读出 b"q"。

import cv2
import numpy as np
import datetime
import time
import sys
import msvcrt
sys.path.append("../")
from config import *

# 摄像头编号
# cam=0
cam = 0

camera = cv2.VideoCapture(cam)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
btn = 0
if __name__ == "__main__":
    if cam == 0:
        result_dir = "./front_image"
    # cam=1
    else:
        result_dir = "./side_image"
    print("Start!")
    while True:

        ch = msvcrt.getch()
        # ch = sys.stdin.read(1)
        print(ch)
        if ch == b"t":
            return_value, image = camera.read()
            cv2.imshow("test", image)
            key = cv2.waitKey(1)
            path = "{}/{}.jpg".format(result_dir, btn)
            btn += 1
            name = "{}.jpg".format(btn)
            print(path)
            cv2.imwrite(path, image)
            time.sleep(0.1)
        if ch == "d":
            cv2.destroyAllWindows()
        if ch == b"q":
            break

你可能感兴趣的:(python)