From 8a55fed055ee7d777964c3aaf6a1498fc95bcbe0 Mon Sep 17 00:00:00 2001 From: Refound <137168144+Refound-445@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:04:28 +0800 Subject: [PATCH 1/4] Delete .idea/inspectionProfiles directory --- .idea/inspectionProfiles/profiles_settings.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file From 6fb53ca9e270a5b71446dd5d5ffa8c9ecb0687f1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 27 Nov 2024 15:06:35 +0000 Subject: [PATCH 2/4] chore: pre-commit auto fix [skip ci] --- .../nonebot_plugin_nailongremove/handler.py | 6 +- .../model/hf_detection.py | 14 ++- .../model/target_detection.py | 8 +- .../model/utils/common.py | 90 +++++++++++-------- 4 files changed, 66 insertions(+), 52 deletions(-) diff --git a/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/handler.py b/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/handler.py index 178a792..b48bbd5 100644 --- a/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/handler.py +++ b/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/handler.py @@ -105,7 +105,7 @@ async def handle_function(bot: BaseBot, ev: BaseEvent, msg: UniMsg, session: Uni frames.append(temp_image) except StopIteration: break - commitInfo = process_gif_and_save_jpgs(frames, label, (224,224)) + commitInfo = process_gif_and_save_jpgs(frames, label, (224, 224)) if commitInfo is None: await nailong.finish( f"The new data has been saved to the directory {config.nailong_model_dir}\\records\\{label}, label: {label}.", @@ -137,7 +137,9 @@ async def handle_function(bot: BaseBot, ev: BaseEvent, msg: UniMsg, session: Uni ] if len(template_str_all) == 0: continue - template_str=template_str_all[random.randint(0, len(template_str_all) - 1)] + template_str = template_str_all[ + random.randint(0, len(template_str_all) - 1) + ] mapping = { "$event": ev, "$target": msg.get_target(), diff --git a/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/hf_detection.py b/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/hf_detection.py index e730aa1..0e95ae0 100644 --- a/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/hf_detection.py +++ b/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/hf_detection.py @@ -38,7 +38,6 @@ else: file_path = os.path.join(str(config.nailong_model_dir), FILENAME) model_info = api.model_info(REPO_ID) - def get_file_last_modified_time(file_path): try: timestamp = os.path.getmtime(file_path) @@ -50,7 +49,6 @@ else: except FileNotFoundError: return None - local_time = get_file_last_modified_time(file_path) if local_time is None or model_info.last_modified >= local_time: hf_hub_download( @@ -78,7 +76,7 @@ def _check_single(frame: np.ndarray, is_gif: bool = False) -> CheckSingleResult: input_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) if not os.path.exists( - os.path.join(str(config.nailong_model_dir), "online_temp"), + os.path.join(str(config.nailong_model_dir), "online_temp"), ): os.makedirs(os.path.join(str(config.nailong_model_dir), "online_temp")) image_path = os.path.join( @@ -102,8 +100,8 @@ def _check_single(frame: np.ndarray, is_gif: bool = False) -> CheckSingleResult: ) os.remove(image_path) if ( - "检测到的目标数量: " in result_info - and int(result_info.split("检测到的目标数量: ")[1].split("\n")[0]) < 1 + "检测到的目标数量: " in result_info + and int(result_info.split("检测到的目标数量: ")[1].split("\n")[0]) < 1 ): return CheckSingleResult(ok=False, label=None, extra=frame) if isinstance(result_image, str): @@ -144,9 +142,9 @@ def _check_single(frame: np.ndarray, is_gif: bool = False) -> CheckSingleResult: if pad_w > 0 or pad_h > 0: result_img = result_img[ - pad_h // 2: pad_h // 2 + original_size[1], - pad_w // 2: pad_w // 2 + original_size[0], - ] + pad_h // 2 : pad_h // 2 + original_size[1], + pad_w // 2 : pad_w // 2 + original_size[0], + ] return CheckSingleResult(ok=True, label="nailong", extra=result_img) diff --git a/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/target_detection.py b/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/target_detection.py index 85d2724..ae4f92f 100644 --- a/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/target_detection.py +++ b/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/target_detection.py @@ -83,8 +83,8 @@ class FrameInfo: @run_sync def _check_single( - frame: np.ndarray, - is_gif: bool = False, + frame: np.ndarray, + is_gif: bool = False, ) -> CheckSingleResult[Optional[Detections]]: if is_gif: res = similarity_process(frame) @@ -127,8 +127,8 @@ def _check_single( async def check_single( - frame: np.ndarray, - is_gif: bool = False, + frame: np.ndarray, + is_gif: bool = False, ) -> CheckSingleResult[FrameInfo]: if is_gif: res = await _check_single(frame, True) diff --git a/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/utils/common.py b/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/utils/common.py index 2dc978b..37382f3 100644 --- a/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/utils/common.py +++ b/packages/nonebot-plugin-nailongremove-base/nonebot_plugin_nailongremove/model/utils/common.py @@ -7,13 +7,12 @@ import random import shutil from dataclasses import dataclass, field from typing import Any, Awaitable, Callable, Dict, Generic, Optional, TypeVar - from typing_extensions import TypeAlias import cv2 import numpy as np import torch -import torch.nn.functional as F + from ...config import config from ...frame_source import FrameSource @@ -22,20 +21,24 @@ T = TypeVar("T") device = torch.device("cuda" if torch.cuda.is_available() else "cpu") if config.nailong_similarity_on: - from huggingface_hub import PyTorchModelHubMixin - from torch import nn - import torchvision - from nonebot import logger - import faiss import json - import sklearn + + import faiss + import torchvision + from huggingface_hub import PyTorchModelHubMixin + from nonebot import logger + from torch import nn from torchvision import transforms - transform = transforms.Compose([ - transforms.ToTensor(), - transforms.Normalize(mean=[0.5], std=[0.5]) # Assuming grayscale or single-channel - ]) - + transform = transforms.Compose( + [ + transforms.ToTensor(), + transforms.Normalize( + mean=[0.5], + std=[0.5], + ), # Assuming grayscale or single-channel + ], + ) class MyModel( nn.Module, @@ -44,21 +47,25 @@ if config.nailong_similarity_on: def __init__(self): super().__init__() self.resnet = torchvision.models.resnet18(pretrained=False) - self.resnet.fc = nn.Linear(self.resnet.fc.in_features, 5) # Output dimension is 5 + self.resnet.fc = nn.Linear( + self.resnet.fc.in_features, + 5, + ) # Output dimension is 5 def forward(self, x): return self.resnet(x) - - features_model = MyModel.from_pretrained("refoundd/NailongFeatures", ).to(device) - index_path = config.nailong_model_dir / 'records.index' - json_path = config.nailong_model_dir / 'records.json' + features_model = MyModel.from_pretrained( + "refoundd/NailongFeatures", + ).to(device) + index_path = config.nailong_model_dir / "records.index" + json_path = config.nailong_model_dir / "records.json" if os.path.exists(index_path): index = faiss.read_index(str(index_path)) else: index = faiss.IndexFlatL2(512) if os.path.exists(json_path): - with open(json_path, 'r') as f: + with open(json_path, "r") as f: index_cls = json.load(f) else: index_cls = {} @@ -66,9 +73,10 @@ if config.nailong_similarity_on: try: res = faiss.StandardGpuResources() # 创建GPU资源 index = faiss.index_cpu_to_gpu(res, 0, index) # 将CPU索引转移到GPU - except Exception as e: - logger.warning("load faiss-gpu failed.Please check your GPU device and install faiss-gpu first.") - + except Exception: + logger.warning( + "load faiss-gpu failed.Please check your GPU device and install faiss-gpu first.", + ) def hook(model, input, output): embeddings = input[0] @@ -78,7 +86,6 @@ if config.nailong_similarity_on: d, i = index.search(vector, 1) return 1 - d[0][0], i[0][0], vector - features_model.resnet.fc.register_forward_hook(hook) features_model.eval() @@ -112,9 +119,9 @@ FrameChecker: TypeAlias = Callable[ async def race_check( - checker: FrameChecker[T], - frames: FrameSource, - concurrency: int = config.nailong_concurrency, + checker: FrameChecker[T], + frames: FrameSource, + concurrency: int = config.nailong_concurrency, ) -> Optional[CheckSingleResult[T]]: iterator = iter(frames) if config.nailong_similarity_on: @@ -182,7 +189,11 @@ async def race_check( return None -def similarity_process(image1: np.ndarray, dsize=(224, 224), similarity_threshold=1) -> Optional[CheckSingleResult]: +def similarity_process( + image1: np.ndarray, + dsize=(224, 224), + similarity_threshold=1, +) -> Optional[CheckSingleResult]: # image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB) image1 = cv2.resize(image1, dsize, interpolation=cv2.INTER_LINEAR) image1_tensor = transform(image1).unsqueeze(0).to(device) @@ -198,22 +209,25 @@ def similarity_process(image1: np.ndarray, dsize=(224, 224), similarity_threshol def process_gif_and_save_jpgs(frames, label, dsize=(224, 224), similarity_threshold=1): if ( - len( - list( - glob.glob( - str(config.nailong_model_dir / "records/*/*.jpg") - ), - ), - ) - >= config.nailong_similarity_max_storage and config.nailong_hf_token is not None + len( + list( + glob.glob(str(config.nailong_model_dir / "records/*/*.jpg")), + ), + ) + >= config.nailong_similarity_max_storage + and config.nailong_hf_token is not None ): zip_filename = shutil.make_archive( - config.nailong_model_dir / "{}_records".format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")), + config.nailong_model_dir + / "{}_records".format( + datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"), + ), "zip", - config.nailong_model_dir / "records" + config.nailong_model_dir / "records", ) shutil.rmtree(config.nailong_model_dir / "records") from huggingface_hub import HfApi + api = HfApi() commitInfo = api.upload_file( path_or_fileobj=zip_filename, @@ -255,6 +269,6 @@ def process_gif_and_save_jpgs(frames, label, dsize=(224, 224), similarity_thresh index_cls[str(index.ntotal - 1)] = label count += 1 faiss.write_index(index, str(index_path)) - with open(json_path, 'w') as f: + with open(json_path, "w") as f: json.dump(index_cls, f) return commitInfo From b64b1c491877839bce9319b936e754189784bc85 Mon Sep 17 00:00:00 2001 From: Refound <137168144+Refound-445@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:07:25 +0800 Subject: [PATCH 3/4] Delete .idea/misc.xml .idea/modules.xml .idea/vcs.xml .idea/nonebot-plugin-nailongremove.iml --- .idea/misc.xml | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 .idea/misc.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 060d2c5..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file From b33d06806e2736e2bd8a3b1d42e6fe8e4a5fbae2 Mon Sep 17 00:00:00 2001 From: Refound <137168144+Refound-445@users.noreply.github.com> Date: Wed, 27 Nov 2024 23:07:41 +0800 Subject: [PATCH 4/4] Delete .idea directory --- .idea/.gitignore | 8 -------- .idea/modules.xml | 8 -------- .idea/nonebot-plugin-nailongremove.iml | 8 -------- .idea/vcs.xml | 6 ------ 4 files changed, 30 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/modules.xml delete mode 100644 .idea/nonebot-plugin-nailongremove.iml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 35410ca..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 默认忽略的文件 -/shelf/ -/workspace.xml -# 基于编辑器的 HTTP 客户端请求 -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 2e920b1..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/nonebot-plugin-nailongremove.iml b/.idea/nonebot-plugin-nailongremove.iml deleted file mode 100644 index d0876a7..0000000 --- a/.idea/nonebot-plugin-nailongremove.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file