Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-ASCII file paths for images #1364

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion easyocr/DBNet/DBNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def get_cv2_image(self, image):
'''
if isinstance(image, str):
if os.path.isfile(image):
image = cv2.imread(image, cv2.IMREAD_COLOR).astype('float32')
image = cv2.imdecode(np.fromfile(image.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_COLOR).astype('float32')
else:
raise FileNotFoundError("Cannot find {}".format(image))
elif isinstance(image, np.ndarray):
Expand Down
2 changes: 1 addition & 1 deletion easyocr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ def reformat_input(image):
img_cv_grey = cv2.imread(tmp, cv2.IMREAD_GRAYSCALE)
os.remove(tmp)
else:
img_cv_grey = cv2.imread(image, cv2.IMREAD_GRAYSCALE)
img_cv_grey = cv2.imdecode(np.fromfile(image.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_GRAYSCALE) # numpy can handle non-ASCII file paths
image = os.path.expanduser(image)
img = loadImage(image) # can accept URL
elif type(image) == bytes:
Expand Down
12 changes: 6 additions & 6 deletions trainer/craft/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def dilate_img_to_output_size(self, image, char_bbox):

def make_gt_score(self, index):
img_path = os.path.join(self.data_dir, self.img_names[index][0])
image = cv2.imread(img_path, cv2.IMREAD_COLOR)
image = cv2.imdecode(np.fromfile(img_path.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
all_char_bbox = self.char_bbox[index].transpose(
(2, 1, 0)
Expand Down Expand Up @@ -388,7 +388,7 @@ def load_img_gt_box(self, img_gt_box_path):
def load_data(self, index):
img_name = self.img_names[index]
img_path = os.path.join(self.img_dir, img_name)
image = cv2.imread(img_path)
image = cv2.imdecode(np.fromfile(img_path.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

img_gt_box_path = os.path.join(
Expand Down Expand Up @@ -491,7 +491,7 @@ def load_saved_gt_score(self, index):
"""
img_name = self.img_names[index]
img_path = os.path.join(self.img_dir, img_name)
image = cv2.imread(img_path)
image = cv2.imdecode(np.fromfile(img_path.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

img_gt_box_path = os.path.join(
Expand All @@ -512,9 +512,9 @@ def load_saved_gt_score(self, index):
saved_cf_mask_path = os.path.join(
self.saved_gt_dir, f"res_img_{query_idx}_cf_mask_thresh_0.6.jpg"
)
region_score = cv2.imread(saved_region_scores_path, cv2.IMREAD_GRAYSCALE)
affinity_score = cv2.imread(saved_affi_scores_path, cv2.IMREAD_GRAYSCALE)
confidence_mask = cv2.imread(saved_cf_mask_path, cv2.IMREAD_GRAYSCALE)
region_score = cv2.imdecode(np.fromfile(saved_region_scores_path.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
affinity_score = cv2.imdecode(np.fromfile(saved_affi_scores_path.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
confidence_mask = cv2.imdecode(np.fromfile(saved_cf_mask_path.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_GRAYSCALE)

region_score = cv2.resize(region_score, (img_w, img_h))
affinity_score = cv2.resize(affinity_score, (img_w, img_h))
Expand Down
2 changes: 1 addition & 1 deletion trainer/craft/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def main_eval(model_path, backbone, config, evaluator, result_dir, buffer, model
# -----------------------------------------------------------------------------------------------------------------#
total_imgs_bboxes_pre = []
for k, img_path in enumerate(tqdm(piece_imgs_path)):
image = cv2.imread(img_path)
image = cv2.imdecode(np.fromfile(img_path.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
single_img_bbox = []
bboxes, polys, score_text = test_net(
Expand Down
3 changes: 1 addition & 2 deletions trainer/craft/utils/inference_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def load_icdar2015_gt(dataFolder, isTraing=False):
.replace(".txt", ".jpg")
.replace("gt_", "")
)
image = cv2.imread(img_path)
image = cv2.imdecode(np.fromfile(img_path.replace('\\','/'), dtype=np.uint8), cv2.IMREAD_COLOR)
lines = open(gt_path, encoding="utf-8").readlines()
single_img_bboxes = []
for line in lines:
Expand Down Expand Up @@ -270,7 +270,6 @@ def load_icdar2013_gt(dataFolder, isTraing=False):
.replace(".txt", ".jpg")
.replace("gt_", "")
)
image = cv2.imread(img_path)
lines = open(gt_path, encoding="utf-8").readlines()
single_img_bboxes = []
for line in lines:
Expand Down
4 changes: 2 additions & 2 deletions unit_test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This module can be used as a typical python module. One python wrapper script an
### Python script (*recommneded*)
The script can be called with (assuming calling from `EasyOCR/`);
```
python ./unit_test/run_unit_test.py --easyocr ./easyocr --verbose 2 --test ./unit_test/EasyOcrUnitTestPackage.pickle --data_dir ./examples
python ./unit_test/run_unit_test.py --easyocr ./easyocr --verbose 2 --test_data ./unit_test/data/EasyOcrUnitTestPackage.pickle --image_data_dir ./examples
```

#### Script arguments
Expand All @@ -21,7 +21,7 @@ python ./unit_test/run_unit_test.py --easyocr ./easyocr --verbose 2 --test ./uni
* 3: Same as 2 and also the calculated and the expected outputs of each test.
* 4 or higher: Same as 3 and also the inputs of each test. (This will produce a lot of text on console).
* test_data (-t): [Optional] Path to test package to use (The default is `./unit_test/data/EasyOcrUnitTestPackage.pickle`).
* data_dir (-d): [Optional] Path to EasyOCR example images directory. (The default is `./examples/`
* image_data_dir (-d): [Optional] Path to EasyOCR example images directory. (The default is `./examples`)

### Ipython notebook
Please see `demo.ipynb` for documentation.
4 changes: 2 additions & 2 deletions unit_test/run_unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def main(args):
parser = argparse.ArgumentParser(description="Script to run EasyOCR unit tet.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--easyocr", help="Directory of EasyOCR to test.")
parser.add_argument("-t", "--test_data", default="./data/EasyOcrUnitTestPackage.pickle", help="Path to test data.")
parser.add_argument("-d", "--image_data_dir", default="../examples", help="Path to directory that contains EasyOCR example images.")
parser.add_argument("-t", "--test_data", default="./unit_test/data/EasyOcrUnitTestPackage.pickle", help="Path to test data.")
parser.add_argument("-d", "--image_data_dir", default="./examples", help="Path to directory that contains EasyOCR example images.")
parser.add_argument("-v", "--verbose", default=0, type = int, help="Verbosity level of report.")
args = parser.parse_args()
main(args)