合并计算有时会无故超时

2024-05-23 16:18:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在执行池操作以执行并行任务。这些任务是基于openCV的关键点提取,使用ORB方法对源图像和其他3个不同图像之间的相似性进行评分

我可以执行池函数一次。它起作用了

如果我在通话后没有执行任何其他操作,我可以再次拨打电话,然后它会再次工作

执行时间总是相同的,大约一秒钟,CPU负载行为也是如此。4芯负载高达100%,一秒钟后下降

现在,如果我在这两个相同的调用之间执行类似于图像旋转的操作,即使这个图像没有被我的池函数使用,它也不再工作

调用时CPU不会加载,池函数最终将在100秒后超时

以下是池函数的代码:

def compute_pooled(source, img1, img2, img3, n_features, scale_factor, n_levels, lowe_ratio):

    pool = Pool()

    image_same        = same
    image_similar     = similar
    image_different   = different
    #confidence = get_match_confidence(image, img2)

    args_same = [image_same,source, n_features, scale_factor, n_levels, lowe_ratio]
    args_similar = [image_similar,source, n_features, scale_factor, n_levels, lowe_ratio]
    args_different = [image_different,source, n_features, scale_factor, n_levels, lowe_ratio]

    result_same = pool.apply_async(match_with_orb, args_same)
    result_similar = pool.apply_async(match_with_orb, args_similar)
    result_different = pool.apply_async(match_with_orb, args_different)

    good_same = result_same.get(timeout=100)
    good_similar = result_similar.get(timeout=100)
    good_different = result_different.get(timeout=100)

    values = [good_same, good_similar, good_different]

    ## I tried closing, terminating, not doing anything..
    pool.close()
    return values

下面是每个池中被调用函数的代码:

def match_with_orb(img1, img2, n_features, scale_factor, n_levels, lowe_ratio):
    orb = cv2.ORB_create(nfeatures=n_features, scaleFactor=scale_factor, nlevels=n_levels)
    keypoints_orb1, descriptors1 = orb.detectAndCompute(img1, None)
    keypoints_orb2, descriptors2 = orb.detectAndCompute(img2, None)

    bf = cv2.BFMatcher()

    matches = bf.knnMatch(descriptors1,descriptors2, k=2)
    method='orb'

    good = []

    for m,n in matches:
        if m.distance < lowe_ratio*n.distance:
            good.append([m])

    return len(good)

如果我执行以下指令,一切正常:

import numpy as np
import cv2
from matplotlib import pyplot as plt
from pathlib import Path
from multiprocessing import Pool, cpu_count

imgname_source = 'source.jpg'
img1 = 'img1.jpg'
img2 = 'img2.jpg'
img3 = 'img3.jpg'

dirPath = '/home/path/to/imgs/'

source     = cv2.imread(dirPath+imgname_source,0)
_img1       = cv2.imread(dirPath+img1,0)
_img2    = cv2.imread(dirPath+img2,0)
_img3  = cv2.imread(dirPath+img3,0)

compute_pooled(source, _img1, _img2, _img3, 5000, 1.15, 16, 0.67)

在100%cpu负载下,它在不到一秒钟的时间内自动执行。如果我再次调用compute_pooled函数,它的工作方式也是一样的

现在,使用以下功能:

def rotate(image, angle, center=None, scale=1.0):
    (h, w) = image.shape[:2]
    if center is None:
        center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w, h))
    return rotated

如果我在两个compute_池调用之间调用它,即使在当前使用的映像之外的其他映像上,第二个compute_池调用也不会工作

## First call works
compute_pooled(source, _img1, _img2, _img3, 5000, 1.15, 16, 0.67) 
_img4 = rotate(_img1, 90)
## Second call times out
compute_pooled(source, _img1, _img2, _img3, 5000, 1.15, 16, 0.67) 

这里的问题是什么?我完全不明白为什么第二次调用没有进行任何计算就超时了

这里是错误。无法从第一个.get()调用中检索任何结果时超时

--> 223     good_same = result_same.get(timeout=100)
    224     good_similar = result_similar.get(timeout=100)
    225     good_different = result_different.get(timeout=100)

~/anaconda3/lib/python3.7/multiprocessing/pool.py in get(self, timeout)
    651         self.wait(timeout)
    652         if not self.ready():
--> 653             raise TimeoutError
    654         if self._success:
    655             return self._value

TimeoutError: 


Tags: imagesourcegettimeoutresultcv2img1same