本文整理汇总了Python中cv2.VideoCapture.set方法的典型用法代码示例。如果您正苦于以下问题:Python VideoCapture.set方法的具体用法?Python VideoCapture.set怎么用?Python VideoCapture.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2.VideoCapture
的用法示例。
在下文中一共展示了VideoCapture.set方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: noUVCCapture
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class noUVCCapture():
"""
VideoCapture without uvc control
"""
def __init__(self, src,size=(640,480)):
self.controls = None
self.cvId = src
###add cv videocapture capabilities
self.cap = VideoCapture(src)
self.set_size(size)
self.read = self.cap.read
def set_size(self,size):
width,height = size
self.cap.set(3, width)
self.cap.set(4, height)
def get_size(self):
return self.cap.get(3),self.cap.get(4)
def read_RGB(self):
s,img = self.read()
if s:
cvtColor(img,COLOR_RGB2BGR,img)
return s,img
def read_HSV(self):
s,img = self.read()
if s:
cvtColor(img,COLOR_RGB2HSV,img)
return s,img
示例2: CameraCapture
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class CameraCapture(uvc.Camera):
"""
CameraCapture Class for Image grabbing and control
inherits from an OS specitic Camera that defines all uvc control methods
"""
def __init__(self, cam,size=(640,480)):
super(CameraCapture, self).__init__(cam)
###add cv videocapture capabilities
self.cap = VideoCapture(self.cvId)
self.set_size(size)
self.read = self.cap.read
def set_size(self,size):
width,height = size
self.cap.set(3, width)
self.cap.set(4, height)
def get_size(self):
return self.cap.get(3),self.cap.get(4)
def read_RGB(self):
s,img = self.read()
if s:
cvtColor(img,COLOR_RGB2BGR,img)
return s,img
def read_HSV(self):
s,img = self.read()
if s:
cvtColor(img,COLOR_RGB2HSV,img)
return s,img
示例3: FileCapture
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class FileCapture():
"""
simple file capture that can auto_rewind
"""
def __init__(self,src):
self.auto_rewind = True
self.controls = None #No UVC controls available with file capture
# we initialize the actual capture based on cv2.VideoCapture
self.cap = VideoCapture(src)
timestamps_loc = os.path.join(src.rsplit(os.path.sep,1)[0],'eye_timestamps.npy')
logger.info("trying to load timestamps with video at: %s"%timestamps_loc)
try:
self.timestamps = np.load(timestamps_loc).tolist()
logger.info("loaded %s timestamps"%len(self.timestamps))
except:
logger.info("did not find timestamps")
self.timestamps = None
self._get_frame_ = self.cap.read
def get_size(self):
return self.cap.get(3),self.cap.get(4)
def set_fps(self):
pass
def get_fps(self):
return None
def read(self):
s, img =self._get_frame_()
if self.auto_rewind and not s:
self.rewind()
s, img = self._get_frame_()
return s,img
def get_frame(self):
s, img = self.read()
if self.timestamps:
timestamp = self.timestamps.pop(0)
else:
timestamp = time()
return Frame(timestamp,img)
def rewind(self):
self.cap.set(1,0) #seek to the beginning
def create_atb_bar(self,pos):
return 0,0
def kill_atb_bar(self):
pass
def close(self):
pass
示例4: FileCapture
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class FileCapture():
"""
simple file capture that can auto_rewind
"""
def __init__(self,src):
self.auto_rewind = True
self.controls = None #No UVC controls available with file capture
# we initialize the actual capture based on cv2.VideoCapture
self.cap = VideoCapture(src)
self._get_frame_ = self.cap.read
def get_size(self):
return self.cap.get(3),self.cap.get(4)
def set_fps(self):
pass
def get_fps(self):
return None
def read(self):
s, img =self._get_frame_()
if self.auto_rewind and not s:
self.rewind()
s, img = self._get_frame_()
return s,img
def get_frame(self):
s, img = self.read()
timestamp = time()
return Frame(timestamp,img)
def rewind(self):
self.cap.set(1,0) #seek to the beginning
def create_atb_bar(self,pos):
return 0,0
def kill_atb_bar(self):
pass
def close(self):
pass
示例5: FileCapture
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class FileCapture():
"""
simple file capture that can auto_rewind
"""
def __init__(self,src):
self.auto_rewind = True
self.controls = None #No UVC controls available with file capture
# we initialize the actual capture based on cv2.VideoCapture
self.cap = VideoCapture(src)
self._get_frame_ = self.cap.read
def get_size(self):
return self.cap.get(3),self.cap.get(4)
def read(self):
s, img =self._get_frame_()
if self.auto_rewind and not s:
self.rewind()
s, img = self._get_frame_()
return s,img
def read_RGB(self):
s,img = self.read()
if s:
cvtColor(img,COLOR_RGB2BGR,img)
return s,img
def read_HSV(self):
s,img = self.read()
if s:
cvtColor(img,COLOR_RGB2HSV,img)
return s,img
def rewind(self):
self.cap.set(1,0) #seek to the beginning
示例6: Camera_Capture
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class Camera_Capture():
"""
VideoCapture without uvc control using cv2.VideoCapture
"""
def __init__(self,src_id,size=(640,480),fps=None,timebase=None):
self.controls = None
self.cvId = src_id
self.name = "VideoCapture"
self.controls = None
###add cv videocapture capabilities
self.capture = VideoCapture(src_id)
self.set_size(size)
if timebase == None:
logger.debug("Capture will run with default system timebase")
self.timebase = c_double(0)
elif isinstance(timebase,c_double):
logger.debug("Capture will run with app wide adjustable timebase")
self.timebase = timebase
else:
logger.error("Invalid timebase variable type. Will use default system timebase")
self.timebase = c_double(0)
def get_frame(self):
s, img = self.capture.read()
timestamp = time()
return Frame(timestamp,img)
def set_size(self,size):
width,height = size
self.capture.set(3, width)
self.capture.set(4, height)
def get_size(self):
return self.capture.get(3), self.capture.get(4)
def set_fps(self,fps):
self.capture.set(5,fps)
def get_fps(self):
return self.capture.get(5)
def get_now(self):
return time()
def create_atb_bar(self,pos):
size = 0,0
return size
def kill_atb_bar(self):
pass
def close(self):
pass
示例7: Camera_Capture
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class Camera_Capture():
"""
VideoCapture without uvc control using cv2.VideoCapture
"""
def __init__(self,src_id,size=(640,480),fps=None):
self.controls = None
self.cvId = src_id
self.name = "VideoCapture"
self.controls = None
###add cv videocapture capabilities
self.capture = VideoCapture(src_id)
self.set_size(size)
def get_frame(self):
s, img = self.capture.read()
timestamp = time()
return Frame(timestamp,img)
def set_size(self,size):
width,height = size
self.capture.set(3, width)
self.capture.set(4, height)
def get_size(self):
return self.capture.get(3), self.capture.get(4)
def set_fps(self,fps):
self.capture.set(5,fps)
def get_fps(self):
return self.capture.get(5)
def create_atb_bar(self,pos):
size = 0,0
return size
def kill_atb_bar(self):
pass
def close(self):
pass
示例8: camera
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class camera(object):
'''
Object containing camera information
Call-able, retrieve current frame in camera buffer
User accessible attributes:
device system device number
resolution camera resolution
BGRimage image in BGR format
HSVimage image in HSV format
RGBimage image in RGB format
FPS camera speed in FPS
User accessible methods:
close close camera device
'''
def __init__(self, cam_num = -1, resolution = (640, 480)):
'''
create camera object
cam_num device number (integer)
resolution image resolution (tuple width x height)
'''
self.device = cam_num
self.resolution = resolution
self.BGRimage = []
self.HSVimage = []
self.RGBimage = []
self.FPS = [0, 0]
self.__avr = 0
#assign and open device
self.__capture = VideoCapture(cam_num)
self.__capture.set(CV_CAP_PROP_FRAME_WIDTH,resolution[0])
self.__capture.set(CV_CAP_PROP_FRAME_HEIGHT,resolution[1])
self.__capture.open
self.__flag = False
t0 = time()
self.__flag, self.BGRimage = self.__capture.read()
self.FPS[0] = 1/(time()-t0)
self.FPS[1] = self.FPS[0]
self.__avr = self.FPS[0]
print "camera", self.device, "ready @", self.FPS[0], "fps"
return
def __call__(self, frame_delay = 0, fast = False):
'''
retrieve current frame in camera buffer
frame_delay delay the frame decoding (integer)
fast if true don't decode image to RGB format (logic)
'''
#set timer to meassure fps
self.__avr = self.FPS[1]
t0 = time()
#try to retrieve current frame
while not self.__flag:
if frame_delay > 0:
for i in xrange(frame_delay + 1):
self.__capture.grab()
self.__flag, self.BGRimage = self.__capture.retrieve()
del i
else:
self.__flag, self.BGRimage = self.__capture.read()
self.__flag = False
#decode bgr format to hsv
self.HSVimage = cvtColor(self.BGRimage, CV_BGR2HSV)
if fast:
self.FPS[0] = 1/(time()-t0)
self.FPS[1] = (self.FPS[0]+self.__avr)/2
return
#decode bgr format to rgb
self.RGBimage = cvtColor(self.BGRimage, CV_BGR2RGB)
self.FPS[0] = 1/(time()-t0)
self.FPS[1] = (self.FPS[0]+self.__avr)/2
return
def __str__(self):
'''
return camera information;
device number
device resolution
instant speed
average speed
'''
tmp = "camera object @ dev "+str(self.device)+", resolution: "+str(self.resolution)
tmp = tmp +", fps: "+str(self.FPS[0])+", Avr. fps: "+str(self.FPS[1])
return tmp
def __del__(self):
'''
when the object is deleted, it closes the device
'''
self.close()
return
def close(self):
'''
close device, making it available to use
'''
#if the device is open then close it
if self.__capture.isOpened():
self.__capture.release()
print "camera", self.device, "closed"
return
示例9: Camera_Capture
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class Camera_Capture(object):
"""docstring for uvcc_camera"""
def __init__(self, cam, size=(640, 480), fps=30):
self.src_id = cam.src_id
self.uId = cam.uId
self.name = cam.name
self.controls = Controls(self.uId)
try:
self.controls["UVCC_REQ_FOCUS_AUTO"].set_val(0)
except KeyError:
pass
self.capture = VideoCapture(self.src_id)
self.set_size(size)
def re_init(self, cam, size=(640, 480), fps=30):
self.src_id = cam.src_id
self.uId = cam.uId
self.name = cam.name
self.controls = Controls(self.uId)
try:
self.controls["UVCC_REQ_FOCUS_AUTO"].set_val(0)
except KeyError:
pass
self.capture = VideoCapture(self.src_id)
self.set_size(size)
# recreate the bar with new values
bar_pos = self.bar._get_position()
self.bar.destroy()
self.create_atb_bar(bar_pos)
def re_init_cam_by_src_id(self, src_id):
try:
cam = Camera_List()[src_id]
except KeyError:
logger.warning("could not reinit capture, src_id not valid anymore")
return
self.re_init(cam, self.get_size())
def get_frame(self):
s, img = self.capture.read()
timestamp = time()
return Frame(timestamp, img)
def set_size(self, size):
width, height = size
self.capture.set(3, width)
self.capture.set(4, height)
def get_size(self):
return self.capture.get(3), self.capture.get(4)
def set_fps(self, fps):
self.capture.set(5, fps)
def get_fps(self):
return self.capture.get(5)
def create_atb_bar(self, pos):
# add uvc camera controls to a separate ATB bar
size = (200, 200)
self.bar = atb.Bar(
name="Camera_Controls",
label=self.name,
help="UVC Camera Controls",
color=(50, 50, 50),
alpha=100,
text="light",
position=pos,
refresh=2.0,
size=size,
)
sorted_controls = [c for c in self.controls.itervalues()]
sorted_controls.sort(key=lambda c: c.order)
cameras_enum = atb.enum("Capture", dict([(c.name, c.src_id) for c in Camera_List()]))
self.bar.add_var("Capture", vtype=cameras_enum, getter=lambda: self.src_id, setter=self.re_init_cam_by_src_id)
for control in sorted_controls:
name = control.atb_name
if control.type == "bool":
self.bar.add_var(name, vtype=atb.TW_TYPE_BOOL8, getter=control.get_val, setter=control.set_val)
elif control.type == "int":
self.bar.add_var(name, vtype=atb.TW_TYPE_INT32, getter=control.get_val, setter=control.set_val)
self.bar.define(definition="min=" + str(control.min), varname=name)
self.bar.define(definition="max=" + str(control.max), varname=name)
self.bar.define(definition="step=" + str(control.step), varname=name)
elif control.type == "menu":
if control.menu is None:
vtype = None
else:
vtype = atb.enum(name, control.menu)
self.bar.add_var(name, vtype=vtype, getter=control.get_val, setter=control.set_val)
#.........这里部分代码省略.........
示例10: Camera
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class Camera(object):
''' Communicate with the camera.
Class governing the communication with the camera.
Parameters
-----------
camera : int
the index of the camera, best taken from func lookForCameras,
from eyetracker.camera.capture
dic : dic{propID value}
to check corresponding propIDs check
opencv documentation under the term VideoCapture.
They will be set in the moment of object creation.
Defines
--------
self.camera : index of the camera
self.cap : capturing object
self.frame : returns a frame from camera
self.close : closes cap
self.reOpen : reopens cap
'''
def __init__(self, camera, dic=None):
self.camera = int(camera)
self.cap = VideoCapture(self.camera)
if dic:
for propID, value in dic.iteritems():
self.cap.set(propID, value)
first_frame = self.frame()
def frame(self):
''' Read frame from camera.
Returns
--------
frame : np.array
frame from camera
'''
if self.cap.isOpened:
return self.cap.read()[1]
else:
print 'Cap is not opened.'
return None
def set(self, **kwargs):
''' Set camera parameters.
Parameters
-----------
kwargs : {propID : value}
'''
for propID, value in kwargs.iteritems():
self.cap.set(propID, value)
def close(self):
''' Closes cap, you can reopen it with self.reOpen.
'''
self.cap.release()
def reOpen(self, cameraIndex):
''' Reopens cap.
'''
self.cap.open(self.camera)
first_frame = self.frame()
示例11: Camera_Capture
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class Camera_Capture(object):
"""docstring for uvcc_camera"""
def __init__(self, cam,size=(640,480),fps=30):
self.src_id = cam.src_id
self.uId = cam.uId
self.name = cam.name
self.controls = Controls(self.uId)
try:
self.controls['UVCC_REQ_FOCUS_AUTO'].set_val(0)
except KeyError:
pass
if '6000' in self.name and False: #on mac we dont have enough controls to use this right.
logger.info("adjusting exposure for HD-6000 camera")
try:
self.controls['UVCC_REQ_EXPOSURE_AUTOMODE'].set_val(1)
self.controls['UVCC_REQ_EXPOSURE_ABS'].set_val(156)
except KeyError:
pass
self.capture = VideoCapture(self.src_id)
self.set_size(size)
def get_frame(self):
s, img = self.capture.read()
timestamp = time()
return Frame(timestamp,img)
def set_size(self,size):
width,height = size
self.capture.set(3, width)
self.capture.set(4, height)
def get_size(self):
return self.capture.get(3), self.capture.get(4)
def set_fps(self,fps):
self.capture.set(5,fps)
def get_fps(self):
return self.capture.get(5)
def create_atb_bar(self,pos):
# add uvc camera controls to a separate ATB bar
size = (200,200)
self.bar = atb.Bar(name="Camera_Controls", label=self.name,
help="UVC Camera Controls", color=(50,50,50), alpha=100,
text='light',position=pos,refresh=2., size=size)
sorted_controls = [c for c in self.controls.itervalues()]
sorted_controls.sort(key=lambda c: c.order)
for control in sorted_controls:
name = control.atb_name
if control.type=="bool":
self.bar.add_var(name,vtype=atb.TW_TYPE_BOOL8,getter=control.get_val,setter=control.set_val)
elif control.type=='int':
self.bar.add_var(name,vtype=atb.TW_TYPE_INT32,getter=control.get_val,setter=control.set_val)
self.bar.define(definition='min='+str(control.min), varname=name)
self.bar.define(definition='max='+str(control.max), varname=name)
self.bar.define(definition='step='+str(control.step), varname=name)
elif control.type=="menu":
if control.menu is None:
vtype = None
else:
vtype= atb.enum(name,control.menu)
self.bar.add_var(name,vtype=vtype,getter=control.get_val,setter=control.set_val)
if control.menu is None:
self.bar.define(definition='min='+str(control.min), varname=name)
self.bar.define(definition='max='+str(control.max), varname=name)
self.bar.define(definition='step='+str(control.step), varname=name)
else:
pass
if control.flags == "inactive":
pass
# self.bar.define(definition='readonly=1',varname=control.name)
self.bar.add_button("refresh",self.controls.update_from_device)
self.bar.add_button("load defaults",self.controls.load_defaults)
return size
def kill_atb_bar(self):
pass
def close(self):
pass
示例12: __init__
# 需要导入模块: from cv2 import VideoCapture [as 别名]
# 或者: from cv2.VideoCapture import set [as 别名]
class Worker:
## Initialize Worker robot.
def __init__(self):
try:
self.camera = VideoCapture(CAMERA_INDEX)
self.camera.set(3,CAMERA_WIDTH)
self.camera.set(4,CAMERA_HEIGHT)
message = 'Success.'
except Exception:
message = 'Failure.'
print('[Setting up Camera]...' + message)
try:
self.arduino = serial.Serial(DEVICE, BAUD)
message = 'Success.'
except Exception:
message = 'Failure.'
print('[Setting up Controller]...' + message)
self.reset_worker()
## Capture image then identify Home.
def detect_green(self):
objects = []
x = 0
for cache in range(10):
(success, frame) = self.camera.read()
raw = Image.fromarray(frame)
BGR = raw.split()
B = array(BGR[0].getdata(), dtype=float32)
G = array(BGR[1].getdata(), dtype=float32)
R = array(BGR[2].getdata(), dtype=float32)
NDI_G = (BIAS*G + MINIMUM_COLOR)/(R + B + MINIMUM_COLOR)
matrix = NDI_G.reshape(CAMERA_HEIGHT,CAMERA_WIDTH)
columns = matrix.sum(axis=0)
high = numpy.mean(columns) + numpy.std(columns)
low = numpy.mean(columns)
while (x < CAMERA_WIDTH-1):
if (columns[x] > high):
start = x
while (columns[x] > low) and (x < CAMERA_WIDTH-1):
x += 1
end = x
size = (end - start)
offset = (start + (end - start)/2) - CAMERA_CENTER
if (size > MINIMUM_SIZE):
objects.append((size,offset))
else:
x += 1
return objects
## Detect
def detect_yellow(self):
objects = []
x = 0
for cache in range(30):
(success, frame) = self.camera.read()
raw = Image.fromarray(frame)
BGR = raw.split()
B = array(BGR[0].getdata(), dtype=float32)
G = array(BGR[1].getdata(), dtype=float32)
R = array(BGR[2].getdata(), dtype=float32)
NDI_G = BIAS*(G + R + MINIMUM_COLOR)/(2*B + MINIMUM_COLOR)
matrix = NDI_G.reshape(CAMERA_HEIGHT,CAMERA_WIDTH)
columns = matrix.sum(axis=0)
high = numpy.mean(columns) + numpy.std(columns)
low = numpy.mean(columns)
while (x < CAMERA_WIDTH-1):
if (columns[x] > high):
start = x
while (columns[x] > low) and (x < CAMERA_WIDTH-1):
x += 1
end = x
size = (end - start)
offset = (start + (end - start)/2) - CAMERA_CENTER
if (size > MINIMUM_SIZE):
objects.append((size,offset))
else:
x += 1
return objects
## Is Oriented? --> Boolean
def is_oriented(self):
for cache in range(10):
(success, frame) = self.camera.read()
grayscale = cv2.cvtColor(frame, cv.CV_BGR2GRAY)
blurred = cv2.GaussianBlur(grayscale, (0,0), 5) # (0,0), 5
colored = cv2.cvtColor(blurred,cv2.COLOR_GRAY2BGR)
(flag, thresholded) = cv2.threshold(blurred, 50, 255, cv2.THRESH_BINARY) # 50 and 255 WORKS
circles = cv2.HoughCircles(thresholded,cv2.cv.CV_HOUGH_GRADIENT,DP,MIN_DISTANCE,param1=EDGE_THRESHOLD, param2=CENTER_THRESHOLD, minRadius=MIN_RADIUS,maxRadius=MAX_RADIUS)
try:
for target in circles[0,:]:
x = target[0]
y = target[1]
r = target[2]
if (abs(CAMERA_CENTER - x) < CAMERA_THRESHOLD):
return True
else:
#.........这里部分代码省略.........