diff --git a/deploy/paddleserving/image_http_client.py b/deploy/paddleserving/image_http_client.py index 3b92091c659613c83e4423a3f22b0d4d20321f43..4e33c4a7e4bb60b2937b9a0073cb16c74fe4d911 100644 --- a/deploy/paddleserving/image_http_client.py +++ b/deploy/paddleserving/image_http_client.py @@ -22,10 +22,9 @@ py_version = sys.version_info[0] def predict(image_path, server): - if py_version == 2: - image = base64.b64encode(open(image_path).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 8286130749081e5d50038b7c07b8a20d578c7d9f..52ebfc08955c9457e9ffe1fa1445b3bd35c74039 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 6ee6455fa8d24b4610c8d364031ab5e7c075807c..6278096d8ff96193835d0970a75440331c760ecc 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 baae63c257cd51f91b0d9d82894e0027d3d0d2df..80fc6bb08d6e2458ca2604dc91933b4a28971d2f 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]))