From 5f97001ed4e5deb5c92eb200a79b5cb9da861130 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Tue, 5 Apr 2022 12:54:25 +0200 Subject: [PATCH] Context manager `open(file) as f` fixes (#7289) * Flask context manager `open()` fix * Additional read context manager fixes --- data/VOC.yaml | 3 ++- export.py | 3 ++- models/common.py | 3 ++- utils/flask_rest_api/example_request.py | 12 +++++++++--- utils/flask_rest_api/restapi.py | 2 ++ 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/data/VOC.yaml b/data/VOC.yaml index be04fb1..9865967 100644 --- a/data/VOC.yaml +++ b/data/VOC.yaml @@ -72,7 +72,8 @@ download: | imgs_path.mkdir(exist_ok=True, parents=True) lbs_path.mkdir(exist_ok=True, parents=True) - image_ids = open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt').read().strip().split() + with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f: + image_ids = f.read().strip().split() for id in tqdm(image_ids, desc=f'{image_set}{year}'): f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path diff --git a/export.py b/export.py index c0b98ce..df4f3b6 100644 --- a/export.py +++ b/export.py @@ -407,7 +407,8 @@ def export_tfjs(keras_model, im, file, prefix=colorstr('TensorFlow.js:')): f'--output_node_names="Identity,Identity_1,Identity_2,Identity_3" {f_pb} {f}' subprocess.run(cmd, shell=True) - json = open(f_json).read() + with open(f_json) as j: + json = j.read() with open(f_json, 'w') as j: # sort JSON Identity_* in ascending order subst = re.sub( r'{"outputs": {"Identity.?.?": {"name": "Identity.?.?"}, ' diff --git a/models/common.py b/models/common.py index dcd3e5f..5a83bce 100644 --- a/models/common.py +++ b/models/common.py @@ -378,7 +378,8 @@ class DetectMultiBackend(nn.Module): return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs)) gd = tf.Graph().as_graph_def() # graph_def - gd.ParseFromString(open(w, 'rb').read()) + with open(w, 'rb') as f: + gd.ParseFromString(f.read()) frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs="Identity:0") elif tflite or edgetpu: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu diff --git a/utils/flask_rest_api/example_request.py b/utils/flask_rest_api/example_request.py index ff21f30..773ad89 100644 --- a/utils/flask_rest_api/example_request.py +++ b/utils/flask_rest_api/example_request.py @@ -1,12 +1,18 @@ -"""Perform test request""" +# YOLOv5 🚀 by Ultralytics, GPL-3.0 license +""" +Perform test request +""" + import pprint import requests DETECTION_URL = "http://localhost:5000/v1/object-detection/yolov5s" -TEST_IMAGE = "zidane.jpg" +IMAGE = "zidane.jpg" -image_data = open(TEST_IMAGE, "rb").read() +# Read image +with open(IMAGE, "rb") as f: + image_data = f.read() response = requests.post(DETECTION_URL, files={"image": image_data}).json() diff --git a/utils/flask_rest_api/restapi.py b/utils/flask_rest_api/restapi.py index 38868cc..62adb4b 100644 --- a/utils/flask_rest_api/restapi.py +++ b/utils/flask_rest_api/restapi.py @@ -1,6 +1,8 @@ +# YOLOv5 🚀 by Ultralytics, GPL-3.0 license """ Run a Flask REST API exposing a YOLOv5s model """ + import argparse import io