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

close files after reading #637

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions PythonAPI/pycocotools/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -438,4 +440,4 @@ def annToMask(self, ann):
"""
rle = self.annToRLE(ann)
m = maskUtils.decode(rle)
return m
return m