From f63e6ab4df14efcb39206c2b5435da5d755b1c78 Mon Sep 17 00:00:00 2001 From: Richard Brown <33289025+rijobro@users.noreply.github.com> Date: Wed, 29 Mar 2023 13:30:34 +0100 Subject: [PATCH] close files after reading --- PythonAPI/pycocotools/coco.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/PythonAPI/pycocotools/coco.py b/PythonAPI/pycocotools/coco.py index e4182c74..ee96d100 100644 --- a/PythonAPI/pycocotools/coco.py +++ b/PythonAPI/pycocotools/coco.py @@ -81,7 +81,8 @@ def __init__(self, annotation_file=None): if not annotation_file == None: print('loading annotations into memory...') tic = time.time() - dataset = json.load(open(annotation_file, 'r')) + with open(annotation_file, 'r', encoding='utf-8') as openFile: + dataset = json.load(openFile) assert type(dataset)==dict, 'annotation file format {} not supported'.format(type(dataset)) print('Done (t={:0.2f}s)'.format(time.time()- tic)) self.dataset = dataset @@ -314,7 +315,8 @@ def loadRes(self, resFile): print('Loading and preparing results...') tic = time.time() if type(resFile) == str or (PYTHON_VERSION == 2 and type(resFile) == unicode): - anns = json.load(open(resFile)) + with open(resFile, 'r', encoding='utf-8') as openFile: + anns = json.load(openFile) elif type(resFile) == np.ndarray: anns = self.loadNumpyAnnotations(resFile) else: @@ -438,4 +440,4 @@ def annToMask(self, ann): """ rle = self.annToRLE(ann) m = maskUtils.decode(rle) - return m \ No newline at end of file + return m