当前位置: 首页 > article >正文

python简单图像识别自动化工具

import pyautogui
import cv2
import numpy as np
import time

"""
小型自动化工具
图片识别
自动化简单工具
"""

# -*- coding:utf-8 -*-
# #cv读取中文有点问题 需要修改库里使用编码为utf-8
# 获取图像数据匹配全屏寻找
class Mouse():
    def __init__(self, address):  # 创建Mouse的时候加入图片地址参数
        time.sleep(0.5)
        self.address = address
        img = pyautogui.screenshot(region=[0, 0, pyautogui.size().width, pyautogui.size().height])  # x,y,w,h
        img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)  # cvtColor用于在图像中不同的色彩空间进行转换,用于后续处理。
        template = cv2.imread(self.address, cv2.COLOR_RGB2BGR)
        res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        self.max_loc = max_loc
        self.template = template
        self.X = max_loc[0] + template.shape[1] / 2
        self.Y = max_loc[1] + template.shape[0] / 2
        self.max_val = max_val
        self.img = img

    # 移动鼠标
    def move(self, val=0.85, x=0, y=0):  # x,y为偏移量
        if self.max_val > val:
            pyautogui.moveTo(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)

    # 获取匹配度
    def value(self):
        print("匹配度:", self.max_val)

    # 进行点击
    def click(self, val=0.85, x=0, y=0):  # x,y为偏移量
        if self.max_val > val:
            pyautogui.click(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)

    # 进行双击
    def doubleClick(self, val=0.85, x=0, y=0):  # x,y为偏移量
        """
        :param val: 匹配度
        :param x:   x轴偏移    左负右正
        :param y:   y轴偏移    上加下减
        :return:
        """
        if self.max_val > val:
            pyautogui.doubleClick(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)

    # 进行显示
    def show(self, val=0.85):
        if self.max_val > val:
            right_bottom = (self.max_loc[0] + self.template.shape[1], self.max_loc[1] + self.template.shape[0])  # 右下角
            cv2.rectangle(self.img, self.max_loc, right_bottom, 255, 2)  # 画出矩形位置
            cv2.imshow("output", self.img)
            cv2.waitKey(0)

# 写入
def write(string):
    pyautogui.write(string)

# 删除
def delete():
    pyautogui.hotkey('Ctrl', 'a')
    pyautogui.press('delete')

# 点击
def click(x, y, duration=0.2):
    pyautogui.click(x, y, duration=duration, tween=pyautogui.easeInOutQuad)

# 移动
def move(x, y, duration=0.2):
    pyautogui.moveTo(x, y, duration=duration, tween=pyautogui.easeInOutQuad)

# 右键
def clickRight(x, y, duration=0.2):
    pyautogui.rightClick(x, y, duration=duration, tween=pyautogui.easeInOutQuad)

# 操作流程
m = Mouse("test.jpg")
m.move()
m.value()
m.show()

import pyautogui
import cv2
import numpy as np
import time

"""
小型自动化工具
图片识别
自动化简单工具
"""

# -*- coding:utf-8 -*-
# #cv读取中文有点问题 需要修改库里使用编码为utf-8
# 获取图像数据匹配全屏寻找
class Mouse():
    def __init__(self, address):  # 创建Mouse的时候加入图片地址参数
        time.sleep(0.5)
        self.address = address
        img = pyautogui.screenshot(region=[0, 0, pyautogui.size().width, pyautogui.size().height])  # x,y,w,h
        img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)  # cvtColor用于在图像中不同的色彩空间进行转换,用于后续处理。
        template = cv2.imread(self.address, cv2.COLOR_RGB2BGR)
        res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        self.max_loc = max_loc
        self.template = template
        self.X = max_loc[0] + template.shape[1] / 2
        self.Y = max_loc[1] + template.shape[0] / 2
        self.max_val = max_val
        self.img = img

    # 移动鼠标
    def move(self, val=0.85, x=0, y=0):  # x,y为偏移量
        if self.max_val > val:
            pyautogui.moveTo(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)

    # 获取匹配度
    def value(self):
        print("匹配度:", self.max_val)

    # 进行点击
    def click(self, val=0.85, x=0, y=0):  # x,y为偏移量
        if self.max_val > val:
            pyautogui.click(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)

    # 进行双击
    def doubleClick(self, val=0.85, x=0, y=0):  # x,y为偏移量
        """
        :param val: 匹配度
        :param x:   x轴偏移    左负右正
        :param y:   y轴偏移    上加下减
        :return:
        """
        if self.max_val > val:
            pyautogui.doubleClick(self.X + x, self.Y - y, duration=0.2, tween=pyautogui.easeInOutQuad)

    # 进行显示
    def show(self, val=0.85):
        if self.max_val > val:
            right_bottom = (self.max_loc[0] + self.template.shape[1], self.max_loc[1] + self.template.shape[0])  # 右下角
            cv2.rectangle(self.img, self.max_loc, right_bottom, 255, 2)  # 画出矩形位置
            cv2.imshow("output", self.img)
            cv2.waitKey(0)

# 写入
def write(string):
    pyautogui.write(string)

# 删除
def delete():
    pyautogui.hotkey('Ctrl', 'a')
    pyautogui.press('delete')

# 点击
def click(x, y, duration=0.2):
    pyautogui.click(x, y, duration=duration, tween=pyautogui.easeInOutQuad)

# 移动
def move(x, y, duration=0.2):
    pyautogui.moveTo(x, y, duration=duration, tween=pyautogui.easeInOutQuad)

# 右键
def clickRight(x, y, duration=0.2):
    pyautogui.rightClick(x, y, duration=duration, tween=pyautogui.easeInOutQuad)

# 操作流程
m = Mouse("test.jpg")
m.move()
m.value()
m.show()
# -*- coding:utf-8 -*-
import pyautogui
import cv2
from pynput.keyboard import Key, Listener as Listener_K, Controller as Controller_K
from pynput.mouse import Button, Listener as Listener_M, Controller as Controller_M
import numpy as np

'''
通过按键来触发
可以容错,不固定方位
如果有问题输出错误
如果没问题就输出两点的坐标
需求:
1、按键触发
2、输出大致图片
3、检测错误
'''

run_key = Key.ctrl_l  # 按键触发,ctrl_l即左边的Ctrl键
coordinate = []  # 存储坐标x1,y1,x2,y2


def on_click(x, y, button, pressed):  # 监听鼠标
    if pressed:  # 按压打印
        print(f'{button} pressed at {x}, {y}')
        coordinate.extend([x, y])
    else:
        return False


def on_release(key):
    '释放按键时执行。'
    print(key)
    if key == run_key:  # 可以改str类型来判断输入字母之类的
        return False  # 停止监听,作为运行


if __name__ == '__main__':
    keyboard = Controller_K()  # 监听按键
    with Listener_K(on_release=on_release) as listener:
        listener.join()
    # 监听到特定按键跳转
    print("鼠标点击")
    mouse = Controller_M()  # 监听鼠标
    for _ in range(2):
        with Listener_M(on_click=on_click) as listener:
            listener.join()
    print(coordinate)
    # 如果没有特殊情况,就截图
    if coordinate[0] - coordinate[2] == 0 or coordinate[1] - coordinate[3] == 0:
        print("错误")
    else:
        # 获取左上角坐标和差值
        coord, distance = [], []
        for n in range(2):
            if coordinate[n] < coordinate[n + 2]:
                coord.append(coordinate[n])
            else:
                coord.append(coordinate[n + 2])
            distance.append(abs(coordinate[n] - coordinate[n + 2]))
        img = pyautogui.screenshot(region=[coord[0], coord[1], distance[0], distance[1]])  # 左上角坐标和宽、高
        img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
        name = input("输入保存名称:")
        cv2.imwrite(name+".jpg", img)


# -*- coding:utf-8 -*-
import pyautogui
import cv2
from pynput.keyboard import Key, Listener as Listener_K, Controller as Controller_K
from pynput.mouse import Button, Listener as Listener_M, Controller as Controller_M
import numpy as np

'''
通过按键来触发
可以容错,不固定方位
如果有问题输出错误
如果没问题就输出两点的坐标
需求:
1、按键触发
2、输出大致图片
3、检测错误
'''

run_key = Key.ctrl_l  # 按键触发,ctrl_l即左边的Ctrl键
coordinate = []  # 存储坐标x1,y1,x2,y2


def on_click(x, y, button, pressed):  # 监听鼠标
    if pressed:  # 按压打印
        print(f'{button} pressed at {x}, {y}')
        coordinate.extend([x, y])
    else:
        return False


def on_release(key):
    '释放按键时执行。'
    print(key)
    if key == run_key:  # 可以改str类型来判断输入字母之类的
        return False  # 停止监听,作为运行


if __name__ == '__main__':
    keyboard = Controller_K()  # 监听按键
    with Listener_K(on_release=on_release) as listener:
        listener.join()
    # 监听到特定按键跳转
    print("鼠标点击")
    mouse = Controller_M()  # 监听鼠标
    for _ in range(2):
        with Listener_M(on_click=on_click) as listener:
            listener.join()
    print(coordinate)
    # 如果没有特殊情况,就截图
    if coordinate[0] - coordinate[2] == 0 or coordinate[1] - coordinate[3] == 0:
        print("错误")
    else:
        # 获取左上角坐标和差值
        coord, distance = [], []
        for n in range(2):
            if coordinate[n] < coordinate[n + 2]:
                coord.append(coordinate[n])
            else:
                coord.append(coordinate[n + 2])
            distance.append(abs(coordinate[n] - coordinate[n + 2]))
        img = pyautogui.screenshot(region=[coord[0], coord[1], distance[0], distance[1]])  # 左上角坐标和宽、高
        img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
        name = input("输入保存名称:")
        cv2.imwrite(name+".jpg", img)



http://www.kler.cn/a/455594.html

相关文章:

  • Oracle中listagg与wm_concat函数的区别
  • 2024年前端工程师总结
  • Java爬虫技术:按关键字搜索VIP商品详情
  • 一键图片转3D模型,AI建模,一键把图片转三维模型,二维图片转3维模型,AI建模
  • LabVIEW数字式气压计自动检定系统
  • elasticsearch中使用fuzzy查询
  • 使用Kotlin开发Springboot项目
  • Linux设置自动重启脚本
  • 流架构的读书笔记(2)
  • 「地平线」副总裁余轶南与「理想汽车」智驾产品总监赵哲伦联手创业,入局具身智能赛道!
  • 实践KDTS-WEB从mysql迁移到kingbasev9
  • 平凉大锅盔:历史与美味的交织
  • matlab客户端最新功能:使用vs code的github copilot编写mlx实时脚本文件
  • 渗透--还是tp
  • 浅谈下Spring MVC的执行流程
  • 关于SNAT、DNAT及浮动地址
  • MAC M4安装QT使用国内镜像源在线安装
  • JAVAweb學習日記(四)Maven
  • 大语言模型提示词工程
  • 基于Sentinel的服务保护方案的三种方式(请求限流、线程隔离、服务熔断)超详细讲解
  • AR 模型的功率谱
  • 优雅草央千澈-关于蓝湖如何快速的标注交互原型是如何使用的-如何使用蓝湖设计交互原型和整个软件项目的流程逻辑-实践项目详细说明
  • 卷积神经网络-三维卷积
  • 请分别从CPU、内 存、IO、⽹络的⻆度判断Linux的瓶颈?
  • 【数据库学习笔记】SQL触发器(例题+代码)
  • STM32F103RCT6学习之二:GPIO开发