Skip to content

Commit 052b089

Browse files
author
Christopher Woodall
committed
close files after reading cocodataset#637
1 parent 8ddbe35 commit 052b089

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

PythonAPI/pycocotools/coco.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def __init__(self, annotation_file=None):
8181
if not annotation_file == None:
8282
print('loading annotations into memory...')
8383
tic = time.time()
84-
dataset = json.load(open(annotation_file, 'r'))
84+
with open(annotation_file, 'r', encoding='utf-8') as openFile:
85+
dataset = json.load(openFile)
8586
assert type(dataset)==dict, 'annotation file format {} not supported'.format(type(dataset))
8687
print('Done (t={:0.2f}s)'.format(time.time()- tic))
8788
self.dataset = dataset
@@ -314,7 +315,8 @@ def loadRes(self, resFile):
314315
print('Loading and preparing results...')
315316
tic = time.time()
316317
if type(resFile) == str or (PYTHON_VERSION == 2 and type(resFile) == unicode):
317-
anns = json.load(open(resFile))
318+
with open(resFile, 'r', encoding='utf-8') as openFile:
319+
anns = json.load(openFile)
318320
elif type(resFile) == np.ndarray:
319321
anns = self.loadNumpyAnnotations(resFile)
320322
else:
@@ -438,4 +440,4 @@ def annToMask(self, ann):
438440
"""
439441
rle = self.annToRLE(ann)
440442
m = maskUtils.decode(rle)
441-
return m
443+
return m

0 commit comments

Comments
 (0)