影刀RPA | 获取两张图片的相似度

需要本机有足够的内存空间,否则会抛出异常

Posted by Haauleon on January 18, 2024

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 使用提醒:
# 1. xbot包提供软件自动化、数据表格、Excel、日志、AI等功能
# 2. package包提供访问当前应用数据的功能,如获取元素、访问全局变量、获取资源文件等功能
# 3. 当此模块作为流程独立运行时执行main函数
# 4. 可视化流程中可以通过"调用模块"的指令使用此模块

import xbot
import xbot_visual
from xbot import print, sleep
from .import package
from .package import variables as glv


# 全局变量
SAVE_PATH = glv['IMAGE_FOLDER']


def image_download(image_url):
    """网络图片下载至本地并返回图片存放的绝对路径"""
    image_path = xbot_visual.web_service.download(url=image_url, save_folder=SAVE_PATH, custom_filename=False, save_filename=None, wait_complete_timeout="3000", connect_timeout_seconds="3000", send_by_web=False, browser=None)
    # xbot_visual.programing.log(type="info", text=f"图片已成功下载至 >>> {image_path}")
    return image_path


def get_image_similarity(img1_path, img2_path):
    """获取两张图片的相似度"""
    for _ in range(20):
        try:
            similarity = xbot_visual.process.run(process="xbot_extensions.shadowbot_picture.process18", package=__name__, inputs={
                "img1_path": img1_path,
                "img2_path": img2_path,
                }, outputs=[
                "similarity",
            ])
            xbot_visual.programing.log(type="info", text=f"相似度:{similarity}")
            return similarity
        except: 
            sleep(10)
            pass


def is_same_image(img1_path, img2_path):
    """判断是否是同一张图片
    如果相似度 >=0.50 则视为相同图片
    如果相似度 <0.50  则视为不同图片
    """
    is_same_image = False
    if get_image_similarity(img1_path, img2_path) >= 0.50:
        is_same_image = True
    return is_same_image


def main(args):
    if is_same_image(image_download('https://m.media-amazon.com/images/I/71oYTB6NM0L.jpg'), 
                     image_download('https://m.media-amazon.com/images/I/71oYTB6NM0L.jpg')):
        print('相同的图片')
    else:
        print('不同的图片')