chore: 完成 wheel 打包配置

This commit is contained in:
XcantloadX 2025-01-23 17:16:42 +08:00
parent 00d8b6021f
commit e1c2fc4db7
8 changed files with 84 additions and 7 deletions

1
MANIFEST.in Normal file
View File

@ -0,0 +1 @@
graft res

View File

@ -104,7 +104,12 @@ def _img2str(image: MatLike | str | None) -> str:
if image is None:
return 'None'
if isinstance(image, str):
return os.path.relpath(image)
try:
return os.path.relpath(image)
except ValueError:
# ValueError: path is on mount 'C:', start on mount 'E:'
# 程序路径与资源路径不在同一个地方的情况
return image
else:
return '<opencv Mat>'

View File

@ -4,7 +4,7 @@ import unicodedata
from os import PathLike
from typing import TYPE_CHECKING, Callable, NamedTuple, overload
from .util import Rect, grayscaled
from .util import Rect, grayscaled, res_path
from .debug import result as debug_result, debug
import cv2
@ -12,13 +12,13 @@ from cv2.typing import MatLike
from rapidocr_onnxruntime import RapidOCR
_engine_jp = RapidOCR(
rec_model_path=r'res\models\japan_PP-OCRv3_rec_infer.onnx',
rec_model_path=res_path('res/models/japan_PP-OCRv3_rec_infer.onnx'),
use_det=True,
use_cls=False,
use_rec=True,
)
_engine_en = RapidOCR(
rec_model_path=r'res\models\en_PP-OCRv3_rec_infer.onnx',
rec_model_path=res_path('res/models/en_PP-OCRv3_rec_infer.onnx'),
use_det=True,
use_cls=False,
use_rec=True,
@ -76,7 +76,7 @@ def _draw_result(image: 'MatLike', result: list[OcrResult]) -> 'MatLike':
# 加载字体
try:
font = ImageFont.truetype(r'res\fonts\SourceHanSansHW-Regular.otf', 16)
font = ImageFont.truetype(res_path('res/fonts/SourceHanSansHW-Regular.otf'), 16)
except:
font = ImageFont.load_default()

View File

@ -1,7 +1,10 @@
import re
import os
import time
import typing
import logging
from time import sleep
from importlib import resources
from functools import lru_cache
from typing import Literal, NamedTuple, Callable, TYPE_CHECKING
@ -13,6 +16,8 @@ if TYPE_CHECKING:
from kotonebot.client.protocol import DeviceABC
from kotonebot.backend.color import HsvColor
logger = logging.getLogger(__name__)
class TaskInfo(NamedTuple):
name: str
description: str
@ -236,3 +241,28 @@ class AdaptiveWait:
def reset(self):
self.interval = self.base_interval
self.start_time = None
package_mode: Literal['wheel', 'standalone'] | None = None
def res_path(path: str) -> str:
"""
返回资源文件的绝对路径
:param path: 资源文件路径必须以 `res/` 开头
"""
global package_mode
if package_mode is None:
if os.path.exists('res'):
package_mode = 'standalone'
else:
package_mode = 'wheel'
ret = path
if package_mode == 'standalone':
ret = os.path.abspath(ret)
else:
# resources.files('kotonebot.res') 返回的就是 res 文件夹的路径
# 但是 path 已经有了 res所以这里需要去掉 res
real_path = resources.files('kotonebot.res') / '..' / path
ret = str(real_path)
logger.debug(f'res_path: {ret}')
return ret

View File

@ -499,7 +499,7 @@ class KotoneBotUI:
def main() -> None:
ui = KotoneBotUI()
app = ui.create_ui()
app.launch()
app.launch(inbrowser=True)
if __name__ == "__main__":
main()

40
pyproject.toml Normal file
View File

@ -0,0 +1,40 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[project]
name = "kaa"
version = "0.1.0.250123"
description = "Kotones Auto Assistant"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
# 图像处理 & OCR
"opencv-python==4.10.0.84",
"rapidocr_onnxruntime==1.4.3",
"av==14.0.1",
"scikit-image==0.25.0",
"thefuzz==0.22.1",
# Adb 控制
"adbutils==2.8.0",
# 可视化调试页面
"fastapi==0.115.6",
"uvicorn==0.34.0",
"python-multipart==0.0.20",
"websockets==14.1",
"numpy==2.2.1",
"psutil==6.1.1",
# GUI
"gradio==5.12.0",
# 配置读写
"pydantic==2.10.4",
# 其他
"typing-extensions==4.12.2"
]
[tool.setuptools]
package-dir = { "kotonebot" = "kotonebot", "kotonebot.res" = "res" }
include-package-data = true
[project.scripts]
kaa = "kotonebot.ui.gr:main"

View File

@ -2,6 +2,7 @@
####### 此文件为自动生成,请勿编辑 #######
####### AUTO GENERATED. DO NOT EDIT. #######
import os
from kotonebot.backend.util import res_path
{% for lang in data -%}
{%- for class_name, attrs in lang.resources.items() -%}

View File

@ -36,7 +36,7 @@ def main():
print(current, attr_name)
parent_class[attr_name] = {
'type': 'image',
'value': 'os.path.abspath("' + rel_path.replace('\\', '/') + '")',
'value': 'res_path("' + rel_path.replace('\\', '/') + '")',
'name': attr_name,
'abspath': abs_path.replace('\\', '/'),
'class_path': class_path,