版权声明:本文为博主原创文章,未经博主允许不得转载。若有任何问题,请联系QQ:575925154(加好友时,请备注:CSDN) https://blog.csdn.net/Miracle0_0/article/details/82080445
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()