OpenCV模式匹配
import cv2
import numpy as np
import pyautogui
import time
import random

num = 0
class ScreenRecognizer:

    def __init__(self):
        pass

    def capture_screen(self):
        try:
            screenshot = pyautogui.screenshot()
            screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
            return screenshot
        except Exception as e:
            print(f"捕获屏幕时出错: {e}")
            return None

    def find_image(self, image_path, threshold=0.8):
        screen = self.capture_screen()
        if screen is None:
            return None

        try:
            # 读取模板图像
            template = cv2.imread(image_path, cv2.IMREAD_COLOR)
            if template is None:
                print(f"无法读取模板图像: {image_path}")
                return None

            # 模板匹配
            result = cv2.matchTemplate(screen, template, cv2.TM_CCOEFF_NORMED)
            y_loc, x_loc = np.where(result >= threshold)

            if len(x_loc) > 0:
                # 返回第一个匹配的位置和大小
                return x_loc[0], y_loc[0], template.shape[1], template.shape[0]
        except Exception as e:
            print(f"查找图像时出错: {e}")

        return None

    def click_coordinate(self, x, y, w, h):
        # 计算点击位置为模板图像的中心
        center_x = x + w // 2
        center_y = y + h // 2

        offset_x = random.randint(-60, 60)
        offset_y = random.randint(-15, 15)

        final_x = center_x + offset_x
        final_y = center_y + offset_y

        pyautogui.click(final_x, final_y)
        print(f"Clicked at: ({final_x}, {final_y})")


    def move(self,x,y,w,h):

        center_x = x + w // 2
        center_y = y + h // 2

        pyautogui.moveTo(center_x, center_y+100)

    def perform_action(self, image_path, action, threshold=0.8):
        global num
        result = self.find_image(image_path, threshold)
        if result is not None:
            print(action)
            x, y, w, h = result
            if action == 'again_click':
                self.click_coordinate(x, y, w, h)
                self.move(x, y, w, h)
                num +=1
            elif action == 'confirm_click':
                self.click_coordinate(x, y, w, h)
            elif action == 'none_click':
                self.click_coordinate(x, y, w, h)
                num -=1
            else:
                print(f"未知操作: {action}")

if __name__ == "__main__":
    recognizer = ScreenRecognizer()
    aim = int(input("循环次数:"))
    try:
        while num <= aim:
            print(num)
            # 定义多个模板图像及其对应的操作
            actions = [
                ('template1.png', 'again_click'),
                ('template2.png', 'confirm_click'),
                ('template3.png', 'none_click')
            ]

            for image_path, action in actions:
                recognizer.perform_action(image_path, action)

            time.sleep(1)
    except KeyboardInterrupt:
        print("程序已退出")
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇