我在运行darknet.py提供的detect_image(...)时遇到了内存泄漏。我在没完没了的while循环中检测对象。我使用的是Ubuntu20.04、Python3.8.10、OpenCV 4.5.2和Cuda10.2。
发布于 2021-10-17 04:33:30
darknet.py已经有一个函数来处理这个问题,也就是free_image(image)。由于某些原因,这不会在函数detect_image(...)中调用。我已经在free_detections(detections, num)下面添加了这个,内存泄漏已经解决了。下面是确切的代码:
def detect_image(network, class_names, image_path, thresh=.5, hier_thresh=.5, nms=.45):
"""
Returns a list with highest confidence class and their bbox
"""
pnum = pointer(c_int(0))
image = load_image(image_path,0,0)
predict_image(network, image)
detections = get_network_boxes(network, image.w, image.h,
thresh, hier_thresh, None, 0, pnum, 0)
num = pnum[0]
if nms:
do_nms_sort(detections, num, len(class_names), nms)
predictions = remove_negatives(detections, class_names, num)
predictions = decode_detection(predictions)
free_detections(detections, num)
free_image(image) # this was missing...
return sorted(predictions, key=lambda x: x[1])```https://stackoverflow.com/questions/69601339
复制相似问题