TIKA 2400 : Changes to video_util.py
# Fix formatting issues
# Remove unused imports
# limitations under the License.
import cv2
-import os
-import sys
import ntpath
import numpy as np
@@ -30,11 +28,13 @@ if hasattr(cv2,"cv"):
else:
CV_FRAME_COUNT = cv2.CAP_PROP_FRAME_COUNT
+
def _get_image_from_array(image_array):
# JPG to support tensorflow
byte_arr = cv2.imencode(".jpg", image_array)[1]
return "".join(map(chr, byte_arr))
+
def _path_leaf(path):
"""
Returns file name from path. Path should not end with slash(/)
@@ -42,6 +42,7 @@ def _path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
+
def get_center_frame(video_path):
"""
Traverse till half of video and saves center snapshot
@@ -54,12 +55,13 @@ def get_center_frame(video_path):
success, image = cap.read()
count = 0
- while(success and count < length/2):
+ while success and count < length / 2:
success, image = cap.read()
count += 1
return _get_image_from_array(image)
+
def get_frames_interval(video_path, frame_interval):
"""
Selects one frames after every frame_interval
@@ -74,7 +76,7 @@ def get_frames_interval(video_path, frame_interval):
count = 0
image_arr = []
- while(success and count < length):
+ while success and count < length:
success, image = cap.read()
if count % frame_interval == 0:
image = _get_image_from_array(image)
@@ -84,6 +86,7 @@ def get_frames_interval(video_path, frame_interval):
return image_arr
+
def get_n_frames(video_path, num_frame):
"""
Get N frames equidistant to each other in a video
@@ -100,14 +103,12 @@ def get_n_frames(video_path, num_frame):
count = 0
image_arr = []
- while(success and count < length):
+ while success and count < length:
success, image = cap.read()
if success and count in op_frame_idx:
-
image = _get_image_from_array(image)
image_arr.append(image)
count += 1
return image_arr
-
