Răsfoiți Sursa

perf(ocr): Improve OCR speed due to add enlarge_canvas

runhey 3 ani în urmă
părinte
comite
0b6bfb10e5

+ 16 - 0
module/ocr/base_ocr.py

@@ -18,6 +18,21 @@ from module.logger import logger
 
 
 
+def enlarge_canvas(image):
+    """
+    copy from https://github.com/LmeSzinc/StarRailCopilot
+    Enlarge image into a square fill with black background. In the structure of PaddleOCR,
+    image with w:h=1:1 is the best while 3:1 rectangles takes three times as long.
+    Also enlarge into the integer multiple of 32 cause PaddleOCR will downscale images to 1/32.
+    """
+    height, width = image.shape[:2]
+    length = int(max(width, height) // 32 * 32 + 32)
+    border = (0, length - height, 0, length - width)
+    if sum(border) > 0:
+        image = cv2.copyMakeBorder(image, *border, borderType=cv2.BORDER_CONSTANT, value=(0, 0, 0))
+    return image
+
+
 class OcrMode(Enum):
     FULL = 1  # str: "Full"
     SINGLE = 2  # str: "Single"
@@ -154,6 +169,7 @@ class BaseCor:
         start_time = time.time()
         image = self.crop(image, self.roi)
         image = self.pre_process(image)
+        image = enlarge_canvas(image)
 
         # ocr
         boxed_results: list[BoxedResult] = self.model.detect_and_ocr(image)

BIN
tasks/KekkaiActivation/a/a_a_harvest_kaiko_3.png


BIN
tasks/KekkaiActivation/a/a_a_harvest_kaiko_4.png


+ 18 - 0
tasks/KekkaiActivation/a/image.json

@@ -151,5 +151,23 @@
     "method": "Template matching",
     "threshold": 0.8,
     "description": "description"
+  },
+  {
+    "itemName": "a_harvest_kaiko_4",
+    "imageName": "a_a_harvest_kaiko_4.png",
+    "roiFront": "894,164,48,41",
+    "roiBack": "867,132,100,100",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "太鼓4"
+  },
+  {
+    "itemName": "a_harvest_kaiko_3",
+    "imageName": "a_a_harvest_kaiko_3.png",
+    "roiFront": "893,163,47,42",
+    "roiBack": "865,136,100,100",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "description"
   }
 ]

+ 4 - 0
tasks/KekkaiActivation/assets.py

@@ -62,6 +62,10 @@ class KekkaiActivationAssets:
 	I_A_EMPTY = RuleImage(roi_front=(677,255,353,128), roi_back=(677,255,353,128), threshold=0.8, method="Template matching", file="./tasks/KekkaiActivation/a/a_a_empty.png")
 	# description 
 	I_A_CHECK_CARD = RuleImage(roi_front=(513,32,260,49), roi_back=(513,32,260,49), threshold=0.8, method="Template matching", file="./tasks/KekkaiActivation/a/a_a_check_card.png")
+	# 太鼓4 
+	I_A_HARVEST_KAIKO_4 = RuleImage(roi_front=(894,164,48,41), roi_back=(867,132,100,100), threshold=0.8, method="Template matching", file="./tasks/KekkaiActivation/a/a_a_harvest_kaiko_4.png")
+	# description 
+	I_A_HARVEST_KAIKO_3 = RuleImage(roi_front=(893,163,47,42), roi_back=(865,136,100,100), threshold=0.8, method="Template matching", file="./tasks/KekkaiActivation/a/a_a_harvest_kaiko_3.png")
 
 
 	# Image Rule Assets

+ 2 - 0
tasks/KekkaiActivation/script_task.py

@@ -356,6 +356,8 @@ class ScriptTask(KU, KekkaiActivationAssets):
         """
         self.appear_then_click(self.I_A_HARVEST_EXP)
         self.appear_then_click(self.I_A_HARVEST_FISH4)  # 斗鱼的如果一直没有领的话
+        self.appear_then_click(self.I_A_HARVEST_KAIKO_4)
+        self.appear_then_click(self.I_A_HARVEST_KAIKO_3)
 
 if __name__ == "__main__":
     from module.config.config import Config

+ 7 - 2
tasks/Orochi/script_task.py

@@ -332,16 +332,21 @@ if __name__ == '__main__':
     d = Device(c)
     t = ScriptTask(c, d)
 
-    t.run()
+    # t.run()
 
     # t.check_layer('悲')
 
+    from module.base.timer import timer
+
+    @timer
     @profile
     def test_memory():
         t.screenshot()
         print(t.ocr_appear(t.O_O_TEST_OCR))
         print(t.L_LAYER_LIST.image_appear(t.device.image, '悲'))
-    # test_memory()
+    for i in range(4):
+        test_memory()
+        print('=====================')