perf: 优化 np.array 为 np.asarray 减少内存拷贝
np.array() 默认会复制数据,而 np.asarray() 只在必要时才复制。 对于只读用途,asarray() 性能更好。 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1dea80d5be
commit
61d72b2b8d
1 file changed
+4
-4
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user