From 61d72b2b8d9c09ed76af66a0e37cd00c263b53d0 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 20 May 2026 22:50:33 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20np.array=20?= =?UTF-8?q?=E4=B8=BA=20np.asarray=20=E5=87=8F=E5=B0=91=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=8B=B7=E8=B4=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit np.array() 默认会复制数据,而 np.asarray() 只在必要时才复制。 对于只读用途,asarray() 性能更好。 Co-Authored-By: Claude Opus 4.5 --- douyin/detection/douyin_pixel.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/douyin/detection/douyin_pixel.py b/douyin/detection/douyin_pixel.py index 7055585..6d62d46 100644 --- a/douyin/detection/douyin_pixel.py +++ b/douyin/detection/douyin_pixel.py @@ -85,7 +85,7 @@ def detect_coupon_popup_background( return False bgr = cv2.cvtColor( # type: ignore[attr-defined] - np.array(pic_rgb), cv2.COLOR_RGB2BGR # type: ignore[attr-defined] + np.asarray(pic_rgb), cv2.COLOR_RGB2BGR # type: ignore[attr-defined] ) roi = bgr[y1:y2, x1:x2] # type: ignore[index] if roi.size == 0: @@ -230,7 +230,7 @@ def detect_redpacket_popup_background( # 裁剪感兴趣区域 roi = pic_rgb.crop((x1, y1, x2, y2)) - arr = np.array(roi) + arr = np.asarray(roi) if arr.size == 0: if return_info: @@ -834,10 +834,10 @@ def detect_live_room_icons_by_pixel(pic_rgb: Image.Image) -> tuple[bool, float]: draw.polygon(polygon_points, fill=255) # 将mask转换为numpy数组 - mask_array = np.array(mask) + mask_array = np.asarray(mask) # 获取原始图像的像素数据 - img_array = np.array(pic_rgb) + img_array = np.asarray(pic_rgb) # 计算多边形内部的总像素数 total_pixels = np.sum(mask_array > 0)