def _compute_3d_dct_opencv(self, file):
r"""
Runs much faster than the menpo-based one
but usually opencv is distributed without video support (compile flag)
and is harder to set up
Works fine with the opencv package from arch linux repos
in which case the system Python has to be used
:param file:
:return:
"""
cap = cv2.VideoCapture(file)
vidframes = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
rois = self._read_roi_file(file)
totalframes = rois.shape[0]
if totalframes != vidframes:
print('Roi Frames: %d\n' % totalframes)
print('Vid Frames: %d\n' % vidframes)
raise Exception('Mismatch between the actual number of video frames and the provided ROI _labels')
dct_seq = np.zeros((totalframes, self._yres, self._xres),
dtype=np.float32) # _yres goes first since numpy indexing is rows-first
this_frame = 0
while cap.isOpened():
ret, frame = cap.read()
if ret is False:
break
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
gray_roi = _crop_roi(gray, rois[this_frame, :])
resized = self._resize_frame(gray_roi)
dctmat = np.zeros(np.shape(resized))
cv2.dct(resized, dctmat)
dct_seq[this_frame, :, :] = dctmat
this_frame += 1
return dct_seq
评论列表
文章目录