优化opencv函数

2024-04-23 22:49:50 发布

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

你能给我复习一下代码吗。有没有办法减少函数的运行时间?现在平均工作时间约为3秒。你知道吗

def searchCards(screen_area, deck, list_length):
    hand = ''
    threshold = 0.98
    for value in deck:
    try:
        path = getLastScreen(screen_area)
        path = path[0]['image_path']
        img_rgb = cv2.imread(path, 0)
        template = cv2.imread(str(value['image_path']), 0)
        res = cv2.matchTemplate(img_rgb, template, cv2.TM_CCOEFF_NORMED)
        loc = np.where(res >= threshold)
        if len(loc[0]) != 0:
            hand += value['alias']
        if len(hand) == list_length:
            return hand
    except Exception as e:
        error_log.errorLog('searchCards', str(e))
        print(e)
if len(hand) < 4:
    hand = '72o'
return hand

Tags: pathimagethresholdlenifvalue时间area