Просмотр исходного кода

feat(atom): image, click, long_click, swipe

runhey 3 лет назад
Родитель
Сommit
1f5b155ca1

+ 160 - 0
module/atom/cBezier.py

@@ -0,0 +1,160 @@
+'''
+author: cbb
+这个库基于贝塞尔曲线实现模拟人手动滑动的轨迹
+可以用于selenium轨迹的模拟,或者生成轨迹数组用于js加密通过网站服务器后端分控检测
+QQ群 134064772
+里面的人说话好听,个个都是人才
+'''
+
+import numpy as np
+import math
+import random
+
+class BezierTrajectory:
+
+    @classmethod
+    def _bztsg(cls, dataTrajectory):
+        lengthOfdata = len(dataTrajectory)
+
+        def staer(x):
+            t = ((x - dataTrajectory[0][0]) / (dataTrajectory[-1][0] - dataTrajectory[0][0]))
+            y = np.array([0, 0], dtype=np.float64)
+            for s in range(len(dataTrajectory)):
+                y += dataTrajectory[s] * ((math.factorial(lengthOfdata - 1) / (
+                            math.factorial(s) * math.factorial(lengthOfdata - 1 - s))) * math.pow(t, s) * math.pow(
+                    (1 - t), lengthOfdata - 1 - s))
+            return y[1]
+
+        return staer
+
+    @classmethod
+    def _type(cls, type, x, numberList):
+        numberListre = []
+        pin = (x[1] - x[0]) / numberList
+        if type == 0:
+            for i in range(numberList):
+                numberListre.append(i * pin)
+            if pin >= 0:
+                numberListre = numberListre[::-1]
+        elif type == 1:
+            for i in range(numberList):
+                numberListre.append(1 * ((i * pin) ** 2))
+            numberListre = numberListre[::-1]
+        elif type == 2:
+            for i in range(numberList):
+                numberListre.append(1 * ((i * pin - x[1]) ** 2))
+
+        elif type == 3:
+            dataTrajectory = [np.array([0,0]), np.array([(x[1]-x[0])*0.8, (x[1]-x[0])*0.6]), np.array([x[1]-x[0], 0])]
+            fun = cls._bztsg(dataTrajectory)
+            numberListre = [0]
+            for i in range(1,numberList):
+                numberListre.append(fun(i * pin) + numberListre[-1])
+            if pin >= 0:
+                numberListre = numberListre[::-1]
+        numberListre = np.abs(np.array(numberListre) - max(numberListre))
+        biaoNumberList = ((numberListre - numberListre[numberListre.argmin()]) / (
+                    numberListre[numberListre.argmax()] - numberListre[numberListre.argmin()])) * (x[1] - x[0]) + x[0]
+        biaoNumberList[0] = x[0]
+        biaoNumberList[-1] = x[1]
+        return biaoNumberList
+
+    @classmethod
+    def getFun(cls, s):
+        '''
+
+        :param s: 传入P点
+        :return: 返回公式
+        '''
+        dataTrajectory = []
+        for i in s:
+            dataTrajectory.append(np.array(i))
+        return cls._bztsg(dataTrajectory)
+
+    @classmethod
+    def simulation(cls, start, end, le=1, deviation=0, bias=0.5):
+        '''
+
+        :param start:开始点的坐标 如 start = [0, 0]
+        :param end:结束点的坐标 如 end = [100, 100]
+        :param le:几阶贝塞尔曲线,越大越复杂 如 le = 4
+        :param deviation:轨迹上下波动的范围 如 deviation = 10
+        :param bias:波动范围的分布位置 如 bias = 0.5
+        :return:返回一个字典equation对应该曲线的方程,P对应贝塞尔曲线的影响点
+        '''
+        start = np.array(start)
+        end = np.array(end)
+        cbb = []
+        if le != 1:
+            e = (1 - bias) / (le - 1)
+            cbb = [[bias + e * i, bias + e * (i + 1)] for i in range(le - 1)]
+
+        dataTrajectoryList = [start]
+
+        t = random.choice([-1, 1])
+        w = 0
+        for i in cbb:
+            px1 = start[0] + (end[0] - start[0]) * (random.random() * (i[1] - i[0]) + (i[0]))
+            p = np.array([px1, cls._bztsg([start, end])(px1) + t * deviation])
+            dataTrajectoryList.append(p)
+            w += 1
+            if w >= 2:
+                w = 0
+                t = -1 * t
+
+        dataTrajectoryList.append(end)
+        return {"equation": cls._bztsg(dataTrajectoryList), "P": np.array(dataTrajectoryList)}
+
+    @classmethod
+    def trackArray(cls, start, end, numberList, le=1, deviation=0, bias=0.5, type=0, cbb=0, yhh=10):
+        '''
+
+        :param start:开始点的坐标 如 start = [0, 0]
+        :param end:结束点的坐标 如 end = [100, 100]
+        :param numberList:返回的数组的轨迹点的数量 numberList = 150
+        :param le:几阶贝塞尔曲线,越大越复杂 如 le = 4
+        :param deviation:轨迹上下波动的范围 如 deviation = 10
+        :param bias:波动范围的分布位置 如 bias = 0.5
+        :param type:0表示均速滑动,1表示先慢后快,2表示先快后慢,3表示先慢中间快后慢 如 type = 1
+        :param cbb:在终点来回摆动的次数
+        :param yhh:在终点来回摆动的范围
+        :return:返回一个字典trackArray对应轨迹数组,P对应贝塞尔曲线的影响点
+        '''
+        s = []
+        fun = cls.simulation(start, end, le, deviation, bias)
+        w = fun['P']
+        fun = fun["equation"]
+        if cbb != 0:
+            numberListOfcbb = round(numberList*0.2/(cbb+1))
+            numberList -= (numberListOfcbb*(cbb+1))
+
+            xTrackArray = cls._type(type, [start[0], end[0]], numberList)
+            for i in xTrackArray:
+                s.append([i, fun(i)])
+            dq = yhh/cbb
+            kg = 0
+            ends = np.copy(end)
+            for i in range(cbb):
+                if kg == 0:
+                    d = np.array([end[0] + (yhh-dq*i), ((end[1]-start[1])/(end[0]-start[0]))*(end[0]+(yhh-dq*i)) +(end[1]-((end[1]-start[1])/(end[0]-start[0]))*end[0])   ])
+                    kg = 1
+                else:
+                    d = np.array([end[0] - (yhh - dq * i), ((end[1]-start[1])/(end[0]-start[0]))*(end[0]-(yhh-dq*i)) +(end[1]-((end[1]-start[1])/(end[0]-start[0]))*end[0])  ])
+                    kg = 0
+                print(d)
+                y = cls.trackArray(ends, d, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
+                s += list(y['trackArray'])
+                ends = d
+            y = cls.trackArray(ends, end, numberListOfcbb, le=2, deviation=0, bias=0.5, type=0, cbb=0, yhh=10)
+            s += list(y['trackArray'])
+
+        else:
+            xTrackArray = cls._type(type, [start[0], end[0]], numberList)
+            for i in xTrackArray:
+                s.append([i, fun(i)])
+        # return {"trackArray": np.array(s), "P": w}
+        return [[int(s[0]), int(s[1])] for s in s]
+
+# print(BezierTrajectory.trackArray(start=[0, 0], end=[100, 100], numberList=50, le=2,
+#                     deviation=0, bias=0.5, type=0, cbb=0, yhh=10))
+

+ 69 - 0
module/atom/click.py

@@ -0,0 +1,69 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import numpy as np
+
+from module.base.decorator import cached_property
+from module.logger import logger
+
+class RuleClick:
+
+    def __init__(self, roi_front: tuple, roi_back: tuple) -> None:
+        """
+        初始化
+        :param roi_front:
+        :param roi_back:
+        """
+        self.roi_front = roi_front
+        self.roi_back = roi_back
+
+    def coord(self) -> tuple:
+        """
+        获取坐标, 从roi_front随机获取坐标
+        :return:
+        """
+        x, y, w, h = self.roi_front
+        x = np.random.randint(x, x + w)
+        y = np.random.randint(y, y + h)
+        return x, y
+
+    def coord_more(self) -> tuple:
+        """
+        从roi_back随机获取坐标
+        :return:
+        """
+        x, y, w, h = self.roi_back
+        x = np.random.randint(x, x + w)
+        y = np.random.randint(y, y + h)
+        return x, y
+
+    @property
+    def center(self) -> tuple:
+        """
+        返回roi_front的中心坐标
+        :return:
+        """
+        x, y, w, h = self.roi_front
+        return x + w // 2, y + h // 2
+
+    def move(self, x: int, y: int) -> None:
+        """
+        移动roi_front, 需要限幅x是0-1280, y是0-720
+        :param x:
+        :param y:
+        :return:
+        """
+        x, y, w, h = self.roi_front
+        x += x
+        y += y
+        if x <= 0 :
+            x = 0
+        elif x >= 1280:
+            x = 1280
+
+        if y <= 0 :
+            y = 0
+        elif y >= 720:
+            y = 720
+
+        self.roi_front = x, y, w, h

+ 142 - 0
module/atom/image.py

@@ -0,0 +1,142 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import cv2
+import numpy as np
+
+from numpy import float32, int32, uint8, fromfile
+from pathlib import Path
+
+from module.base.decorator import cached_property
+from module.logger import logger
+
+class RuleImage:
+
+    def __init__(self, roi_front: tuple, roi_back: tuple, method: str, threshold: float, file: str) -> None:
+        """
+        初始化
+        :param roi_front: 前置roi
+        :param roi_back: 后置roi 用于匹配的区域
+        :param method: 匹配方法
+        :param threshold: 阈值
+        :param file: 相对路径, 带后缀
+        """
+        self.roi_front = roi_front
+        self.roi_back = roi_back
+        self.method = method
+        self.threshold = threshold
+        self.file = file
+
+        self._image = None  # 这个是匹配的目标
+
+    @cached_property
+    def name(self) -> str:
+        """
+
+        :return:
+        """
+        return Path(self.file).stem
+
+    def __str__(self):
+        return self.name
+
+    __repr__ = __str__
+
+    def __eq__(self, other):
+        return str(self) == str(other)
+
+    def __hash__(self):
+        return hash(self.name)
+
+    def __bool__(self):
+        return True
+
+
+
+    def load_image(self) -> None:
+        """
+        加载图片
+        :return:
+        """
+        if self._image is not None:
+            return
+        img = cv2.imdecode(fromfile(self.file, dtype=uint8), -1)
+        # self._image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
+        self._image = img
+
+        height, width, channels = self._image.shape
+        if height != self.roi_front[3] or width != self.roi_front[2]:
+            self.roi_front[2] = width
+            self.roi_front[3] = height
+            logger.info(f"roi_front size changed to {width}x{height}")
+
+    @property
+    def image(self):
+        """
+        获取图片
+        :return:
+        """
+        if self._image is None:
+            self.load_image()
+        return self._image
+
+    @cached_property
+    def is_template_match(self) -> bool:
+        """
+        是否是模板匹配
+        :return:
+        """
+        return self.method == "Template matching"
+
+
+    def corp(self, image: np.array) -> np.array:
+        """
+        截取图片
+        :param image:
+        :return:
+        """
+        x, y, w, h = self.roi_back
+        return image[y:y + h, x:x + w]
+
+
+    def match(self, image: np.array, threshold: float = None) -> bool:
+        """
+
+        :param threshold:
+        :param image:
+        :return:
+        """
+        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
+        res = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
+        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)  # 最小匹配度,最大匹配度,最小匹配度的坐标,最大匹配度的坐标
+        if max_val > threshold:
+            return True
+        else:
+            return False
+
+    def coord(self) -> tuple:
+        """
+        获取roi_front的随机的点击的坐标
+        :return:
+        """
+        x, y, w, h = self.roi_front
+        return x + np.random.randint(0, w), y + np.random.randint(0, h)
+
+    def coord_more(self) -> tuple:
+        """
+         获取roi_back的随机的点击的坐标
+        :return:
+        """
+        x, y, w, h = self.roi_back
+        return x + np.random.randint(0, w), y + np.random.randint(0, h)
+
+
+if __name__ == "__main__":
+    pass

+ 23 - 0
module/atom/long_click.py

@@ -0,0 +1,23 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import numpy as np
+
+from module.base.decorator import cached_property
+from module.logger import logger
+from module.atom.click import RuleClick
+
+
+class RuleLongClick(RuleClick):
+
+    def __init__(self, roi_front: tuple, roi_back: tuple, duration: int = 1000) -> None:
+        """
+        初始化
+        :param roi_front:
+        :param roi_back:
+        :param duration:
+        """
+        super().__init__(roi_front, roi_back)
+        self.duration = duration
+
+

+ 3 - 0
module/atom/ocr.py

@@ -0,0 +1,3 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey

+ 111 - 0
module/atom/swipe.py

@@ -0,0 +1,111 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import numpy as np
+import random
+
+from math import dist
+
+from module.base.decorator import cached_property
+from module.atom.cBezier import BezierTrajectory
+from module.logger import logger
+
+
+class RuleSwipe:
+
+    def __init__(self, roi_front: tuple, roi_back: tuple, mode: str) -> None:
+        """
+        初始化
+        :param roi_front:
+        :param roi_back:
+        :param mode:
+        """
+        self.roi_front = roi_front
+        self.roi_back = roi_back
+        self.mode = mode
+
+        self.interval: int = 8  # 每次移动的间隔时间
+
+    @cached_property
+    def is_default_mode(self) -> bool:
+        """
+        是否是默认模式
+        :return:
+        """
+        return self.mode == 'default'
+
+    @cached_property
+    def is_vector_mode(self) -> bool:
+        """
+        是否是向量模式
+        :return:
+        """
+        return self.mode == 'vector'
+
+    def coord(self) -> tuple:
+        """
+        获取坐标, 从roi_front随机获取坐标 和从roi_back随机获取的坐标
+        :return: 两个坐标的tuple
+        """
+        x, y, w, h = self.roi_front
+        x = np.random.randint(x, x + w)
+        y = np.random.randint(y, y + h)
+        x2, y2, w2, h2 = self.roi_back
+        x2 = np.random.randint(x2, x2 + w2)
+        y2 = np.random.randint(y2, y2 + h2)
+        return x, y, x2, y2
+
+    def trace(self) -> list:
+        """
+        获取滑动的路径,list的每一项都是tuple
+        :return:
+        """
+        if self.is_default_mode:
+            start_pos, end_pos = self.coord()
+            # 表示每秒移动1.5个像素点, 总的时间除以每个点10ms就得到总的点的个数
+            number_list: int = int(dist(start_pos, end_pos) / (1.5 * self.interval))
+            le = random.randint(2, 4)  #
+            deviation = random.randint(20, 40)  # 幅度
+            b_type = 3
+            obbs_type = random.random()  # 0.8的概率是先快中间慢后面快, 0.1概率是先快后慢, 0.1概率先慢后快
+            if 0 < obbs_type <= 0.8:
+                b_type = 3
+            elif obbs_type < 0.9:
+                b_type = 2
+            else:
+                b_type = 1
+
+            return BezierTrajectory.trackArray(start=start_pos, end=end_pos, numberList=number_list, le=le,
+                     deviation=30, bias=0.5, type=b_type, cbb=0, yhh=20)
+
+        elif self.is_vector_mode:
+            # 获取两个点的直线的规矩
+            start_pos, end_pos = self.coord()
+            # 表示每秒移动1.5个像素点, 总的时间除以每个点10ms就得到总的点的个数
+            number_list: int = int(dist(start_pos, end_pos) / (1.5 * self.interval))
+
+            def generate_linear_trajectory(start_pos: tuple, end_pos: tuple, num_points: int) -> list:
+                """
+                生成线性轨迹
+                :param start_pos:
+                :param end_pos:
+                :param num_points:
+                :return:
+                """
+                trajectory = []
+                delta_x = (end_pos[0] - start_pos[0]) / (num_points - 1)
+                delta_y = (end_pos[1] - start_pos[1]) / (num_points - 1)
+                for i in range(num_points):
+                    x = start_pos[0] + delta_x * i
+                    y = start_pos[1] + delta_y * i
+                    trajectory.append((x, y))
+                return trajectory
+
+            return generate_linear_trajectory(start_pos, end_pos, number_list)
+
+        else:
+            raise ValueError(f'Invalid mode: {self.mode}')
+
+
+
+

+ 29 - 1
module/tasks/CoinChallenge/res/test.json

@@ -1 +1,29 @@
-[{"itemName":"new1","imageName":"res_new1","roiFront":"157,240,70,79","roiBack":"41,185,279,207","method":"Template matching","threshold":0.8,"description":"description"},{"itemName":"new2","imageName":"res_new2","roiFront":"362,243,100,100","roiBack":"803,101,100,100","method":"Template matching","threshold":0.8,"description":"description"},{"itemName":"new3","imageName":"res_new3","roiFront":"568,212,240,220","roiBack":"0,0,100,100","method":"Template matching","threshold":0.9,"description":"desc55"}]
+[
+    {
+        "itemName": "new1",
+        "imageName": "res_new1",
+        "roiFront": "157,240,70,79",
+        "roiBack": "41,185,279,207",
+        "method": "Template matching",
+        "threshold": 0.8,
+        "description": "description"
+    },
+    {
+        "itemName": "new2",
+        "imageName": "res_new2",
+        "roiFront": "362,243,100,100",
+        "roiBack": "803,101,100,100",
+        "method": "Template matching",
+        "threshold": 0.8,
+        "description": "description"
+    },
+    {
+        "itemName": "new3",
+        "imageName": "res_new3",
+        "roiFront": "568,212,240,220",
+        "roiBack": "0,0,100,100",
+        "method": "Template matching",
+        "threshold": 0.9,
+        "description": "desc55"
+    }
+]

+ 23 - 1
module/tasks/CoinChallenge/res/test_long_click.json

@@ -1 +1,23 @@
-[{"itemName":"new1","roiFront":"208,151,100,100","roiBack":"542,279,100,100","duration":1500,"description":"description"},{"itemName":"new2","roiFront":"355,191,100,100","roiBack":"526,323,100,100","duration":1500,"description":"description"},{"itemName":"new3","roiFront":"112,156,100,100","roiBack":"469,200,100,100","duration":1000,"description":"de66666n"}]
+[
+    {
+        "itemName": "new1",
+        "roiFront": "208,151,100,100",
+        "roiBack": "542,279,100,100",
+        "duration": 1500,
+        "description": "description"
+    },
+    {
+        "itemName": "new2",
+        "roiFront": "355,191,100,100",
+        "roiBack": "526,323,100,100",
+        "duration": 1500,
+        "description": "description"
+    },
+    {
+        "itemName": "new3",
+        "roiFront": "112,156,100,100",
+        "roiBack": "469,200,100,100",
+        "duration": 1000,
+        "description": "de66666n"
+    }
+]

+ 30 - 1
module/tasks/CoinChallenge/res/test_swipe.json

@@ -1 +1,30 @@
-[{"itemName":"new1","roiFront":"253,212,100,100","roiBack":"652,292,100,100","mode":"default","description":"description"},{"itemName":"new2","roiFront":"265,272,100,100","roiBack":"622,338,100,100","mode":"default","description":"description"},{"itemName":"new3","roiFront":"198,160,100,100","roiBack":"626,305,100,100","mode":"default","description":"description"},{"itemName":"new4","roiFront":"150,145,100,100","roiBack":"535,199,100,100","mode":"vector","description":"description"}]
+[
+    {
+        "itemName": "new1",
+        "roiFront": "253,212,100,100",
+        "roiBack": "652,292,100,100",
+        "mode": "default",
+        "description": "description"
+    },
+    {
+        "itemName": "new2",
+        "roiFront": "265,272,100,100",
+        "roiBack": "622,338,100,100",
+        "mode": "default",
+        "description": "description"
+    },
+    {
+        "itemName": "new3",
+        "roiFront": "198,160,100,100",
+        "roiBack": "626,305,100,100",
+        "mode": "default",
+        "description": "description"
+    },
+    {
+        "itemName": "new4",
+        "roiFront": "150,145,100,100",
+        "roiBack": "535,199,100,100",
+        "mode": "vector",
+        "description": "description"
+    }
+]