可以使用全局变量来作为另一个函数的参数吗?

huangapple go评论101阅读模式
英文:

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?

  1. from functools import cmp_to_key
  2. def bbox_sort(a, b,):
  3. bbox_a = a
  4. bbox_b = b
  5. if abs(bbox_a[1] - bbox_b[1]) <= thresh:
  6. return bbox_a[0] - bbox_b[0]
  7. return bbox_a[1] - bbox_b[1]
  8. def get_prediction(result):
  9. coord_list = []
  10. res = result.to_coco_annotations()
  11. global thresh
  12. thresh = 10
  13. for ann in res:
  14. current_bbox = ann['bbox']
  15. x = current_bbox[0]
  16. y = current_bbox[1]
  17. w = current_bbox[2]
  18. h = current_bbox[3]
  19. coord_list.append((x, y, w, h))
  20. cnts = sorted(coord_list, key=cmp_to_key(bbox_sort))
  21. for pred in range(len(res)-1):
  22. res[pred]['image_id'] = cnts.index(tuple(res[pred]['bbox']))
  23. return res

答案1

得分: 1

  1. 你可以创建一个名为 `create_bbox_sort` 的函数它会根据传递给 `create_bbox_sort` `thresh` 参数动态创建一个 `bbox_sort` 函数这样你就不需要使用全局变量而且 `create_bbox_sort` 函数可以轻松地在其他地方和其他脚本中使用这类似于 [带属性的装饰器][1] 的工作方式
  2. 新代码如下
  3. ```python
  4. from functools import cmp_to_key
  5. def create_bbox_sort(thresh):
  6. def bbox_sort(a, b,):
  7. bbox_a = a
  8. bbox_b = b
  9. if abs(bbox_a[1] - bbox_b[1]) <= thresh:
  10. return bbox_a[0] - bbox_b[0]
  11. return bbox_a[1] - bbox_b[1]
  12. return bbox_sort
  13. def get_prediction(result):
  14. coord_list = []
  15. res = result.to_coco_annotations()
  16. thresh = 10
  17. for ann in res:
  18. current_bbox = ann['bbox']
  19. x = current_bbox[0]
  20. y = current_bbox[1]
  21. w = current_bbox[2]
  22. h = current_bbox[3]
  23. coord_list.append((x, y, w, h))
  24. cnts = sorted(coord_list, key=cmp_to_key(create_bbox_sort(thresh)))
  25. for pred in range(len(res)-1):
  26. res[pred]['image_id'] = cnts.index(tuple(res[pred]['bbox']))
  27. return res
  1. <details>
  2. <summary>英文:</summary>
  3. 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&#39;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.
  4. The new code is :
  5. ```python
  6. from functools import cmp_to_key
  7. def create_bbox_sort(thresh):
  8. def bbox_sort(a, b,):
  9. bbox_a = a
  10. bbox_b = b
  11. if abs(bbox_a[1] - bbox_b[1]) &lt;= thresh:
  12. return bbox_a[0] - bbox_b[0]
  13. return bbox_a[1] - bbox_b[1]
  14. return bbox_sort
  15. def get_prediction(result):
  16. coord_list = []
  17. res = result.to_coco_annotations()
  18. thresh = 10
  19. for ann in res:
  20. current_bbox = ann[&#39;bbox&#39;]
  21. x = current_bbox[0]
  22. y = current_bbox[1]
  23. w = current_bbox[2]
  24. h = current_bbox[3]
  25. coord_list.append((x, y, w, h))
  26. cnts = sorted(coord_list, key=cmp_to_key(create_bbox_sort(thresh)))
  27. for pred in range(len(res)-1):
  28. res[pred][&#39;image_id&#39;] = cnts.index(tuple(res[pred][&#39;bbox&#39;]))
  29. 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):

huangapple
  • 本文由 发表于 2023年2月23日 19:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定