英文:
Can I use a global variable to parameter another function?
问题
我有一个bbox_sort函数,用于按从左到右的顺序排序坐标。为了考虑两个边界框之间的Y高度,我启动了thresh变量。在get_predict函数中,bbox_sort函数被作为参数传递给cmp_to_key。我需要使thresh成为可选参数。
我知道使用全局变量是不良实践,但我不知道如何将thresh参数传递给bbox_sort函数。将数值thresh写入一个单独的文件并从那里获取它是否是良好实践,或者我应该如何重新制作排序函数?
英文:
I have a bbox_sort function to sort the coordinates in order from left to right. To take into account the Y height between the two bounding bbox, I start the thresh variable. In get_predict , the bbox_sort function is passed as an argument to cmp_to_key. I need to make optional parameter thresh
I know it's bad practice to use global variables, but I don't know how else to pass the thresh parameter to the bbox_sort function. Is it good practice to write the number thresh to a separate file and get it from there or how can I remake the sort function?
from functools import cmp_to_key
def bbox_sort(a, b,):
bbox_a = a
bbox_b = b
if abs(bbox_a[1] - bbox_b[1]) <= thresh:
return bbox_a[0] - bbox_b[0]
return bbox_a[1] - bbox_b[1]
def get_prediction(result):
coord_list = []
res = result.to_coco_annotations()
global thresh
thresh = 10
for ann in res:
current_bbox = ann['bbox']
x = current_bbox[0]
y = current_bbox[1]
w = current_bbox[2]
h = current_bbox[3]
coord_list.append((x, y, w, h))
cnts = sorted(coord_list, key=cmp_to_key(bbox_sort))
for pred in range(len(res)-1):
res[pred]['image_id'] = cnts.index(tuple(res[pred]['bbox']))
return res
答案1
得分: 1
你可以创建一个名为 `create_bbox_sort` 的函数,它会根据传递给 `create_bbox_sort` 的 `thresh` 参数动态创建一个 `bbox_sort` 函数。这样,你就不需要使用全局变量,而且 `create_bbox_sort` 函数可以轻松地在其他地方和其他脚本中使用。这类似于 [带属性的装饰器][1] 的工作方式。
新代码如下:
```python
from functools import cmp_to_key
def create_bbox_sort(thresh):
def bbox_sort(a, b,):
bbox_a = a
bbox_b = b
if abs(bbox_a[1] - bbox_b[1]) <= thresh:
return bbox_a[0] - bbox_b[0]
return bbox_a[1] - bbox_b[1]
return bbox_sort
def get_prediction(result):
coord_list = []
res = result.to_coco_annotations()
thresh = 10
for ann in res:
current_bbox = ann['bbox']
x = current_bbox[0]
y = current_bbox[1]
w = current_bbox[2]
h = current_bbox[3]
coord_list.append((x, y, w, h))
cnts = sorted(coord_list, key=cmp_to_key(create_bbox_sort(thresh)))
for pred in range(len(res)-1):
res[pred]['image_id'] = cnts.index(tuple(res[pred]['bbox']))
return res
<details>
<summary>英文:</summary>
You can create a `create_bbox_sort` function which creates a `bbox_sort` function on the fly using the `thresh` passed as a parameter to `create_bbox_sort`. This way, you don't need a global variable and the `create_bbox_sort` function can easily be used elsewhere and in other scripts. This is similar to the way [decorators with attributes][1] work.
The new code is :
```python
from functools import cmp_to_key
def create_bbox_sort(thresh):
def bbox_sort(a, b,):
bbox_a = a
bbox_b = b
if abs(bbox_a[1] - bbox_b[1]) <= thresh:
return bbox_a[0] - bbox_b[0]
return bbox_a[1] - bbox_b[1]
return bbox_sort
def get_prediction(result):
coord_list = []
res = result.to_coco_annotations()
thresh = 10
for ann in res:
current_bbox = ann['bbox']
x = current_bbox[0]
y = current_bbox[1]
w = current_bbox[2]
h = current_bbox[3]
coord_list.append((x, y, w, h))
cnts = sorted(coord_list, key=cmp_to_key(create_bbox_sort(thresh)))
for pred in range(len(res)-1):
res[pred]['image_id'] = cnts.index(tuple(res[pred]['bbox']))
return res
答案2
得分: 0
If your threshold is a global variable only configurable by the developer it's common practice to define it on top of the file written in all caps THRESHOLD = 10
right under your imports.
In python you only need to use global ...
when you edit a global variable inside a function. (Which you don't plan on doing here, I assume.)
If the threshold isn't constant, and you want to be able to change it at runtime, you can wrap your function in a lambda like that:
cnts = sorted(coord_list, key=cmp_to_key(lambda a,b: bbox_sort(a, b, thresh)))
And then add a threshold to your arguments:
def bbox_sort(a, b, threshold):
英文:
If your threshold is a global variable only configurable by the developer it's common practice to define it on top of the file written in all caps THRESHOLD = 10
right under your imports.
In python you only need to use global ...
when you edit a global variable inside a function. (Which you don't plan on doing here, I assume.)
If the threshold isn't constant, and you want to be able to change it at runtime, you can wrap your function in a lambda like that:
cnts = sorted(coord_list, key=cmp_to_key(lambda a,b: bbox_sort(a, b, thresh)))
And then add a threshold to your arguments:
def bbox_sort(a, b, threshold):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论