opencv调取摄像头并实时显示

# -*- coding: utf-8 -*-
"""
Created on Mon May  7 14:59:54 2018

@author: PC
"""

import cv2
import time
import numpy as np  

cam = cv2.VideoCapture(0)

cam.set(3, 2448) # set video widht
cam.set(4, 2448) # set video height
ret, img =cam.read() 
img = np.rot90(img)
#img = np.rot90(img)
img = cv2.flip(img, 1)
img = np.rot90(img)
cv2.namedWindow('camera')

#cam1 = cv2.VideoCapture(1)
#cam1.set(3, 1440) # set video widht
#cam1.set(4, 1280) # set video height
#ret, img1 =cam1.read() 
#img = cv2.flip(img, 1)
#img1 = np.rot90(img1)
#cv2.namedWindow('camera1')

#cv2.setMouseCallback("img", onmouse)   #回调绑定窗口           
while True:
    ret, img =cam.read() 
    img = np.rot90(img)
#    img = np.rot90(img)
    img = cv2.flip(img, 1)
    img = np.rot90(img)
    cv2.imshow('camera',img) 
   
    k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
    if k == ord("s"):
        textTime = time.strftime('new2-%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
        imageName = "C:/Users/PC/Desktop/" + textTime + ".jpg"
        cv2.imwrite(imageName, img)
#        break
    if k == 27:
        break
#    
#    ret, img1 =cam1.read() 
#    img1 = cv2.flip(img1, 1)
#    img1 = np.rot90(img1)
#    cv2.imshow('camera1',img1)-
#    k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
#    if k == ord("s"):
#        textTime = time.strftime('new1-%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
#        imageName = "C:/Users/PC/Desktop/" + textTime + ".jpg"
#        cv2.imwrite(imageName, img1)
##        break
#    if k == 27:
#        break

cam.release()
cv2.destroyAllWindows()

猜你喜欢

转载自blog.csdn.net/weixin_39153202/article/details/82425056