mirror of
https://github.com/Refound-445/nonebot-plugin-nailongremove.git
synced 2025-11-04 21:22:43 +08:00
up
This commit is contained in:
parent
c118baade8
commit
3148fda51a
@ -176,6 +176,10 @@ plugins = [
|
|||||||
|
|
||||||
## 📝 更新日志
|
## 📝 更新日志
|
||||||
|
|
||||||
|
### 2.1.2
|
||||||
|
|
||||||
|
- 重构部分代码,修复潜在 Bug
|
||||||
|
|
||||||
### 2.1.1
|
### 2.1.1
|
||||||
|
|
||||||
- 新增变量 `$checked_image`
|
- 新增变量 `$checked_image`
|
||||||
|
|||||||
@ -9,7 +9,7 @@ require("nonebot_plugin_uninfo")
|
|||||||
from . import handler as handler
|
from . import handler as handler
|
||||||
from .config import Config
|
from .config import Config
|
||||||
|
|
||||||
__version__ = "2.1.1.post2"
|
__version__ = "2.1.2"
|
||||||
__plugin_meta__ = PluginMetadata(
|
__plugin_meta__ = PluginMetadata(
|
||||||
name="自动撤回奶龙",
|
name="自动撤回奶龙",
|
||||||
description="一个基于图像分类模型的简单插件~",
|
description="一个基于图像分类模型的简单插件~",
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import io
|
import io
|
||||||
from asyncio import Semaphore
|
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
Awaitable,
|
Awaitable,
|
||||||
@ -13,6 +12,7 @@ from typing import (
|
|||||||
TypeVar,
|
TypeVar,
|
||||||
cast,
|
cast,
|
||||||
)
|
)
|
||||||
|
from typing_extensions import TypeAlias
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@ -115,35 +115,35 @@ async def nailong_rule(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def check_frames(
|
CheckFrameResult: TypeAlias = Tuple[bool, Optional[np.ndarray]]
|
||||||
frames: Iterator[np.ndarray],
|
|
||||||
) -> Tuple[bool, Optional[np.ndarray]]:
|
|
||||||
signal = asyncio.Future[Tuple[bool, Optional[np.ndarray]]]()
|
|
||||||
sem = Semaphore(config.nailong_concurrency)
|
|
||||||
|
|
||||||
async def task(f: np.ndarray):
|
|
||||||
try:
|
|
||||||
async with sem:
|
|
||||||
ok = await check_image(f)
|
|
||||||
except Exception as e:
|
|
||||||
signal.set_exception(e)
|
|
||||||
else:
|
|
||||||
if isinstance(ok, bool):
|
|
||||||
ok = (ok, None)
|
|
||||||
signal.set_result(ok)
|
|
||||||
|
|
||||||
async def wait_result():
|
async def check_frames(frames: Iterator[np.ndarray]) -> CheckFrameResult:
|
||||||
nonlocal signal
|
async def worker() -> CheckFrameResult:
|
||||||
ok = await signal
|
while True:
|
||||||
signal = asyncio.Future()
|
try:
|
||||||
return ok
|
frame = next(frames)
|
||||||
|
except StopIteration:
|
||||||
|
return False, None
|
||||||
|
res = await check_image(frame)
|
||||||
|
if not isinstance(res, tuple):
|
||||||
|
res = res, None
|
||||||
|
if res[0]:
|
||||||
|
return res
|
||||||
|
|
||||||
for f in frames:
|
tasks = [asyncio.create_task(worker()) for _ in range(config.nailong_concurrency)]
|
||||||
if (sem.locked() or signal.done()) and (r := await wait_result())[0]:
|
while True:
|
||||||
return r
|
if not tasks:
|
||||||
asyncio.create_task(task(f))
|
break
|
||||||
|
done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
|
||||||
|
for t in done:
|
||||||
|
if (res := t.result())[0]:
|
||||||
|
for pt in pending:
|
||||||
|
pt.cancel()
|
||||||
|
return res
|
||||||
|
tasks = pending
|
||||||
|
|
||||||
return await wait_result()
|
return False, None
|
||||||
|
|
||||||
|
|
||||||
nailong = on_message(rule=Rule(nailong_rule), priority=config.nailong_priority)
|
nailong = on_message(rule=Rule(nailong_rule), priority=config.nailong_priority)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user