From e4780174d1ada9fd5f417da47379d4a044e25096 Mon Sep 17 00:00:00 2001 From: weishengyu Date: Wed, 7 Jul 2021 14:30:45 +0800 Subject: [PATCH] decorate open by with --- deploy/paddleserving/image_http_client.py | 6 ++++-- .../image_augmentation/ImageAugment_en.md | 10 ++++++---- .../image_augmentation/ImageAugment.md | 10 ++++++---- ppcls/data/dataloader/vehicle_dataset.py | 3 ++- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/deploy/paddleserving/image_http_client.py b/deploy/paddleserving/image_http_client.py index 3b92091c..d2b90be2 100644 --- a/deploy/paddleserving/image_http_client.py +++ b/deploy/paddleserving/image_http_client.py @@ -23,9 +23,11 @@ py_version = sys.version_info[0] def predict(image_path, server): if py_version == 2: - image = base64.b64encode(open(image_path).read()) + with open(image_path) as f: + image = base64.b64encode(f.read()) else: - image = base64.b64encode(open(image_path, "rb").read()).decode("utf-8") + with open(image_path, "rb") as f: + image = base64.b64encode(f.read()).decode("utf-8") req = json.dumps({"feed": [{"image": image}], "fetch": ["prediction"]}) r = requests.post( server, data=req, headers={"Content-Type": "application/json"}) diff --git a/docs/en/advanced_tutorials/image_augmentation/ImageAugment_en.md b/docs/en/advanced_tutorials/image_augmentation/ImageAugment_en.md index 82861307..52ebfc08 100644 --- a/docs/en/advanced_tutorials/image_augmentation/ImageAugment_en.md +++ b/docs/en/advanced_tutorials/image_augmentation/ImageAugment_en.md @@ -154,10 +154,12 @@ cutout_op = Cutout(n_holes=1, length=112) ops = [decode_op, resize_op, cutout_op] -imgs_dir = image_path -fnames = os.listdir(imgs_dir) -for f in fnames: - data = open(os.path.join(imgs_dir, f)).read() +imgs_dir = "imgs_dir" +file_names = os.listdir(imgs_dir) +for file_name in file_names: + file_path = os.join(imgs_dir, file_name) + with open(file_path) as f: + data = f.read() img = transform(data, ops) ``` diff --git a/docs/zh_CN/advanced_tutorials/image_augmentation/ImageAugment.md b/docs/zh_CN/advanced_tutorials/image_augmentation/ImageAugment.md index 6ee6455f..6278096d 100644 --- a/docs/zh_CN/advanced_tutorials/image_augmentation/ImageAugment.md +++ b/docs/zh_CN/advanced_tutorials/image_augmentation/ImageAugment.md @@ -74,10 +74,12 @@ autoaugment_op = ImageNetPolicy() ops = [decode_op, resize_op, autoaugment_op] -imgs_dir = 图像路径 -fnames = os.listdir(imgs_dir) -for f in fnames: - data = open(os.path.join(imgs_dir, f)).read() +imgs_dir = "imgs_dir" +file_names = os.listdir(imgs_dir) +for file_name in file_names: + file_path = os.join(imgs_dir, file_name) + with open(file_path) as f: + data = f.read() img = transform(data, ops) ``` diff --git a/ppcls/data/dataloader/vehicle_dataset.py b/ppcls/data/dataloader/vehicle_dataset.py index baae63c2..80fc6bb0 100644 --- a/ppcls/data/dataloader/vehicle_dataset.py +++ b/ppcls/data/dataloader/vehicle_dataset.py @@ -61,7 +61,8 @@ class CompCars(Dataset): label_path = os.path.join(self._label_root, l[0].split('.')[0] + '.txt') assert os.path.exists(label_path) - bbox = open(label_path).readlines()[-1].strip().split() + with open(label_path) as f: + bbox = f.readlines()[-1].strip().split() bbox = [int(x) for x in bbox] self.images.append(os.path.join(self._img_root, l[0])) self.labels.append(int(l[1])) -- GitLab