glenn-jocher commited on
Commit
5f97001
β€’
1 Parent(s): 2181ef3

Context manager `open(file) as f` fixes (#7289)

Browse files

* Flask context manager `open()` fix

* Additional read context manager fixes

data/VOC.yaml CHANGED
@@ -72,7 +72,8 @@ download: |
72
  imgs_path.mkdir(exist_ok=True, parents=True)
73
  lbs_path.mkdir(exist_ok=True, parents=True)
74
 
75
- image_ids = open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt').read().strip().split()
 
76
  for id in tqdm(image_ids, desc=f'{image_set}{year}'):
77
  f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path
78
  lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path
 
72
  imgs_path.mkdir(exist_ok=True, parents=True)
73
  lbs_path.mkdir(exist_ok=True, parents=True)
74
 
75
+ with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f:
76
+ image_ids = f.read().strip().split()
77
  for id in tqdm(image_ids, desc=f'{image_set}{year}'):
78
  f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path
79
  lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path
export.py CHANGED
@@ -407,7 +407,8 @@ def export_tfjs(keras_model, im, file, prefix=colorstr('TensorFlow.js:')):
407
  f'--output_node_names="Identity,Identity_1,Identity_2,Identity_3" {f_pb} {f}'
408
  subprocess.run(cmd, shell=True)
409
 
410
- json = open(f_json).read()
 
411
  with open(f_json, 'w') as j: # sort JSON Identity_* in ascending order
412
  subst = re.sub(
413
  r'{"outputs": {"Identity.?.?": {"name": "Identity.?.?"}, '
 
407
  f'--output_node_names="Identity,Identity_1,Identity_2,Identity_3" {f_pb} {f}'
408
  subprocess.run(cmd, shell=True)
409
 
410
+ with open(f_json) as j:
411
+ json = j.read()
412
  with open(f_json, 'w') as j: # sort JSON Identity_* in ascending order
413
  subst = re.sub(
414
  r'{"outputs": {"Identity.?.?": {"name": "Identity.?.?"}, '
models/common.py CHANGED
@@ -378,7 +378,8 @@ class DetectMultiBackend(nn.Module):
378
  return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs))
379
 
380
  gd = tf.Graph().as_graph_def() # graph_def
381
- gd.ParseFromString(open(w, 'rb').read())
 
382
  frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs="Identity:0")
383
  elif tflite or edgetpu: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
384
  try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu
 
378
  return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs))
379
 
380
  gd = tf.Graph().as_graph_def() # graph_def
381
+ with open(w, 'rb') as f:
382
+ gd.ParseFromString(f.read())
383
  frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs="Identity:0")
384
  elif tflite or edgetpu: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
385
  try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu
utils/flask_rest_api/example_request.py CHANGED
@@ -1,12 +1,18 @@
1
- """Perform test request"""
 
 
 
 
2
  import pprint
3
 
4
  import requests
5
 
6
  DETECTION_URL = "http://localhost:5000/v1/object-detection/yolov5s"
7
- TEST_IMAGE = "zidane.jpg"
8
 
9
- image_data = open(TEST_IMAGE, "rb").read()
 
 
10
 
11
  response = requests.post(DETECTION_URL, files={"image": image_data}).json()
12
 
 
1
+ # YOLOv5 πŸš€ by Ultralytics, GPL-3.0 license
2
+ """
3
+ Perform test request
4
+ """
5
+
6
  import pprint
7
 
8
  import requests
9
 
10
  DETECTION_URL = "http://localhost:5000/v1/object-detection/yolov5s"
11
+ IMAGE = "zidane.jpg"
12
 
13
+ # Read image
14
+ with open(IMAGE, "rb") as f:
15
+ image_data = f.read()
16
 
17
  response = requests.post(DETECTION_URL, files={"image": image_data}).json()
18
 
utils/flask_rest_api/restapi.py CHANGED
@@ -1,6 +1,8 @@
 
1
  """
2
  Run a Flask REST API exposing a YOLOv5s model
3
  """
 
4
  import argparse
5
  import io
6
 
 
1
+ # YOLOv5 πŸš€ by Ultralytics, GPL-3.0 license
2
  """
3
  Run a Flask REST API exposing a YOLOv5s model
4
  """
5
+
6
  import argparse
7
  import io
8