13
13
14
14
from src .ocr_utils import OCRUtils as OCR
15
15
from src .image_utils import ImageUtils
16
+ from src .mouse_key import MouseKey
16
17
from src .filectl import FileCtl
17
18
from src .dogtail_utils import DogtailUtils
18
19
from src .custom_exception import TemplateElementNotFound
@@ -398,12 +399,11 @@ def assert_ocr_exist(
398
399
timeout : [int , float ] = None ,
399
400
max_match_number : int = None ,
400
401
mode : str = "all" ,
402
+ bbox : dict = None ,
401
403
):
402
404
"""
403
405
断言文案存在
404
- :param args:
405
- 目标字符,识别一个字符串或多个字符串,并返回其在图片中的坐标;
406
- 如果不传参,返回图片中识别到的所有字符串。
406
+ :param args: 目标字符,识别一个字符串或多个字符串。
407
407
:param picture_abspath: 要识别的图片路径,如果不传默认截取全屏识别。
408
408
:param similarity: 匹配度。
409
409
:param return_first: 只返回第一个,默认为 False,返回识别到的所有数据。
@@ -413,10 +413,54 @@ def assert_ocr_exist(
413
413
:param timeout: 最大匹配超时,单位秒
414
414
:param max_match_number: 最大匹配次数
415
415
:param mode: "all" or "any",all 表示识别所有目标字符,any 表示识别任意一个目标字符,默认值为 all
416
+ :param bbox:
417
+ 接收一个字典,包含一个区域,在区域内进行识别,用于干扰较大时提升OCR识别精准度
418
+ 字典字段:
419
+ start_x: 开始 x 坐标(左上角)
420
+ start_y: 开始 y 坐标(左上角)
421
+ w: 宽度
422
+ h: 高度
423
+ end_x: 结束 x 坐标(右下角)
424
+ end_y: 结束 y 坐标(右下角)
425
+ 注意 : end_x + end_y 与 w + h 为互斥关系, 必须且只能传入其中一组
426
+ 示例:
427
+ {start_x=0, start_y=0, w=100, h=100}
428
+ {start_x=0, start_y=0, end_x=100, end_y=100}
416
429
"""
430
+
431
+ if len (args ) == 0 :
432
+ raise ValueError ("缺少 ocr 断言关键字" )
433
+
417
434
pic = None
418
435
if picture_abspath is not None :
419
436
pic = picture_abspath + ".png"
437
+
438
+ resolution = MouseKey .screen_size ()
439
+ if bbox is not None :
440
+ start_x = bbox .get ("start_x" ) if bbox .get ("start_x" ) is not None else None
441
+ start_y = bbox .get ("start_y" ) if bbox .get ("start_y" ) is not None else None
442
+ w = bbox .get ("w" ) if bbox .get ("w" ) is not None else None
443
+ h = bbox .get ("h" ) if bbox .get ("h" ) is not None else None
444
+ end_x = bbox .get ("end_x" ) if bbox .get ("end_x" ) is not None else None
445
+ end_y = bbox .get ("end_y" ) if bbox .get ("end_y" ) is not None else None
446
+
447
+ if start_x is None or start_y is None :
448
+ raise ValueError ("缺失 start_x 或 start_y 坐标" )
449
+
450
+ wh_provided = w is not None and h is not None
451
+ end_xy_provided = end_x is not None and end_y is not None
452
+
453
+ if not (wh_provided ^ end_xy_provided ):
454
+ raise ValueError ("end_x + end_y 与 w + h 为互斥关系, 必须且只能传入其中一组" )
455
+
456
+ if end_xy_provided :
457
+ w = end_x - start_x
458
+ h = end_y - start_y
459
+ picture_abspath = ImageUtils .save_temporary_picture (start_x , start_y , w , h )
460
+ pic = picture_abspath + ".png"
461
+
462
+ resolution = f"{ start_x , start_y } -> { w , h } "
463
+
420
464
res = OCR .ocr (
421
465
* args ,
422
466
picture_abspath = pic ,
@@ -430,7 +474,10 @@ def assert_ocr_exist(
430
474
)
431
475
if res is False :
432
476
raise AssertionError (
433
- (f"通过OCR未识别到:{ args } " , f"{ pic if pic else GlobalConfig .SCREEN_CACHE } " )
477
+ (
478
+ f"通过OCR在范围[{ resolution } ]未识别到:{ args } " ,
479
+ f"{ pic if pic else GlobalConfig .SCREEN_CACHE } " ,
480
+ )
434
481
)
435
482
if isinstance (res , tuple ):
436
483
pass
@@ -440,14 +487,14 @@ def assert_ocr_exist(
440
487
res = filter (lambda x : x [1 ] is False , res .items ())
441
488
raise AssertionError (
442
489
(
443
- f"通过OCR未识别到 :{ dict (res )} " ,
490
+ f"通过OCR在范围[ { resolution } ]未识别到 :{ dict (res )} " ,
444
491
f"{ pic if pic else GlobalConfig .SCREEN_CACHE } " ,
445
492
)
446
493
)
447
494
elif mode == "any" and len (res ) == list (res .values ()).count (False ):
448
495
raise AssertionError (
449
496
(
450
- f"通过OCR未识别到 :{ args } 中的任意一个" ,
497
+ f"通过OCR在范围[ { resolution } ]未识别到 :{ args } 中的任意一个" ,
451
498
f"{ pic if pic else GlobalConfig .SCREEN_CACHE } " ,
452
499
)
453
500
)
@@ -463,11 +510,67 @@ def assert_ocr_not_exist(
463
510
pause : [int , float ] = None ,
464
511
timeout : [int , float ] = None ,
465
512
max_match_number : int = None ,
513
+ bbox : dict = None ,
466
514
):
467
- """断言文案不存在"""
515
+ """
516
+ 断言文案不存在
517
+ :param args: 目标字符,识别一个字符串或多个字符串。
518
+ :param picture_abspath: 要识别的图片路径,如果不传默认截取全屏识别。
519
+ :param similarity: 匹配度。
520
+ :param return_first: 只返回第一个,默认为 False,返回识别到的所有数据。
521
+ :param lang: `ch`, `en`, `fr`, `german`, `korean`, `japan`
522
+ :param network_retry: 连接服务器重试次数
523
+ :param pause: 重试间隔时间,单位秒
524
+ :param timeout: 最大匹配超时,单位秒
525
+ :param max_match_number: 最大匹配次数
526
+ :param bbox:
527
+ 接收一个字典,包含一个区域,在区域内进行识别,用于干扰较大时提升OCR识别精准度
528
+ 字典字段:
529
+ start_x: 开始 x 坐标(左上角)
530
+ start_y: 开始 y 坐标(左上角)
531
+ w: 宽度
532
+ h: 高度
533
+ end_x: 结束 x 坐标(右下角)
534
+ end_y: 结束 y 坐标(右下角)
535
+ 注意 : end_x + end_y 与 w + h 为互斥关系, 必须且只能传入其中一组
536
+ 示例:
537
+ {start_x=0, start_y=0, w=100, h=100}
538
+ {start_x=0, start_y=0, end_x=100, end_y=100}
539
+ """
540
+
541
+ if len (args ) == 0 :
542
+ raise ValueError ("缺少 ocr 断言关键字" )
543
+
468
544
pic = None
469
545
if picture_abspath is not None :
470
546
pic = picture_abspath + ".png"
547
+
548
+ resolution = MouseKey .screen_size ()
549
+ if bbox is not None :
550
+ start_x = bbox .get ("start_x" ) if bbox .get ("start_x" ) is not None else None
551
+ start_y = bbox .get ("start_y" ) if bbox .get ("start_y" ) is not None else None
552
+ w = bbox .get ("w" ) if bbox .get ("w" ) is not None else None
553
+ h = bbox .get ("h" ) if bbox .get ("h" ) is not None else None
554
+ end_x = bbox .get ("end_x" ) if bbox .get ("end_x" ) is not None else None
555
+ end_y = bbox .get ("end_y" ) if bbox .get ("end_y" ) is not None else None
556
+
557
+ if start_x is None or start_y is None :
558
+ raise ValueError ("缺失 start_x 或 start_y 坐标" )
559
+
560
+ wh_provided = w is not None and h is not None
561
+ end_xy_provided = end_x is not None and end_y is not None
562
+
563
+ if not (wh_provided ^ end_xy_provided ):
564
+ raise ValueError ("end_x + end_y 与 w + h 为互斥关系, 必须且只能传入其中一组" )
565
+
566
+ if end_xy_provided :
567
+ w = end_x - start_x
568
+ h = end_y - start_y
569
+ picture_abspath = ImageUtils .save_temporary_picture (start_x , start_y , w , h )
570
+ pic = picture_abspath + ".png"
571
+
572
+ resolution = f"{ start_x , start_y } -> { w , h } "
573
+
471
574
res = OCR .ocr (
472
575
* args ,
473
576
picture_abspath = pic ,
@@ -484,15 +587,18 @@ def assert_ocr_not_exist(
484
587
elif isinstance (res , tuple ):
485
588
raise AssertionError (
486
589
(
487
- f"通过ocr识别到不应存在的文案 { res } " ,
590
+ f"通过ocr在范围[ { resolution } ]识别到不应存在的文案 { res } " ,
488
591
f"{ pic if pic else GlobalConfig .SCREEN_CACHE } " ,
489
592
)
490
593
)
491
- elif isinstance (res , dict ) and True in res .values ():
492
- res = filter (lambda x : x [1 ] is not False , res .items ())
493
- raise AssertionError (
494
- (
495
- f"通过OCR识别到不应存在的文案:{ dict (res )} " ,
496
- f"{ pic if pic else GlobalConfig .SCREEN_CACHE } " ,
594
+ elif isinstance (res , dict ):
595
+ if all (value is False for value in res .values ()):
596
+ pass
597
+ else :
598
+ res = filter (lambda x : x [1 ] is not False , res .items ())
599
+ raise AssertionError (
600
+ (
601
+ f"通过OCR在范围[{ resolution } ]识别到不应存在的文案:{ dict (res )} " ,
602
+ f"{ pic if pic else GlobalConfig .SCREEN_CACHE } " ,
603
+ )
497
604
)
498
- )
0 commit comments