Tip:鍵盤響應.
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 26 11:14:19 2018
@author: Miracle
"""
import cv2
#打開默認攝像頭
cap = cv2.VideoCapture(0)
#當前狀態、之前狀態
cur_flag = -1
pre_flag = -1
#判斷是否打開
if not cap.isOpened():
raise IOError('Cannot open webcam!')
#無限循環
while True:
#每一幀
ret,frame = cap.read()
frame = cv2.resize(frame,None,fx = 1.25,fy = 1.05,
interpolation = cv2.INTER_CUBIC)
#獲取鍵盤事件
flag = cv2.waitKey(1)
#Esc,退出
if flag == 27:
break
#判斷是否按下其他鍵
if flag > -1 and flag != pre_flag:
cur_flag = flag
pre_flag = flag
#響應事件
if cur_flag == ord('g'):
output = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
elif cur_flag == ord('y'):
output = cv2.cvtColor(frame,cv2.COLOR_BGR2YUV)
elif cur_flag == ord('h'):
output = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
else:
output = frame
#顯示
cv2.imshow('webcam',output)
#關閉
cap.release()
cv2.destroyAllWindows()