本文整理汇总了Python中cv2.CAP_PROP_POS_FRAMES属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.CAP_PROP_POS_FRAMES属性的具体用法?Python cv2.CAP_PROP_POS_FRAMES怎么用?Python cv2.CAP_PROP_POS_FRAMES使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.CAP_PROP_POS_FRAMES属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __next__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def __next__(self):
self.count += 1
# Respect FPS for files
if self.isFile:
delta = time.time() - self.ts
self.sma.add(delta)
time.sleep(max(0,(1.0 - self.sma.current*self.fps)/self.fps))
self.ts = time.time()
# Read image
ret_val, img0 = self.cam.read()
if not ret_val and self.isFile:
self.cam.set(cv2.CAP_PROP_POS_FRAMES, 0)
ret_val, img0 = self.cam.read()
assert ret_val, 'Video Error'
# Preprocess
img = img0
if not self.isFile:
img = cv2.flip(img, 1)
return self.count, img
示例2: count_frames
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def count_frames(self, check_validity=False):
offset = 0
if self.vid_path.endswith('.flv'):
offset = -1
unverified_frame_count = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT)) + offset
if check_validity:
verified_frame_count = 0
for i in range(unverified_frame_count):
self.cap.set(cv2.CAP_PROP_POS_FRAMES, i)
if not self.cap.grab():
logging.warning("VideoIter:: >> frame (start from 0) {} corrupted in {}".format(i, self.vid_path))
break
verified_frame_count = i + 1
self.frame_count = verified_frame_count
else:
self.frame_count = unverified_frame_count
assert self.frame_count > 0, "VideoIter:: Video: `{}' has no frames".format(self.vid_path)
return self.frame_count
示例3: search_switch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def search_switch(self, cap, key, thresh=0.5):
left = 0
right = cap.get(cv2.CAP_PROP_FRAME_COUNT) - 1
cap.set(cv2.CAP_PROP_POS_FRAMES, int((right + left) / 2))
while right > left + 1:
_, frame = cap.read()
image = self.extractor.prepare_frame(frame, self.extractor.image_dict[key][0])
if not self.extractor.exists(image, self.extractor.image_dict[key][1][0], thresh):
left = int((right + left) / 2)
else:
right = int((right + left) / 2)
cap.set(cv2.CAP_PROP_POS_FRAMES, int((right + left) / 2))
cap.set(cv2.CAP_PROP_POS_FRAMES, left)
return left
示例4: skip_from_launch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def skip_from_launch(self, cap, key, time, thresh=None):
"""
Move the capture to T+time (time can be negative) and returns the frame index.
:param cap: OpenCV capture
:param time: delta time from launch to skip to
:return: index of requested frame
"""
if thresh is None:
thresh = self.extractor.image_dict[key][2]
number_of_frames = int(cap.get(cv2.CAP_PROP_FPS) * time) + self.search_switch(cap, key, thresh)
number_of_frames = max(number_of_frames, 0)
number_of_frames = min(number_of_frames, cap.get(cv2.CAP_PROP_FRAME_COUNT))
cap.set(cv2.CAP_PROP_POS_FRAMES, number_of_frames)
return number_of_frames
示例5: find_anchor
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def find_anchor(self, cap, start=0, end=1, maxiter=10):
if not isinstance(self.extractor, RelativeExtract):
return False
original_location = cap.get(cv2.CAP_PROP_POS_FRAMES)
for i in range(maxiter):
pos = random.uniform(start, end)
cap.set(cv2.CAP_PROP_POS_FRAMES, pos*cap.get(cv2.CAP_PROP_FRAME_COUNT))
_, frame = cap.read()
if self.extractor.prepare_image_dict(frame):
return True
cap.set(cv2.CAP_PROP_POS_FRAMES, original_location)
return False
示例6: show_frames
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def show_frames(capture, frame_num_list, bbox_list=None, wait_flag=True):
# Iterate through the frame_num_list, while using indexes for bboxes.
for frame_index, frame_num in enumerate(frame_num_list):
capture.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
_, frame = capture.read()
if bbox_list:
frame = cv2.rectangle(frame, bbox_list[frame_index][0],
bbox_list[frame_index][1], [0, 0, 255], 6)
cv2.imshow('frame', frame)
if wait_flag:
cv2.waitKey(0)
else:
cv2.waitKey(1)
# Given a list of bounding boxes, return the average bounding box.
示例7: read
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def read(self, position=None):
"""Reads a certain frame.
NOTE: The returned frame is assumed to be with `RGB` channel order.
Args:
position: Optional. If set, the reader will read frames from the exact
position. Otherwise, the reader will read next frames. (default: None)
"""
if position is not None and position < self.length:
self.video.set(cv2.CAP_PROP_POS_FRAMES, position)
self.position = position
success, frame = self.video.read()
self.position = self.position + 1
return frame[:, :, ::-1] if success else None
示例8: run_ocr
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def run_ocr(self, lang: str, time_start: str, time_end: str,
conf_threshold: int, use_fullframe: bool) -> None:
self.lang = lang
self.use_fullframe = use_fullframe
ocr_start = utils.get_frame_index(time_start, self.fps) if time_start else 0
ocr_end = utils.get_frame_index(time_end, self.fps) if time_end else self.num_frames
if ocr_end < ocr_start:
raise ValueError('time_start is later than time_end')
num_ocr_frames = ocr_end - ocr_start
# get frames from ocr_start to ocr_end
with Capture(self.path) as v, multiprocessing.Pool() as pool:
v.set(cv2.CAP_PROP_POS_FRAMES, ocr_start)
frames = (v.read()[1] for _ in range(num_ocr_frames))
# perform ocr to frames in parallel
it_ocr = pool.imap(self._image_to_data, frames, chunksize=10)
self.pred_frames = [
PredictedFrame(i + ocr_start, data, conf_threshold)
for i, data in enumerate(it_ocr)
]
示例9: is_picture
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def is_picture(self):
sampling_interval = int(math.floor(self.scene_length / 5))
sampling_frames = list(range(self.start_frame_no + sampling_interval,
self.end_frame_no - sampling_interval + 1, sampling_interval))
frames = []
for frame_no in sampling_frames:
self.video.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
ret, frame = self.video.read()
frames.append(frame)
diff = 0
n_diff = 0
for frame, next_frame in zip(frames, frames[1:]):
diff += cv2.norm(frame, next_frame, cv2.NORM_L1) # abs diff
n_diff += 1
diff /= n_diff
self.debugging_info[4] = round(diff, 0)
return diff < 3000000
示例10: write_to_trackfile
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def write_to_trackfile(track_info):
# st = time.time()
with open(WORK_DIR + "/METADATA/" + "trackfile.txt", "a+") as f:
for item in track_info:
f.write(str(item) + "\n")
# print(str(item) + "\n")
f.close()
# et = time.time()
# print ("write time:", et - st)
# sys.exit()
#uncomment to use as stand-alone file
# start_frame = 8*3
# vid = VIDEO_PATH + "/cam_8.avi"
# cap = cv2.VideoCapture(vid)
# cap.set(cv2.CAP_PROP_FPS, 8)
# cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
# ok, frame = cap.read()
# bbox = selectROI(frame)
# track(0,cap,bbox,2)
# cam_id, cap, bbox, data_inc
示例11: _get_frame_from_capture
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def _get_frame_from_capture(cap, frame_number, filename, total_frames):
frame_pos_const = cv2.cv.CV_CAP_PROP_POS_FRAMES if \
cv2.__version__.startswith('2') else cv2.CAP_PROP_POS_FRAMES
if (cap.get(frame_pos_const) != int(frame_number)):
# Following is an expensive operation; avoid if possible
cap.set(frame_pos_const, int(frame_number))
success, I = cap.read()
if not success:
logger.error('Unable to read frame {} (out-of {}) from {}. Using 0s.'
.format(frame_number, total_frames, filename))
elif I.max() > 256 or I.min() < 0:
logger.error('Invalid values detected when reading frame {} '
'from {}. Using 0s.'.format(frame_number, filename))
else:
return I
return cfg.PIXEL_MEANS
示例12: generate_dataset
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def generate_dataset(videos_path, framerate, width, height):
"""Converts videos from specified path to ndarrays of shape [numberOfVideos, -1, width, height, 1]
Args:
videos_path: Inside the 'videos/' directory, the name of the subdirectory for videos.
framerate: The desired framerate of the dataset.
width: The width we will resize the videos to.
height: The height we will resize the videos to.
Returns:
The dataset with the new size and framerate, and converted to monochromatic.
"""
dataset = []
video_index = 0
for playlist in os.listdir('videos/' + videos_path):
for video_name in os.listdir('videos/{}/{}'.format(videos_path, playlist)):
dataset.append([])
print('Video: {}'.format(video_name))
video = cv2.VideoCapture('videos/{}/{}/{}'.format(videos_path, playlist, video_name))
while video.isOpened():
success, frame = video.read()
if success:
frame = preprocess_image(frame, width, height)
dataset[video_index].append(frame)
frame_index = video.get(cv2.CAP_PROP_POS_FRAMES)
video_framerate = video.get(cv2.CAP_PROP_FPS)
video.set(cv2.CAP_PROP_POS_FRAMES, frame_index + video_framerate // framerate)
last_frame_index = video.get(cv2.CAP_PROP_FRAME_COUNT)
if frame_index >= last_frame_index:
# Video is over
break
else:
break
dataset[video_index] = np.reshape(dataset[video_index], (-1, width, height, 1))
video_index += 1
return dataset
示例13: video_jump
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def video_jump(video_cap: cv2.VideoCapture, frame_id: int):
# IMPORTANT:
# - frame is a range actually
# - frame 1 's timestamp is the beginning of this frame
#
# video_jump(cap, 2) means: moving the pointer to the start point of frame 2 => the end point of frame 1
# another -1 for re-read
video_cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id - 1 - 1)
video_cap.read()
logger.debug(
f"previous pointer: {get_current_frame_id(video_cap)}({get_current_frame_time(video_cap)})"
)
示例14: get_current_frame_id
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def get_current_frame_id(video_cap: cv2.VideoCapture) -> int:
# IMPORTANT:
# this id is the frame which has already been grabbed
# we jump to 5, which means the next frame will be 5
# so the current frame id is: 5 - 1 = 4
return int(video_cap.get(cv2.CAP_PROP_POS_FRAMES))
示例15: setFramePosition
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CAP_PROP_POS_FRAMES [as 别名]
def setFramePosition(self, framePos):
self.stream.set(cv2.CAP_PROP_POS_FRAMES, framePos)