用opencv调用系统摄像头

本文介绍了一个使用Python和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, 1948) # set video widht
cam.set(4, 1948) # set video height
#cam.set(CV_CAP_PROP_SETTINGS, 1) # video settings 
ret, img =cam.read() 
img = np.rot90(img)
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 = np.rot90(img)
    img = cv2.flip(img, 1)
    cv2.imshow('camera',img) 
   
    k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video
    if k == ord("s"):
        textTime = time.strftime('new3-%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()

可以调用摄像头,摄像,按“s”键保存单张图片到电脑桌面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值