Skip to content

Commit

Permalink
Merge pull request #405 from runhey/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
runhey authored Sep 1, 2024
2 parents ef18b89 + dd46f14 commit 2576072
Show file tree
Hide file tree
Showing 54 changed files with 1,456 additions and 319 deletions.
47 changes: 47 additions & 0 deletions dev_tools/get_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This Python file uses the following encoding: utf-8
# @author runhey
# github https://github.com/runhey
import cv2
import time
from datetime import timedelta, datetime
from cached_property import cached_property
from random import choice
from pathlib import Path


from module.exception import TaskEnd
from module.logger import logger
from module.base.timer import Timer

from tasks.Script.config_device import ScreenshotMethod, ControlMethod
from tasks.base_task import BaseTask
from tasks.Exploration.version import highlight

class GetAnimation(BaseTask):

@cached_property
def save_folder(self) -> Path:
save_time = datetime.now().strftime('%Y%m%dT%H%M%S')
save_folder = Path(f'./log/temp/{save_time}')
save_folder.mkdir(parents=True, exist_ok=True)
return save_folder

def run_screenshot(self):
self.config.model.script.device.screenshot_method = ScreenshotMethod.WINDOW_BACKGROUND
run_timer = Timer(3)
sho_timer = Timer(0.1)
run_timer.start()
sho_timer.start()
save_images = {}
while 1:
if run_timer.reached():
break
if sho_timer.reached():
sho_timer.reset()
image = self.device.screenshot_window_background()
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = highlight(image)
time_now1 = int(time.time() * 1000)
save_images[time_now1] = image
for time_now, image in save_images.items():
cv2.imwrite(str(self.save_folder / f'all{time_now}.png'), image)
57 changes: 57 additions & 0 deletions module/atom/gif.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This Python file uses the following encoding: utf-8
# @author runhey
# github https://github.com/runhey
import numpy as np

from module.atom.image import RuleImage



class RuleGif:
# 大部分实现同RuleImage 的接口

@property
def name(self) -> str:
return self.appear_target.name

def __init__(self, targets: list[RuleImage]):
self.targets = targets
self.roi_front: list = [0, 0, 0, 0]
self.appear_target = targets[0]

def pre_process(self, image):
return image


def search(self, image, roi: list = None, threshold: float = None) -> tuple:
"""
:param image:
:param roi:
:param threshold:
:return: bool
第一项是否为出现, 第二项为匹配的RuleImage
"""
image = self.pre_process(image)
#
threshold = self.targets[0].threshold if threshold is None else threshold
roi = self.targets[0].roi_back if roi is None else roi
for target in self.targets:
target.roi_back = roi
if target.match(image, threshold):
self.roi_front = target.roi_front
self.appear_target = target
return True, target
return False, None

def match(self, image, threshold: float = None) -> bool:
return self.search(image, threshold=threshold)[0]


def coord(self) -> tuple:
x, y, w, h = self.roi_front
return x + np.random.randint(0, w), y + np.random.randint(0, h)

def front_center(self) -> tuple:
x, y, w, h = self.roi_front
return int(x + w//2), int(y + h//2)
49 changes: 47 additions & 2 deletions module/atom/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def match(self, image: np.array, threshold: float = None) -> bool:
threshold = self.threshold

if not self.is_template_match:
raise Exception(f"unknown method {self.method}")
return self.sift_match(image)
# raise Exception(f"unknown method {self.method}")

source = self.corp(image)
mat = self.image
Expand All @@ -157,6 +158,34 @@ def match(self, image: np.array, threshold: float = None) -> bool:
else:
return False

def match_all(self, image: np.array, threshold: float = None, roi: list = None) -> list[tuple]:
"""
区别于match,这个是返回所有的匹配结果
:param roi:
:param image:
:param threshold:
:return:
"""
if roi is not None:
self.roi_back = roi
if threshold is None:
threshold = self.threshold
if not self.is_template_match:
raise Exception(f"unknown method {self.method}")
source = self.corp(image)
mat = self.image
results = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
locations = np.where(results >= threshold)
matches = []
for pt in zip(*locations[::-1]): # (x, y) coordinates
score = results[pt[1], pt[0]]
# 得分, x, y, w, h
x = self.roi_back[0] + pt[0]
y = self.roi_back[1] + pt[1]
matches.append((score, x, y, mat.shape[1], mat.shape[0]))
return matches


def coord(self) -> tuple:
"""
获取roi_front的随机的点击的坐标
Expand Down Expand Up @@ -215,7 +244,7 @@ def sift_match(self, image: np.array, show=False) -> bool:
# 设定阈值, 距离小于对方的距离的0.7倍我们认为是好的匹配点.
if m.distance < 0.6 * n.distance:
good.append(m)
if len(good) >= 4:
if len(good) >= 10:
src_pts = float32([self.kp[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = float32([kp[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)

Expand Down Expand Up @@ -252,6 +281,22 @@ def sift_match(self, image: np.array, show=False) -> bool:
cv2.destroyAllWindows()
return result

def match_mean_color(self, image, color: tuple, bias=10) -> bool:
"""
:param image:
:param color: rgb
:param bias:
:return:
"""
image = self.corp(image)
average_color = cv2.mean(image)
logger.info(f'{self.name} average_color: {average_color}')
for i in range(3):
if abs(average_color[i] - color[i]) > bias:
return False
return True


if __name__ == "__main__":
from dev_tools.assets_test import detect_image
Expand Down
7 changes: 7 additions & 0 deletions module/device/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This Python file uses the following encoding: utf-8
# https://github.com/LmeSzinc/StarRailCopilot/blob/master/module/device/env.py
import sys

IS_WINDOWS = sys.platform == 'win32'
IS_MACINTOSH = sys.platform == 'darwin'
IS_LINUX = sys.platform == 'linux'
3 changes: 3 additions & 0 deletions tasks/Component/GeneralInvite/general_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ def invite_friend(self, name: str = None, find_mode: FindMode = FindMode.AUTO_FI
list_2 = self.O_F_LIST_2.ocr(self.device.image)
list_3 = self.O_F_LIST_3.ocr(self.device.image)
list_4 = self.O_F_LIST_4.ocr(self.device.image)
list_1 = list_1.replace(' ', '').replace('、', '')
list_2 = list_2.replace(' ', '').replace('、', '')
list_3 = list_3.replace(' ', '').replace('、', '')
if list_1 is not None and list_1 != '' and list_1 in self.friend_class:
friend_class.append(list_1)
if list_2 is not None and list_2 != '' and list_2 in self.friend_class:
Expand Down
5 changes: 5 additions & 0 deletions tasks/Exploration/READMD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- 必须支持多人,队员队长和单人 三个模式
- 无脑启动
- 可选buff
- 绘卷模式
-
60 changes: 60 additions & 0 deletions tasks/Exploration/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,45 @@
class ExplorationAssets:


# Image Rule Assets
# description
I_LIGHTTEST = RuleImage(roi_front=(550,288,85,82), roi_back=(97,182,1130,409), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_lighttest.png")
# description
I_LIGHT1 = RuleImage(roi_front=(498,297,86,87), roi_back=(97,182,1130,409), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light1.png")
# description
I_LIGHT2 = RuleImage(roi_front=(497,300,86,82), roi_back=(97,182,1130,409), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light2.png")
# description
I_LIGHT3 = RuleImage(roi_front=(495,301,85,77), roi_back=(97,182,1130,409), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light3.png")
# description
I_LIGHT4 = RuleImage(roi_front=(496,296,85,85), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light4.png")
# description
I_LIGHT5 = RuleImage(roi_front=(494,296,82,86), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light5.png")
# description
I_LIGHT6 = RuleImage(roi_front=(490,295,83,85), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light6.png")
# description
I_LIGHT7 = RuleImage(roi_front=(486,295,84,87), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light7.png")
# description
I_LIGHT8 = RuleImage(roi_front=(485,298,84,81), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light8.png")
# description
I_LIGHT9 = RuleImage(roi_front=(484,295,85,84), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light9.png")
# description
I_LIGHT10 = RuleImage(roi_front=(479,297,84,78), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light10.png")
# description
I_LIGHT11 = RuleImage(roi_front=(479,296,83,82), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light11.png")
# description
I_LIGHT12 = RuleImage(roi_front=(477,290,85,90), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light12.png")
# description
I_LIGHT13 = RuleImage(roi_front=(478,293,82,87), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light13.png")
# description
I_LIGHT14 = RuleImage(roi_front=(474,290,82,87), roi_back=(0,0,100,100), threshold=0.6, method="Template matching", file="./tasks/Exploration/highlight/highlight_light14.png")
# 经验怪
I_UP_EXP = RuleImage(roi_front=(471,518,74,71), roi_back=(1,225,1278,410), threshold=0.8, method="Sift Flann", file="./tasks/Exploration/highlight/highlight_up_exp.png")
# 金币怪
I_UP_COIN = RuleImage(roi_front=(330,529,74,74), roi_back=(1,317,1278,316), threshold=0.8, method="Sift Flann", file="./tasks/Exploration/highlight/highlight_up_coin.png")
# 达摩怪
I_UP_DARUMA = RuleImage(roi_front=(1146,510,80,80), roi_back=(1,265,1278,369), threshold=0.8, method="Sift Flann", file="./tasks/Exploration/highlight/highlight_up_daruma.png")


# Click Rule Assets
# 点击设置按钮
C_CLICK_SETTINGS = RuleClick(roi_front=(55,662,21,21), roi_back=(55,662,21,21), name="click_settings")
Expand All @@ -27,6 +66,8 @@ class ExplorationAssets:
C_CLICK_ROTATE_3 = RuleClick(roi_front=(785,587,21,21), roi_back=(785,587,21,21), name="click_rotate_3")
# 位置4
C_CLICK_ROTATE_4 = RuleClick(roi_front=(921,590,21,21), roi_back=(921,590,21,21), name="click_rotate_4")
# 随机点
C_SAFE_RANDOM = RuleClick(roi_front=(0,0,111,12), roi_back=(0,0,111,12), name="safe_random")


# Image Rule Assets
Expand Down Expand Up @@ -70,6 +111,25 @@ class ExplorationAssets:
I_E_EXIT_CONFIRM = RuleImage(roi_front=(694,380,163,49), roi_back=(694,380,163,49), threshold=0.8, method="Template matching", file="./tasks/Exploration/res/res_e_exit_confirm.png")
# 宝箱
I_TREASURE_BOX_CLICK = RuleImage(roi_front=(33,476,70,49), roi_back=(2,130,135,406), threshold=0.7, method="Template matching", file="./tasks/Exploration/res/res_treasure_box_click.png")
# 困28滚动到最后
I_SWIPE_END = RuleImage(roi_front=(994,234,119,100), roi_back=(968,196,311,165), threshold=0.8, method="Template matching", file="./tasks/Exploration/res/res_swipe_end.png")
# 队伍的表情标志
I_TEAM_EMOJI = RuleImage(roi_front=(36,437,44,46), roi_back=(4,407,100,100), threshold=0.8, method="Template matching", file="./tasks/Exploration/res/res_team_emoji.png")
# 组队按钮
I_EXP_CREATE_TEAM = RuleImage(roi_front=(590,514,122,52), roi_back=(543,490,192,100), threshold=0.8, method="Template matching", file="./tasks/Exploration/res/res_exp_create_team.png")
# 创建确认
I_EXP_CREATE_ENSURE = RuleImage(roi_front=(534,486,218,59), roi_back=(516,475,244,85), threshold=0.8, method="Template matching", file="./tasks/Exploration/res/res_exp_create_ensure.png")


# Long Click Rule Assets
# description
L_ROTATE_1 = RuleLongClick(roi_front=(516,582,22,21), roi_back=(516,582,22,21), duration=1500, name="rotate_1")
# description
L_ROTATE_2 = RuleLongClick(roi_front=(650,587,21,21), roi_back=(650,587,21,21), duration=1500, name="rotate_2")
# description
L_ROTATE_3 = RuleLongClick(roi_front=(785,587,21,21), roi_back=(785,587,21,21), duration=1500, name="rotate_3")
# description
L_ROTATE_4 = RuleLongClick(roi_front=(921,590,21,21), roi_back=(921,590,21,21), duration=1500, name="rotate_4")


# Ocr Rule Assets
Expand Down
Loading

0 comments on commit 2576072

Please sign in to comment.