Skip to content

Commit

Permalink
fix: replace cv2.imread with toolbox.imread
Browse files Browse the repository at this point in the history
cv2.imread will not raise error, even if the image is not existed
  • Loading branch information
williamfzc committed Sep 9, 2019
1 parent cd662c9 commit f360212
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion stagesepx/classifier/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def read(self, *args, **kwargs):

@staticmethod
def read_from_path(data: typing.List[pathlib.Path], *_, **__):
return (cv2.imread(each.as_posix()) for each in data)
return (toolbox.imread(each.as_posix()) for each in data)

def read_from_list(self, data: typing.List[int], video_cap: cv2.VideoCapture = None, *_, **__):
cur_frame_id = toolbox.get_current_frame_id(video_cap)
Expand Down
2 changes: 1 addition & 1 deletion stagesepx/classifier/svm.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def predict(self, pic_path: str) -> str:
:param pic_path:
:return:
"""
pic_object = cv2.imread(pic_path)
pic_object = toolbox.imread(pic_path)
return self.predict_with_object(pic_object)

def predict_with_object(self, pic_object: np.ndarray) -> str:
Expand Down
2 changes: 1 addition & 1 deletion stagesepx/cutter/cut_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def contain_image(self,
if image_path:
logger.debug(f'found image path, use it first: {image_path}')
assert os.path.isfile(image_path), f'image {image_path} not existed'
image_object = cv2.imread(image_path)
image_object = toolbox.imread(image_path)
image_object = toolbox.turn_grey(image_object)

# TODO use client or itself..?
Expand Down
13 changes: 7 additions & 6 deletions stagesepx/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random
import typing
import math
import os
import numpy as np
from loguru import logger
from functools import wraps
Expand Down Expand Up @@ -60,6 +61,12 @@ def get_current_frame_time(video_cap: cv2.VideoCapture) -> float:
return video_cap.get(cv2.CAP_PROP_POS_MSEC) / 1000


def imread(img_path: str, *_, **__) -> np.ndarray:
""" wrapper of cv2.imread """
assert os.path.isfile(img_path), f'file {img_path} is not existed'
return cv2.imread(img_path, *_, **__)


def get_frame_time(video_cap: cv2.VideoCapture, frame_id: int, recover: bool = None) -> float:
cur = get_current_frame_id(video_cap)
video_jump(video_cap, frame_id)
Expand Down Expand Up @@ -238,9 +245,3 @@ def _wrapper(*args, **kwargs):
return func(*args, **kwargs)

return _wrapper


if __name__ == '__main__':
t = cv2.imread('../1.png', cv2.IMREAD_GRAYSCALE)
t = sharpen_frame(t)
cv2.imwrite('a.png', t)

0 comments on commit f360212

Please sign in to comment.