python opencv 打开USB相机并设置一些参数

打开单个相机

import cv2
import numpy as np
import datetime

expo = -7
saveKey = '0'
imgPath = "D:/img_collect/"

cap = cv2.VideoCapture(0)

#判断是否正常开启
print(cap.isOpened())
cap.set(3, 800)
cap.set(4, 600)
cap.set(cv2.CAP_PROP_EXPOSURE, expo)
frameNum = 0
# i = 1
while cap.isOpened():
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    # print(frameNum)
    fr = frame[10:590, 150:700]
    fr = np.rot90(fr, -1)
    img = cv2.resize(fr, (640, 480))
    #frameNum = frameNum + 1
    cv2.imshow('img', img)

    times = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S_%f')
    fileName = imgPath + times + str(expo) + '.JPG'
    # print(fileName)
	#每10帧存储一张图片
    """
    if frameNum % 10 == 1:
        if i < 10:
            i = '0' + str(i)
        cv2.imwrite('images/' + str(i) + '.jpg', frame)
        i = int(i)
        i += 1
        break
    """
    if cv2.waitKey(1) == ord(saveKey):
        frameNum = frameNum + 1
        cv2.imwrite(fileName, img)
        print("成功保存图片%s: {%s}"%(frameNum,fileName))

cap.release()
cv2.destroyAllWindows()

打开多个相机

# conding=utf-8
import cv2
import time
import numpy as np

class Camera:
    def __init__(self):
        self.image = None

    def getFrame(self, index):
        capture = cv2.VideoCapture(index)
        capture.set(3,800)
        capture.set(4,600)
        #capture.set(16, -5)
        capture.set(cv2.CAP_PROP_EXPOSURE, -7)
        #capture.set(cv2.CAP_PROP_FPS, 5)

        if capture.isOpened()!= True:
            print("打开摄像头失败")
            return
        frameNum = 0
        while(frameNum < 5):
            ret, frame = capture.read()
            frameNum += 1

        if ret == False:
            print("获取图像失败")
            return
        capture.release()
        fr = frame[10:590, 150:700]
        fr = np.rot90(fr,-1)
        img = cv2.resize(fr,(640,480))
        
        return img

    def saveImage(self, path):




if __name__ == '__main__':
    cameraNum = 4
    cameras = [Camera() for i in range(cameraNum)]
    frame0 = cameras[0].getFrame(0)
    cv2.imshow("frame0", frame0)

    cv2.waitKey()
    #fileName = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
    #print(fileName)
    #print("D:/code/PycharmProjects/Autoselling/" + fileName + "{0}.jpg")
    #cv2.imwrite("D:/code/PycharmProjects/Autoselling/" + fileName + "{0}.jpg", frame0)

截取数据集程序

import cv2
import numpy as np
import datetime

expo = -8
saveKey = '0'
imgPath = "D:/2019_7_25_img_collect/baokuanglishuite500ml/"

cap = cv2.VideoCapture(0)

#判断是否正常开启
print(cap.isOpened())
cap.set(3, 1280)
cap.set(4, 960)
cap.set(cv2.CAP_PROP_EXPOSURE, expo)
frameNum = 0
# i = 1
while cap.isOpened():
    ret, frame = cap.read()
    #cv2.imshow('frame', frame)
    # print(frameNum)
    fr = frame[10:950,200:1000]
    fr = np.rot90(fr, -1)
    img = cv2.resize(fr, (640, 480))
    # frameNum = frameNum + 1
    cv2.imshow('img', img)

    times = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S_%f')
    fileName = imgPath + times + str(expo) + '.JPG'
    # print(fileName)
#每10帧存储一张图片
    """
    if frameNum % 10 == 1:
        if i < 10:
            i = '0' + str(i)
        cv2.imwrite('images/' + str(i) + '.jpg', frame)
        i = int(i)
        i += 1
        break
    """
    if cv2.waitKey(2) == ord(saveKey):
        frameNum = frameNum + 1
        cv2.imwrite(fileName, img)
        print("成功保存图片%s: {%s}"%(frameNum, fileName))

cap.release()
cv2.destroyAllWindows()

更多相关推荐

Opencv 打开网络相机时的一些参...

opencv默认打开相机时候的大小是640*480大小的,如果需要设置可以按照如下格式设置:当然,设...

继续阅读

python + opencv在imagepy中开发...

最近发现了imagepy这个宝藏,正好师兄毕业了,留下了一个USB相机,为了方便使用,结合imagepy...

继续阅读

python控制相机,在OpenCV / Pyth...

我正在使用来自Thorlabs(DC1545M)的USB摄像机的OpenCV(2.4)和Python(2.7.3)。我正在对视频流进...

继续阅读

用python开启相机_使用“打开”编...

是的,有一种方法可以用OpenCV手动控制USB网络摄像头。尽管如此,我认为C5技术适用于所有的网...

继续阅读

python opencv获取视频的总帧数...

@pythonopencv获取视频的总帧数等参数importcv2cap=cv2.VideoCapture(path)frames_num=cap.get...

继续阅读

ROS+opencv实践-打开USB相机做边...

功能包资源下载:http://www.hzcourse.com/oep/image/ueditor/jsp/upload/file/20190812/62199...

继续阅读

2.opencv学习——打开与电脑连接的...

古月金真代码如下:相关解释:waitKey(delay)函数的功能是不断刷新图像,频率时间为delay,单...

继续阅读

Ubuntu 下使用opencv打开usb 摄...

一opencv安装1.安装ubuntu下opencv安装比较简单,直接使用命令 可以看到很多相关包都会被安装...

继续阅读

关于用OpenCV的CalibrateCamera...

最近尝试用OpenCV来标定工业相机,先下载了OpenCV自带的的标定的例程camera_calibration.cpp,...

继续阅读

【python】opencv教程CV2模块——...

CV2模块教程系列——打开读取图片、窗口显示、保存图片 

继续阅读