runhey пре 3 година
родитељ
комит
e385de5b3f

+ 3 - 5
README.md

@@ -96,9 +96,9 @@ oas目前还在开发中,我们期待你的加入,Q群: 465946275
 | ✔自动化部署                                                  |
 | ✔GUI引入工程                                                 |
 | ✔config读写参数字段                                          |
-| 🔨优化window截图+控制                                         |
-| 🔨投屏到OAS                                                   |
-| 🔨操作格式化json生成工具:可以从用户界面简单操作生成维护数据(包括图片匹配,点击操作,长按操作,滑动操作,ocr识别等等) |
+| ✔优化window截图+控制                                         |
+| ✔投屏到OAS                                                   |
+| 🔨操作格式化json生成工具:可以从用户界面简单操作生成维护数据(包括✔图片匹配,点击操作,长按操作,滑动操作,ocr识别等等) |
 | 📆Atom: 对于上一条数据的接口                                  |
 | 📆实现ocr单独进程                                             |
 | 📆到此我们可以(读参数)、(对游戏截屏控制)、(操控数据)。实现第一个脚本任务如挖土 |
@@ -110,5 +110,3 @@ oas目前还在开发中,我们期待你的加入,Q群: 465946275
 | 📆实现更多的任务 | 如何输出Log到GUI |
 | 📆建立任务调度   | 优化速度         |
 |                 |                  |
-
-![screenshot](https://runhey-img-stg1.oss-cn-chengdu.aliyuncs.com/img2/202306031329914.png)

+ 2 - 0
gui.py

@@ -7,6 +7,7 @@ from module.gui.context.add import Add
 from module.gui.context.settings import Setting
 from module.gui.context.process_manager import ProcessManager
 from module.gui.register_type.paint_image import PaintImage
+from module.gui.register_type.rule_file import RuleFile
 from module.gui.fluent_app import FluentApp
 
 if __name__ == "__main__":
@@ -24,5 +25,6 @@ if __name__ == "__main__":
     app.set_context_property(add_config, 'add_config')
     app.set_context_property(process_manager, 'process_manager')
     app.qml_register_type(PaintImage, 'PaintImage')
+    app.qml_register_type(RuleFile, 'RuleFile')
     # 启动一个GUI
     app.run()

+ 1 - 0
module/gui/qml.qrc

@@ -30,5 +30,6 @@
         <file>qml/Content/OcrRule.qml</file>
         <file>qml/Component/MirrorImage.qml</file>
         <file>qml/Component/ResizableRectangle.qml</file>
+        <file>qml/Component/RuleFile.qml</file>
     </qresource>
 </RCC>

+ 56 - 1
module/gui/qml/Component/MirrorImage.qml

@@ -12,6 +12,47 @@ Item {
 
     property alias imageWidth: paintImage.width
     property alias imageHeight: paintImage.height
+
+    property alias roiRed_x: roi_Red.x
+    property alias roiRed_y: roi_Red.y
+    property alias roiRed_width: roi_Red.width
+    property alias roiRed_height: roi_Red.height
+    property alias roiGreen_x: roi_Green.x
+    property alias roiGreen_y: roi_Green.y
+    property alias roiGreen_width: roi_Green.width
+    property alias roiGreen_height: roi_Green.height
+
+    signal roi_red_changed(string roi)
+    signal roi_green_changed(string roi)
+
+    onRoiRed_xChanged: {
+//        roi_red_changed( '${roiRed_x},${roiRed_y},${roiRed_width},${roiRed_height}' )
+        roi_red_changed( roiRed_x + "," +  roiRed_y + "," + roiRed_width + "," + roiRed_height)
+    }
+    onRoiRed_yChanged: {
+        roi_red_changed( roiRed_x + "," +  roiRed_y + "," + roiRed_width + "," + roiRed_height)
+    }
+    onRoiRed_widthChanged: {
+        roi_red_changed( roiRed_x + "," +  roiRed_y + "," + roiRed_width + "," + roiRed_height)
+    }
+    onRoiRed_heightChanged: {
+        roi_red_changed( roiRed_x + "," +  roiRed_y + "," + roiRed_width + "," + roiRed_height)
+    }
+
+
+    onRoiGreen_xChanged: {
+        roi_green_changed( roiGreen_x + "," +  roiGreen_y + "," + roiGreen_width + "," + roiGreen_height )
+    }
+    onRoiGreen_yChanged: {
+        roi_green_changed( roiGreen_x + "," +  roiGreen_y + "," + roiGreen_width + "," + roiGreen_height )
+    }
+    onRoiGreen_widthChanged: {
+        roi_green_changed( roiGreen_x + "," +  roiGreen_y + "," + roiGreen_width + "," + roiGreen_height )
+    }
+    onRoiGreen_heightChanged: {
+        roi_green_changed( roiGreen_x + "," +  roiGreen_y + "," + roiGreen_width + "," + roiGreen_height )
+    }
+
     enum RoiMode {
         None,
         Same,
@@ -69,7 +110,7 @@ Item {
         width: 100
         height: 100
         color: "transparent"
-        border.width: 1
+        border.width: 3
         border.color: "green"
         ResizableRectangle{
             resizeTarget: roi_Green//设置调整目标ID
@@ -288,7 +329,21 @@ Item {
                 }
             }
         }
+        // 帮助
+        Item{
+            width: imageExpander.width
+            height: 50
+            FluText{
+                anchors.left: parent.left
+                anchors.verticalCenter: parent.verticalCenter
+                text: "python+qml 就别想着高帧率了,而且是以截图方式而不是视频流"
+            }
+        }
 
         }
     }
+
+    function save_target_image(roi, file){
+        paintImage.save_target_image(roi, file)
+    }
 }

+ 220 - 0
module/gui/qml/Component/RuleFile.qml

@@ -0,0 +1,220 @@
+import QtQuick
+import QtQuick.Controls
+import FluentUI
+import QtQuick.Dialogs
+import Oas
+
+FluArea {
+    id: root
+    property string file: ""  //完整的文件路径带后缀
+    property alias folder: folderButton.text
+    property alias list_model: listModel
+    property alias rule_file: ruleFile
+
+    property var currentItem: listModel.get(0)
+    property var addFunc: function(){}
+    property var editFunc: function(){}
+    property var saveFunc: function(){}
+    ListModel{
+        id: listModel
+    }
+
+    RuleFile{
+        id: ruleFile
+    }
+
+    Column{
+        id: coi
+        anchors{
+            top: parent.top
+//            topMargin: 10
+            bottom: parent.bottom
+            left: parent.left
+            leftMargin: 20
+            right: parent.right
+            rightMargin: 20
+        }
+
+        spacing: 5
+        // 头
+        Item{
+            width: coi.width
+            height: 40
+            FluText{
+                anchors.left: parent.left
+                anchors.verticalCenter: parent.verticalCenter
+                text: "Select rule"
+                font: FluTextStyle.BodyStrong
+            }
+        }
+        //路径
+        Item{
+            width: coi.width
+            height: 40
+            FluText{
+                anchors.left: parent.left
+                anchors.verticalCenter: parent.verticalCenter
+                text: "Folder"
+                font: FluTextStyle.BodyStrong
+            }
+            FluButton{
+                id: folderButton
+                anchors{
+                    right: parent.right
+                    verticalCenter: parent.verticalCenter
+                }
+                disabled: true
+                text:"Select folder"
+                width: 200
+                onClicked: {
+
+                }
+            }
+        }
+        // 文件名
+        Item{
+            width: coi.width
+            height: 40
+            FluText{
+                anchors.left: parent.left
+                anchors.verticalCenter: parent.verticalCenter
+                text: "File"
+                font: FluTextStyle.BodyStrong
+            }
+            FluButton{
+                id: fileButton
+                anchors{
+                    right: parent.right
+                    verticalCenter: parent.verticalCenter
+                }
+                text:"Select file"
+                width: 200
+                onClicked: {
+                    jsonFileDialog.open()
+                }
+            }
+            FileDialog{
+                   id: jsonFileDialog
+                   title: "select your json file"
+                   nameFilters: ["(*.json)"]
+                   onAccepted: {
+                       const selectedUrl = jsonFileDialog.selectedFile.toString()
+                       if(!selectedUrl.startsWith("file:///")){
+                           console.error("请你选择正确的文件路径")
+                           return
+                       }
+                       const filePath = selectedUrl.substring("file:///".length)
+                       const lastSlashIndex = filePath.lastIndexOf("/"); // 获取最后一个斜杠的索引
+                       const secondLastSlashIndex = filePath.lastIndexOf("/", lastSlashIndex - 1); // 获取倒数第二个斜杠的索引
+
+                       const filename = filePath.substring(lastSlashIndex + 1); // 获取文件名(带后缀)
+                       const directory = filePath.substring(secondLastSlashIndex+1, lastSlashIndex); // 获取文件所在的目录
+
+                       root.file = filePath
+                       folderButton.text = directory
+                       fileButton.text = filename
+                       listModel.clear()
+                       loadFile()
+                   }
+               }
+        }
+        // new
+        Item{
+            width: coi.width
+            height: 40
+            FluFilledButton{
+                id: saveItemButton
+                anchors{
+                    right: parent.right
+                    verticalCenter: parent.verticalCenter
+                }
+                text:"New"
+                disabled: root.file === ""? true : false
+                width: parent.width
+                onClicked: {
+                    listModel.append(addFunc())
+                }
+            }
+        }
+        // 保存 文件
+        Item{
+            width: coi.width
+            height: 40
+            FluFilledButton{
+                id: saveFileButton
+                anchors{
+                    right: parent.right
+                    verticalCenter: parent.verticalCenter
+                }
+                text:"Save file"
+                width: parent.width
+                disabled: root.file === ""? true : false
+                onClicked: {
+                    root.saveFunc()
+                }
+            }
+        }
+        //表格视图
+        Item{
+            width: coi.width
+            height: 400
+            ListView{
+                id: listView
+                width: 200
+                height: parent.height
+                anchors{
+                    right: parent.right
+                }
+                model: listModel
+                delegate: listDelegate
+            }
+            Component{
+                id: listDelegate
+                Item{
+                    width: 200
+                    height: 40
+                FluTextBox{
+                    id: itemName
+                    width: parent.width
+                    anchors{
+                        verticalCenter: parent.verticalCenter
+                    }
+                    placeholderText:"item name"
+                    text: model.itemName
+                    validator: RegularExpressionValidator { regularExpression: /^[a-zA-Z0-9]*$/ }
+                    onActiveFocusChanged: {
+                        if(activeFocus){
+                            currentItem = listModel.get(index)
+                        }
+                    }
+                    onEditingFinished: {
+                        model.itemName = text
+                        root.editFunc(model)
+                    }
+                    onAccepted: {
+
+                    }
+                }
+                }
+            }
+        }
+    }
+
+    // 加载文件,前提是文件的路径是加载好的
+    function loadFile(){
+        const getData = ruleFile.read_file(file)
+        var data = []
+        if (getData === "" || typeof getData === "undefined") {
+            console.log("String is empty or undefined.")
+            // 如果是空的表示这个是一个刚刚建立的rule文件
+            return
+        }
+        console.debug('加载文件的数据', getData)
+        data = JSON.parse(getData)
+        for(let item of data){
+            listModel.append(item)
+        }
+
+    }
+
+}

+ 332 - 0
module/gui/qml/Content/ImageRule.qml

@@ -4,6 +4,15 @@ import FluentUI
 import "../Component"
 
 Item {
+
+    property alias aItemName: itemName.text
+    property alias aImageName: imageName.text
+    property alias aRoiFront: roiFront.text
+    property alias aRoiBack: roiBack.text
+    property alias aMethod: method.currentText
+    property alias aThreshold: threshold.text
+    property alias aDescription: description.text
+
     MirrorImage{
         id: mirrorImage
         anchors{
@@ -14,5 +23,328 @@ Item {
         height: 720 + 50
         imageWidth: 1280
         imageHeight: 720
+
+        onRoi_red_changed: function(roi){
+            if(typeof ruleFile.currentItem === "undefined"){
+                return
+            }
+            ruleFile.currentItem.roiFront = roi
+        }
+
+        onRoi_green_changed: function(roi){
+            if(typeof ruleFile.currentItem === "undefined"){
+                return
+            }
+            ruleFile.currentItem.roiBack = roi
+        }
+    }
+    RuleFile{
+        id: ruleFile
+        anchors{
+            left: mirrorImage.right
+            leftMargin: 10
+            right: parent.right
+            top: parent.top
+        }
+        height: 600
+        addFunc: add
+        editFunc: edit
+        saveFunc: save
+
+        onCurrentItemChanged: {
+            if(currentItem === "" || typeof currentItem === "undefined"){
+                return
+            }
+            var [x, y, width, height] = currentItem.roiFront.split(',')
+            mirrorImage.roiRed_x = parseInt(x)
+            mirrorImage.roiRed_y = parseInt(y)
+            mirrorImage.roiRed_width = parseInt(width)
+            mirrorImage.roiRed_height = parseInt(height)
+            var [x, y, width, height] = currentItem.roiBack.split(',')
+            mirrorImage.roiGreen_x = parseInt(x)
+            mirrorImage.roiGreen_y = parseInt(y)
+            mirrorImage.roiGreen_width = parseInt(width)
+            mirrorImage.roiGreen_height = parseInt(height)
+
+        }
+    }
+
+    FluArea {
+        id: ruleArea
+        anchors{
+            left: mirrorImage.right
+            leftMargin: 10
+            right: parent.right
+            top: ruleFile.bottom
+            topMargin: 12
+            bottom: parent.bottom
+        }
+        Column{
+            id: coi
+            anchors{
+                top: parent.top
+                bottom: parent.bottom
+                left: parent.left
+                leftMargin: 20
+                right: parent.right
+                rightMargin: 20
+            }
+            spacing: 5
+            //头
+            Item{
+                width: coi.width
+                height: 40
+                FluText{
+                    anchors.left: parent.left
+                    anchors.verticalCenter: parent.verticalCenter
+                    text: "Editing rule"
+                    font: FluTextStyle.Subtitle
+                }
+            }
+            //item name 这个参数项的名称
+            Item{
+                width: coi.width
+                height: 40
+                FluText{
+                    anchors.left: parent.left
+                    anchors.verticalCenter: parent.verticalCenter
+                    text: "Item name"
+                    font: FluTextStyle.BodyStrong
+                }
+                FluTextBox{
+                    id: itemName
+                    anchors{
+                        right: parent.right
+                        verticalCenter: parent.verticalCenter
+                    }
+                    placeholderText:"单行输入框"
+                    disabled: true
+                    text: (typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.itemName
+                    width: 200
+                }
+            }
+            // 文件名
+            Item{
+                width: coi.width
+                height: 40
+                FluText{
+                    anchors.left: parent.left
+                    anchors.verticalCenter: parent.verticalCenter
+                    text: "Image name"
+                    font: FluTextStyle.BodyStrong
+                }
+                FluTextBox{
+                    id: imageName
+                    anchors{
+                        right: parent.right
+                        verticalCenter: parent.verticalCenter
+                    }
+                    disabled: true
+                    text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.imageName
+                    placeholderText:"Save image name"
+                    width: 200
+                }
+            }
+            // roi front
+            Item{
+                width: coi.width
+                height: 40
+                FluText{
+                    anchors.left: parent.left
+                    anchors.verticalCenter: parent.verticalCenter
+                    text: "Roi front"
+                    font: FluTextStyle.BodyStrong
+                }
+                FluTextBox{
+                    id: roiFront
+                    anchors{
+                        right: parent.right
+                        verticalCenter: parent.verticalCenter
+                    }
+                    disabled: true
+                    text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.roiFront
+                    placeholderText:"0,0,100,100"
+                    width: 200
+                }
+            }
+            //roi back
+            Item{
+                width: coi.width
+                height: 40
+                FluText{
+                    anchors.left: parent.left
+                    anchors.verticalCenter: parent.verticalCenter
+                    text: "Roi Back"
+                    font: FluTextStyle.BodyStrong
+                }
+                FluTextBox{
+                    id: roiBack
+                    anchors{
+                        right: parent.right
+                        verticalCenter: parent.verticalCenter
+                    }
+                    disabled: true
+                    text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.roiBack
+                    placeholderText:"0,0,100,100"
+                    width: 200
+
+
+                }
+            }
+            //找图的方法
+            Item{
+                width: coi.width
+                height: 40
+                FluText{
+                    anchors.left: parent.left
+                    anchors.verticalCenter: parent.verticalCenter
+                    text: "Find method"
+                    font: FluTextStyle.BodyStrong
+                }
+                FComboBox{
+                    id: method
+                    anchors{
+                        right: parent.right
+                        verticalCenter: parent.verticalCenter
+                    }
+                    width: 200
+                    currentIndex: 0
+                    model: ["Template matching"]
+                    onActivated: {
+
+                    }
+                }
+            }
+            //阈值
+            Item{
+                width: coi.width
+                height: 40
+                FluText{
+                    anchors.left: parent.left
+                    anchors.verticalCenter: parent.verticalCenter
+                    text: "Threshod"
+                    font: FluTextStyle.BodyStrong
+                }
+                FluTextBox{
+                    id: threshold
+                    anchors{
+                        right: parent.right
+                        verticalCenter: parent.verticalCenter
+                    }
+                    width: 200
+                    text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.threshold
+                    placeholderText:"0.8"
+                    onEditingFinished: {
+                        if(typeof ruleFile.currentItem === "undefined"){
+                            return
+                        }
+                        ruleFile.currentItem.threshold = parseFloat(text)
+                    }
+                }
+            }
+            //描述 description
+            Item{
+                width: coi.width
+                height: 40
+                FluText{
+                    anchors.left: parent.left
+                    anchors.verticalCenter: parent.verticalCenter
+                    text: "Description"
+                    font: FluTextStyle.BodyStrong
+                }
+                FluTextBox{
+                    id: description
+                    anchors{
+                        right: parent.right
+                        verticalCenter: parent.verticalCenter
+                    }
+                    width: 200
+                    text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.description
+                    placeholderText:"input your description"
+                    onEditingFinished: {
+                        if(typeof ruleFile.currentItem === "undefined"){
+                            return
+                        }
+                        ruleFile.currentItem.description = text
+                    }
+                }
+            }
+
+        }
+
     }
+
+    // 帮助
+    FluArea{
+        id: ruleHelp
+        anchors{
+            left: parent.left
+            right: ruleArea.left
+            rightMargin: 10
+            top: mirrorImage.bottom
+            topMargin: 10
+            bottom: parent.bottom
+        }
+        FluText{
+            leftPadding: 12
+            topPadding: 12
+            text: "红色的框(Roi front)表示这一个项要匹配的图片,绿色的框(Roi back)表示匹配的范围。
+在移动边框时右边的roi并不会实时显示,当切换软件的焦点时更新显示。但是具体的数值是更新的,可以放心的保存截图"
+        }
+        FluFilledButton{
+            anchors{
+                right: parent.right
+                rightMargin: 10
+                top: parent.top
+                topMargin: 10
+            }
+
+            text:"Save image"
+            onClicked: {
+                const i = ruleFile.file.lastIndexOf("/")
+                const imagepath = ruleFile.file.substring(0, i)
+                mirrorImage.save_target_image(ruleFile.currentItem.roiFront, imagepath +"/"+ruleFile.currentItem.imageName + ".png")
+                showSuccess("Save target image")
+            }
+        }
+    }
+
+    function add(){
+        const item ={}
+        item["itemName"] = "new"
+        item["imageName"] = "image name"
+        item["roiFront"] = "0,0,100,100"
+        item["roiBack"] = "0,0,100,100"
+        item["method"] = "Template matching"
+        item["threshold"] = 0.8
+        item["description"] = "description"
+        showSuccess("Add new item")
+        return item
+    }
+
+    function edit(model){
+        model.imageName = ruleFile.folder +"_"+ model.itemName
+    }
+
+    function save(){
+        const data = []
+        for(var i=0; i<ruleFile.list_model.count; i++){
+            const item = ruleFile.list_model.get(i)
+            const itemData = {}
+            itemData["itemName"] = item["itemName"]
+            itemData["imageName"] = item["imageName"]
+            itemData["roiFront"] = item["roiFront"]
+            itemData["roiBack"] = item["roiBack"]
+            itemData["method"] = item["method"]
+            itemData["threshold"] = item["threshold"]
+            itemData["description"] = item["description"]
+            data.push(itemData)
+        }
+
+        ruleFile.rule_file.write_file(ruleFile.file, JSON.stringify(data))
+        showSuccess("Save file")
+
+    }
+
+
 }

+ 22 - 0
module/gui/register_type/paint_image.py

@@ -50,3 +50,25 @@ class PaintImage(QQuickPaintedItem):
             return None
 
     image = Property(QImage, fget=image, fset=set_image, notify=logger.info)
+
+    @Slot(str, str)
+    def save_target_image(self, roi: str, file: str) -> None:
+        """
+        保存目标图片
+        :param file: 保存的文件名
+        :param roi: 截图的范围 文本如"0,0,100,100"
+        :return:
+        """
+        if not roi:
+            return
+        if not isinstance(roi, str):
+            logger.error("roi must be str")
+        if not file:
+            return
+        if not isinstance(file, str):
+            logger.error("file must be str")
+            return
+        x, y, width, height = map(int, roi.split(','))
+        roi_image = self._image.copy(x, y, width, height)
+        roi_image.save(file)
+        logger.info(f"save target image {file}success")

+ 65 - 0
module/gui/register_type/rule_file.py

@@ -0,0 +1,65 @@
+# This Python file uses the following encoding: utf-8
+# @author runhey
+# github https://github.com/runhey
+import os
+import json
+
+from filelock import FileLock
+from PySide6.QtCore import QObject, Slot
+
+from module.logger import logger
+from module.config.atomicwrites import atomic_write
+
+
+
+class RuleFile(QObject):
+
+    def __init__(self):
+        super().__init__()
+
+    @Slot(str, result="QString")
+    def read_file(self, file: str) -> str:
+        """
+        读取json文件,如果不存在则创建
+        :param file: 包含路径
+        :return:
+        """
+        folder = os.path.dirname(file)
+        if not os.path.exists(folder):
+            os.mkdir(folder)
+
+        if not os.path.exists(file):
+            return ""
+
+        _, ext = os.path.splitext(file)
+        lock = FileLock(f"{file}.lock")
+        with lock:
+            logger.info(f'read: {file}')
+            if ext == '.json':
+                with open(file, mode='r', encoding='utf-8') as f:
+                    return f.read()
+            else:
+                logger.error(f"not support {ext} file")
+                return ""
+
+    @Slot(str, str)
+    def write_file(self, file: str, data: str) -> None:
+        """
+        写入json文件,如果不存在则创建
+        :param file: 包含路径
+        :param data: 写入的数据
+        :return:
+        """
+        folder = os.path.dirname(file)
+        if not os.path.exists(folder):
+            os.mkdir(folder)
+
+        _, ext = os.path.splitext(file)
+        lock = FileLock(f"{file}.lock")
+        with lock:
+            logger.info(f'write: {file}')
+            if ext == '.json':
+                with atomic_write(file, overwrite=True, encoding='utf-8') as f:
+                    f.write(data)
+            else:
+                logger.error(f"not support {ext} file")

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

@@ -0,0 +1 @@
+[{"itemName":"new1","imageName":"res_new1","roiFront":"179,241,70,79","roiBack":"595,238,100,100","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"}]