feat: click model

This commit is contained in:
xtaodada 2024-11-03 21:13:21 +08:00
parent ea8eba883e
commit d4a9e32d92
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
8 changed files with 223320 additions and 32 deletions

View File

@ -2,3 +2,4 @@ DEBUG=false
DOMAIN= DOMAIN=
HOST=0.0.0.0 HOST=0.0.0.0
PORT=8888 PORT=8888
APPKEY=key

View File

@ -5,8 +5,8 @@ description = "Add your description here"
readme = "README.md" readme = "README.md"
requires-python = ">=3.12" requires-python = ">=3.12"
dependencies = [ dependencies = [
"bili-ticket-gt-python>=0.2.7",
"cryptography>=43.0.3", "cryptography>=43.0.3",
"ddddocr>=1.5.6",
"fastapi>=0.115.4", "fastapi>=0.115.4",
"httpx>=0.27.2", "httpx>=0.27.2",
"numpy>=2.1.3", "numpy>=2.1.3",

View File

@ -1,14 +1,14 @@
# This file was autogenerated by uv via the following command: # This file was autogenerated by uv via the following command:
# uv export -o requirements.txt --no-hashes # uv export -o .\requirements.txt --no-hashes
annotated-types==0.7.0 annotated-types==0.7.0
anyio==4.6.2.post1 anyio==4.6.2.post1
bili-ticket-gt-python==0.2.7
certifi==2024.8.30 certifi==2024.8.30
cffi==1.17.1 ; platform_python_implementation != 'PyPy' cffi==1.17.1 ; platform_python_implementation != 'PyPy'
click==8.1.7 click==8.1.7
colorama==0.4.6 ; platform_system == 'Windows' colorama==0.4.6 ; platform_system == 'Windows'
coloredlogs==15.0.1 coloredlogs==15.0.1
cryptography==43.0.3 cryptography==43.0.3
ddddocr==1.5.6
fastapi==0.115.4 fastapi==0.115.4
flatbuffers==24.3.25 flatbuffers==24.3.25
h11==0.14.0 h11==0.14.0
@ -20,6 +20,7 @@ mpmath==1.3.0
networkx==3.4.2 networkx==3.4.2
numpy==2.1.3 numpy==2.1.3
onnxruntime==1.20.0 onnxruntime==1.20.0
opencv-python-headless==4.10.0.84
packaging==24.1 packaging==24.1
persica==0.1.0a4 persica==0.1.0a4
pillow==11.0.0 pillow==11.0.0

223193
src/assets/siamese.onnx Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,77 @@
import bili_ticket_gt_python from pathlib import Path
import ddddocr
from PIL import Image
from io import BytesIO
import numpy as np
import onnxruntime as ort
from persica.factory.component import AsyncInitializingComponent from persica.factory.component import AsyncInitializingComponent
assets_path = Path("src") / "assets"
model_path = assets_path / "siamese.onnx"
class ClickModel(AsyncInitializingComponent): class ClickModel(AsyncInitializingComponent):
def __init__(self): def __init__(self):
self.click = bili_ticket_gt_python.ClickPy() self.det = ddddocr.DdddOcr(det=True, ocr=False)
self.siamese = ort.InferenceSession(model_path)
def predict(self, url: str): def get_icons(self, image_bytes: bytes):
key = self.click.calculate_key(url) def remove_subicon(data):
if key: result = []
return key.split(",") for i in data:
return [] remove = False
for j in data:
if (
i != j
and i[0] >= j[0]
and i[1] >= j[1]
and i[2] <= j[2]
and i[3] <= j[3]
):
remove = True
if not remove:
result.append(i)
return result
img = Image.open(BytesIO(image_bytes))
bboxes = self.det.detection(image_bytes)
small_bboxes = [i for i in bboxes if i[1] > 300]
big_bboxes = [i for i in bboxes if i[1] <= 300]
small_bboxes = sorted(small_bboxes, key=lambda x: x[0])
big_bboxes = remove_subicon(big_bboxes)
small_images = [img.crop(i) for i in small_bboxes]
big_images = [img.crop(i) for i in big_bboxes]
return big_bboxes, small_images, big_images
def calculate_similarity(self, img1: "Image", img2: "Image"):
def preprocess_image(img, size=(105, 105)):
img_resized = img.resize(size)
img_normalized = np.array(img_resized) / 255.0
img_transposed = np.transpose(img_normalized, (2, 0, 1))
img_expanded = np.expand_dims(img_transposed, axis=0).astype(np.float32)
return img_expanded
image_data_1 = preprocess_image(img1)
image_data_2 = preprocess_image(img2)
inputs = {"input": image_data_1, "input.53": image_data_2}
output = self.siamese.run(None, inputs)
output_sigmoid = 1 / (1 + np.exp(-output[0]))
similarity_score = output_sigmoid[0][0]
return similarity_score
def predict(self, image_bytes: bytes):
big_bboxes, small_images, big_images = self.get_icons(image_bytes)
ans = []
for i in small_images:
similarities = [self.calculate_similarity(i, j) for j in big_images]
target_bbox = big_bboxes[similarities.index(max(similarities))]
x = (target_bbox[0] + target_bbox[2]) / 2
y = (target_bbox[1] + target_bbox[3]) / 2
ans.append((x, y))
return ans

View File

@ -6,7 +6,7 @@ from fastapi import FastAPI
from persica.factory.component import AsyncInitializingComponent from persica.factory.component import AsyncInitializingComponent
from starlette.middleware.trustedhost import TrustedHostMiddleware from starlette.middleware.trustedhost import TrustedHostMiddleware
from src.env import DOMAIN, DEBUG, PORT from src.env import DOMAIN, DEBUG, HOST, PORT
class WebApp(AsyncInitializingComponent): class WebApp(AsyncInitializingComponent):
@ -27,7 +27,7 @@ class WebApp(AsyncInitializingComponent):
async def start(self): async def start(self):
self.init_web() self.init_web()
self.web_server = uvicorn.Server( self.web_server = uvicorn.Server(
config=uvicorn.Config(self.app, host="0.0.0.0", port=PORT) config=uvicorn.Config(self.app, host=HOST, port=PORT)
) )
server_config = self.web_server.config server_config = self.web_server.config
server_config.setup_event_loop() server_config.setup_event_loop()

View File

@ -36,17 +36,22 @@ class PredictPlugin(AsyncInitializingComponent):
self.click_model = click_model self.click_model = click_model
self.resnet_model = resnet_model self.resnet_model = resnet_model
def get_point_list(self, pic_content: bytes, pic_url: str, pic_type: str): def get_point_list(self, pic_content: bytes, _: str, pic_type: str):
point_list = [] point_list = []
if pic_type == "nine": if pic_type == "nine":
# 九宫格 # 九宫格
points = self.resnet_model.predict(pic_content) points = self.resnet_model.predict(pic_content)
for x, y in points: for x, y in points:
point_list.append(f"{x}_{y}") point_list.append(f"{x}_{y}")
elif pic_type == "icon":
# 点按
points = self.click_model.predict(pic_content)
for x, y in points:
left = round(x / 333 * 10000)
top = round(y / 333 * 10000)
point_list.append(f"{left}_{top}")
else: else:
# 滑动验证码 return []
key = self.click_model.predict(pic_url)
point_list = key
return point_list return point_list
def vvv(self, crack: Crack, retry: int = 3): def vvv(self, crack: Crack, retry: int = 3):

56
uv.lock
View File

@ -1,8 +1,12 @@
version = 1 version = 1
requires-python = ">=3.12" requires-python = ">=3.12"
resolution-markers = [ resolution-markers = [
"python_full_version < '3.13'", "python_full_version < '3.13' and platform_system == 'Darwin'",
"python_full_version >= '3.13'", "python_full_version < '3.13' and platform_machine == 'aarch64' and platform_system == 'Linux'",
"(python_full_version < '3.13' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version < '3.13' and platform_system != 'Darwin' and platform_system != 'Linux')",
"python_full_version >= '3.13' and platform_system == 'Darwin'",
"python_full_version >= '3.13' and platform_machine == 'aarch64' and platform_system == 'Linux'",
"(python_full_version >= '3.13' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version >= '3.13' and platform_system != 'Darwin' and platform_system != 'Linux')",
] ]
[[package]] [[package]]
@ -27,18 +31,6 @@ wheels = [
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/e4/f5/f2b75d2fc6f1a260f340f0e7c6a060f4dd2961cc16884ed851b0d18da06a/anyio-4.6.2.post1-py3-none-any.whl", hash = "sha256:6d170c36fba3bdd840c73d3868c1e777e33676a69c3a72cf0a0d5d6d8009b61d", size = 90377 }, { url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/e4/f5/f2b75d2fc6f1a260f340f0e7c6a060f4dd2961cc16884ed851b0d18da06a/anyio-4.6.2.post1-py3-none-any.whl", hash = "sha256:6d170c36fba3bdd840c73d3868c1e777e33676a69c3a72cf0a0d5d6d8009b61d", size = 90377 },
] ]
[[package]]
name = "bili-ticket-gt-python"
version = "0.2.7"
source = { registry = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple" }
sdist = { url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/41/21/2cba4545e796dd3f2544e36d2b84f1ba69e8b39d3a317e23677e51f4477e/bili_ticket_gt_python-0.2.7.tar.gz", hash = "sha256:f087bdfa18e971b1f7c8da26dacab2e69578293207a30da58f50f1ef3d5c9c7e", size = 261373 }
wheels = [
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/e9/12/6a2df55e98f8ebf7f983a892f0083e90ab8f2b65640c9002e461067aa481/bili_ticket_gt_python-0.2.7-cp312-cp312-macosx_10_12_x86_64.whl", hash = "sha256:000198cc643b0a8358711006662d6207bfbe2a8b53a26bc61bfa51c7a8429576", size = 87622391 },
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/ce/a0/8f531e5fcab92936f71cf1133e08aaaf3bd3fa8bb51409a6e4611bd0935c/bili_ticket_gt_python-0.2.7-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:3c17222e1b454e196c3a58795640b1d48519f65036780e5776221f0762902f49", size = 86557330 },
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/7f/b5/03b7850c36b21c4161a4dd4cae2f22bb48f29596f9903ea0337b8e943edb/bili_ticket_gt_python-0.2.7-cp312-cp312-manylinux_2_35_x86_64.whl", hash = "sha256:638aff60325d5294d7ee375abf4f490b9c4492c5af27dab6954f00804ba7b614", size = 93546997 },
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/62/9d/5bfd2a1af11983f7c3ae99b2ff6cb2f2d8cb7df68c87ba9cdb55e5934841/bili_ticket_gt_python-0.2.7-cp312-none-win_amd64.whl", hash = "sha256:0a24bf2b567f5814eb8a699faf29a142a39c98f9078bfef11efd8a598b565b45", size = 85581487 },
]
[[package]] [[package]]
name = "certifi" name = "certifi"
version = "2024.8.30" version = "2024.8.30"
@ -143,6 +135,21 @@ wheels = [
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/87/5c/3dab83cc4aba1f4b0e733e3f0c3e7d4386440d660ba5b1e3ff995feb734d/cryptography-43.0.3-cp39-abi3-win_amd64.whl", hash = "sha256:0c580952eef9bf68c4747774cde7ec1d85a6e61de97281f2dba83c7d2c806362", size = 3068026 }, { url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/87/5c/3dab83cc4aba1f4b0e733e3f0c3e7d4386440d660ba5b1e3ff995feb734d/cryptography-43.0.3-cp39-abi3-win_amd64.whl", hash = "sha256:0c580952eef9bf68c4747774cde7ec1d85a6e61de97281f2dba83c7d2c806362", size = 3068026 },
] ]
[[package]]
name = "ddddocr"
version = "1.5.6"
source = { registry = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple" }
dependencies = [
{ name = "numpy" },
{ name = "onnxruntime" },
{ name = "opencv-python-headless" },
{ name = "pillow" },
]
sdist = { url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/0e/cf/1243d5f0d03763a287375366f68eadb5c14418f5b3df00c09eb971e526a7/ddddocr-1.5.6.tar.gz", hash = "sha256:2839a940bfabe02e3284ef3f9d2a037292aa9f641f355b43a9b70bece9e1b73d", size = 75825027 }
wheels = [
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/54/74/418c1c0be49463799f9eeb307a8aa4013ff5fca5e0387f0ef2762fcdb4e2/ddddocr-1.5.6-py3-none-any.whl", hash = "sha256:f13865b00e42de5c2507c1889ba73c2bacd218a49d15b928c2a5c82667062ac5", size = 75868010 },
]
[[package]] [[package]]
name = "fastapi" name = "fastapi"
version = "0.115.4" version = "0.115.4"
@ -171,8 +178,8 @@ name = "geetestbypass"
version = "0.1.0" version = "0.1.0"
source = { virtual = "." } source = { virtual = "." }
dependencies = [ dependencies = [
{ name = "bili-ticket-gt-python" },
{ name = "cryptography" }, { name = "cryptography" },
{ name = "ddddocr" },
{ name = "fastapi" }, { name = "fastapi" },
{ name = "httpx" }, { name = "httpx" },
{ name = "numpy" }, { name = "numpy" },
@ -188,8 +195,8 @@ dependencies = [
[package.metadata] [package.metadata]
requires-dist = [ requires-dist = [
{ name = "bili-ticket-gt-python", specifier = ">=0.2.7" },
{ name = "cryptography", specifier = ">=43.0.3" }, { name = "cryptography", specifier = ">=43.0.3" },
{ name = "ddddocr", specifier = ">=1.5.6" },
{ name = "fastapi", specifier = ">=0.115.4" }, { name = "fastapi", specifier = ">=0.115.4" },
{ name = "httpx", specifier = ">=0.27.2" }, { name = "httpx", specifier = ">=0.27.2" },
{ name = "numpy", specifier = ">=2.1.3" }, { name = "numpy", specifier = ">=2.1.3" },
@ -344,6 +351,23 @@ wheels = [
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/ca/4f/d1d7642706ddce8c253255b52cf8a6fdb6d4aca171a7a476188039816b79/onnxruntime-1.20.0-cp313-cp313t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:dc4e7c10c98c1f407835448c26a7e14ebff3234f131e1fbc53bd9500c828df89", size = 13319499 }, { url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/ca/4f/d1d7642706ddce8c253255b52cf8a6fdb6d4aca171a7a476188039816b79/onnxruntime-1.20.0-cp313-cp313t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:dc4e7c10c98c1f407835448c26a7e14ebff3234f131e1fbc53bd9500c828df89", size = 13319499 },
] ]
[[package]]
name = "opencv-python-headless"
version = "4.10.0.84"
source = { registry = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple" }
dependencies = [
{ name = "numpy" },
]
sdist = { url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/2f/7e/d20f68a5f1487adf19d74378d349932a386b1ece3be9be9915e5986db468/opencv-python-headless-4.10.0.84.tar.gz", hash = "sha256:f2017c6101d7c2ef8d7bc3b414c37ff7f54d64413a1847d89970b6b7069b4e1a", size = 95117755 }
wheels = [
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/1c/9b/583c8d9259f6fc19413f83fd18dd8e6cbc8eefb0b4dc6da52dd151fe3272/opencv_python_headless-4.10.0.84-cp37-abi3-macosx_11_0_arm64.whl", hash = "sha256:a4f4bcb07d8f8a7704d9c8564c224c8b064c63f430e95b61ac0bffaa374d330e", size = 54835657 },
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/c0/7b/b4c67f5dad7a9a61c47f7a39e4050e8a4628bd64b3c3daaeb755d759f928/opencv_python_headless-4.10.0.84-cp37-abi3-macosx_12_0_x86_64.whl", hash = "sha256:5ae454ebac0eb0a0b932e3406370aaf4212e6a3fdb5038cc86c7aea15a6851da", size = 56475470 },
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/91/61/f838ce2046f3ec3591ea59ea3549085e399525d3b4558c4ed60b55ed88c0/opencv_python_headless-4.10.0.84-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:46071015ff9ab40fccd8a163da0ee14ce9846349f06c6c8c0f2870856ffa45db", size = 29329705 },
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/d1/09/248f86a404567303cdf120e4a301f389b68e3b18e5c0cc428de327da609c/opencv_python_headless-4.10.0.84-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:377d08a7e48a1405b5e84afcbe4798464ce7ee17081c1c23619c8b398ff18295", size = 49858781 },
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/30/c0/66f88d58500e990a9a0a5c06f98862edf1d0a3a430781218a8c193948438/opencv_python_headless-4.10.0.84-cp37-abi3-win32.whl", hash = "sha256:9092404b65458ed87ce932f613ffbb1106ed2c843577501e5768912360fc50ec", size = 28675298 },
{ url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/26/d0/22f68eb23eea053a31655960f133c0be9726c6a881547e6e9e7e2a946c4f/opencv_python_headless-4.10.0.84-cp37-abi3-win_amd64.whl", hash = "sha256:afcf28bd1209dd58810d33defb622b325d3cbe49dcd7a43a902982c33e5fad05", size = 38754031 },
]
[[package]] [[package]]
name = "packaging" name = "packaging"
version = "24.1" version = "24.1"