Ver código fonte

1、格式化文本时,尽量前面加u,否则会出现IO Error。
2、疑似总理:getThresholdDict传递的可能不是option本身,而是其first_parent

scbc.sat2 5 anos atrás
pai
commit
1563a5f5bd

+ 18 - 18
ssat_sdk/MenuTree3/OptionConfig.py

@@ -17,21 +17,21 @@ class COptionConfig(TConfig, CBaseLog):
     def __init__(self, menuTreeDir, pathManager):
         self.__pathManager = pathManager
         if self.__pathManager is None:
-            self.error("路径参数错误:None")
+            self.error(u"路径参数错误:None")
         # 状态:False表示路径值失败;
         self.status = False
         # menuTree目录;
         self.__uiTreeDir = menuTreeDir
         # 判断是否为目录;
         if not os.path.isdir(self.__uiTreeDir):
-            self.error("%s 不是menuTree目录" % self.__uiTreeDir)
+            self.error(u"%s 不是menuTree目录" % self.__uiTreeDir)
         # 判断目录是否存在;
         if not os.path.exists(self.__uiTreeDir):
-            self.error("menuTree(%s)目录不存在" % self.__uiTreeDir)
+            self.error(u"menuTree(%s)目录不存在" % self.__uiTreeDir)
         # 判断配置文件是否存在;
         self.__configPath = os.path.join(self.__uiTreeDir, "menutree.ini")
         if not os.path.exists(self.__configPath):
-            self.error("menutree配置文件不存在:%s" % self.__configPath)
+            self.error(u"menutree配置文件不存在:%s" % self.__configPath)
             self.status = True
         # 初始化父类;
         TConfig.__init__(self, self.__configPath)
@@ -51,7 +51,7 @@ class COptionConfig(TConfig, CBaseLog):
             waitTime = float(self.get_value("waitTime", optionName))
         except Exception:
             waitTime = 1.0
-            self.error("获取%s界面的等待界面时间失败,使用默认值1.0s" % str(parent))
+            self.error(u"获取%s界面的等待界面时间失败,使用默认值1.0s" % str(parent))
         return waitTime
 
     # 获取阀值字典;
@@ -75,7 +75,7 @@ class COptionConfig(TConfig, CBaseLog):
         paths = self.__pathManager.get_option_paths(optionName)
         # 判断路径节点是否空;
         if paths.__len__() == 0:
-            self.error("当前【%s】的路径节点空,使用默认的配置值%s" % (optionName, def_cfg))
+            self.error(u"当前【%s】的路径节点空,使用默认的配置值%s" % (optionName, def_cfg))
             return False, def_cfg
 
         # 获取first parent;
@@ -97,22 +97,22 @@ class COptionConfig(TConfig, CBaseLog):
         # 首配图片判断是否存在,不存在取用次配图片路径;
         if not os.path.exists(icon_path):
             # 使用次配图片,父级配图;
-            self.warn("Option(%s)首配图片不存在:%s" % (optionName, icon_path))
+            self.warn(u"Option(%s)首配图片不存在:%s" % (optionName, icon_path))
             icon_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".png")
             icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".dir.png")
             opc_cfg = self.get_value_dict(cur_level, cur_parent)
-            self.warn("Option(%s)首配图片不存在,尝试使用次配图(%s):%s,配置%s" % (optionName, cur_parent, icon_path, opc_cfg))
+            self.warn(u"Option(%s)首配图片不存在,尝试使用次配图(%s):%s,配置%s" % (optionName, cur_parent, icon_path, opc_cfg))
             # 如果次配都不存在,使用顶层配图;
             if not os.path.exists(icon_path):
-                self.warn("Option(%s)次配图片不存在:%s" % (optionName, icon_path))
+                self.warn(u"Option(%s)次配图片不存在:%s" % (optionName, icon_path))
                 # 使用顶层配图(first parent)
                 icon_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".png")
                 icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".dir.png")
                 opc_cfg = self.get_value_dict(cur_level, first_parent)
-                self.warn("Option(%s)次配图片不存在,尝试使用顶层配图(%s):%s,配置%s" % (optionName, first_parent, icon_path, opc_cfg))
+                self.warn(u"Option(%s)次配图片不存在,尝试使用顶层配图(%s):%s,配置%s" % (optionName, first_parent, icon_path, opc_cfg))
                 # 如果顶层配图不存在,退出;
                 if not os.path.exists(icon_path):
-                    self.error("%s对应的顶层菜单配图不存在%s,使用默认配置%s" % (optionName, icon_path, def_cfg))
+                    self.error(u"%s对应的顶层菜单配图不存在%s,使用默认配置%s" % (optionName, icon_path, def_cfg))
                     return False, def_cfg
                 # endif
             # endif
@@ -139,11 +139,11 @@ class COptionConfig(TConfig, CBaseLog):
         paths = self.__pathManager.get_option_paths(optionName)
         # 判断路径节点是否空;
         if paths.__len__() == 0:
-            self.error("当前【%s】的路径节点空,将使用默认的ocr配置%s" % (optionName, def_orc))
+            self.error(u"当前【%s】的路径节点空,将使用默认的ocr配置%s" % (optionName, def_orc))
         else:
             # 如果状态False,退出;
             if self.status is False:
-                self.warn("配置文件(%s)不存在,%s使用默认的ocr配置%s" % (self.__configPath, optionName, def_orc))
+                self.warn(u"配置文件(%s)不存在,%s使用默认的ocr配置%s" % (self.__configPath, optionName, def_orc))
             else:
                 # 读取指定的ocr配置信息;
                 for i in range(g_level.index(paths.__len__() - 1), -1, -1):
@@ -153,26 +153,26 @@ class COptionConfig(TConfig, CBaseLog):
                     # 是否有当前option的ocr配置;
                     if self.has_option(cur_level, optionName + '.ocr'):
                         ocr_dict = self.get_dict(self.get_value(cur_level, optionName + '.ocr'))
-                        self.warn("%s使用自身的ocr配置%s" % (optionName, ocr_dict))
+                        self.warn(u"%s使用自身的ocr配置%s" % (optionName, ocr_dict))
                         break
                     # 如果option本身没有配置,获取其父节点的配置;
                     if self.has_option(cur_level, cur_parent + '.ocr'):
                         ocr_dict = self.get_dict(self.get_value(cur_level, cur_parent + '.ocr'))
-                        self.warn("%s使用父节点%s的ocr配置%s" % (optionName, cur_parent, ocr_dict))
+                        self.warn(u"%s使用父节点%s的ocr配置%s" % (optionName, cur_parent, ocr_dict))
                         break
                     # 如果option父级没有配置,则获取顶层配置(first parent);
                     if self.has_option(cur_level, first_parent + '.ocr'):
                         ocr_dict = self.get_dict(self.get_value(cur_level, first_parent + '.ocr'))
-                        self.warn("%s使用顶层节点%s的ocr配置%s" % (optionName, first_parent, ocr_dict))
+                        self.warn(u"%s使用顶层节点%s的ocr配置%s" % (optionName, first_parent, ocr_dict))
                         break
                 # end-for
             # end-if
         # end-if
         if ocr_dict.__len__() == 0:
             ocr_dict = def_orc
-            self.warn("无有效的ocr配置,将使用默认的ocr配置%s" % def_orc)
+            self.warn(u"无有效的ocr配置,将使用默认的ocr配置%s" % def_orc)
 
-        self.info("%s使用的ocr配置=%s" % (optionName, ocr_dict))
+        self.info(u"%s使用的ocr配置=%s" % (optionName, ocr_dict))
 
         return ocr_dict
 

+ 7 - 7
ssat_sdk/MenuTree3/OptionFocus.py

@@ -54,10 +54,10 @@ class COptionFocus(CBaseLog):
             src_img = cv.imread(src_pic)
             if src_img is not None:
                 self.__tvResolution = [src_img.shape[1], src_img.shape[0]]
-                self.info("获取图片【%s】分辨率成功,使用图片分辨率%s做为当前电视分辨率" % (src_pic, self.__tvResolution))
+                self.info(u"获取图片【%s】分辨率成功,使用图片分辨率%s做为当前电视分辨率" % (src_pic, self.__tvResolution))
             else:
                 self.__tvResolution = [1920, 1080]
-                self.error("获取图片【%s】分辨率失败,将使用默认分辨率%s做为当前电视分辨率" % (src_pic, self.__tvResolution))
+                self.error(u"获取图片【%s】分辨率失败,将使用默认分辨率%s做为当前电视分辨率" % (src_pic, self.__tvResolution))
 
     '''
     函数:根据option的配置内容,在指定源图上查找焦点框;
@@ -138,13 +138,13 @@ class COptionFocus(CBaseLog):
             result, opc_cfg = self.__configManager.getOptionICONConfig(curOptionName)
 
         if result is False:
-            self.warn("获取【%s】的icon配置失败,将使用聚集框作为文本框坐标" % curOptionName)
+            self.warn(u"获取【%s】的icon配置失败,将使用聚集框作为文本框坐标" % curOptionName)
             return curFocusBox
 
         dir_path = opc_cfg['dir_path']
         # 判断方位图是否存在,不存在使用默认聚集框作为文本框;
         if os.path.exists(dir_path) is False:
-            self.warn("dir图片%s不存在,将使用聚集框作为文本框坐标" % dir_path)
+            self.warn(u"dir图片%s不存在,将使用聚集框作为文本框坐标" % dir_path)
             return curFocusBox
 
         # 判断该dir是否已读取过(减少文件读写),读取过直接使用记录;
@@ -163,12 +163,12 @@ class COptionFocus(CBaseLog):
         # 没有记录,否读取图片并记录;
         dir_img = cv.imread(dir_path)
         if dir_img is None:
-            self.error("读取【%s】失败,将使用聚集框作为文本框坐标" % dir_path)
+            self.error(u"读取【%s】失败,将使用聚集框作为文本框坐标" % dir_path)
             return curFocusBox
 
         result, focusBox = self.findFocusByIcon(dir_path, curOptionName, isValueSheet)
         if result is False:
-            self.error("在dir图片【%s】查找聚集框失败,将使用聚集框作为文本框坐标" % dir_path)
+            self.error(u"在dir图片【%s】查找聚集框失败,将使用聚集框作为文本框坐标" % dir_path)
             return curFocusBox
 
         # 记录dir对应的icon等信息, 以备下次使用;
@@ -186,7 +186,7 @@ class COptionFocus(CBaseLog):
 if __name__ == "__main__":
     tData = CTData()
     upath = UITPathManage(tData)
-    opcfg = CConfigManager(r'D:\SAT\resource\MenuTree\RT2851\2851', upath)
+    opcfg = COptionConfig(r'D:\SAT\resource\MenuTree\RT2851\2851', upath)
     opfocus = COptionFocus(opcfg)
 
     res, Box = opfocus.findFocusByIcon("", "picture")

+ 46 - 13
ssat_sdk/MenuTree3/OptionOCR.py

@@ -7,7 +7,7 @@ import cv2 as cv
 import json
 from BaseLog import CBaseLog
 from OptionConfig import COptionConfig
-from ssat_sdk import OCRConvert, ImageCMP
+from ssat_sdk import OCRConvert, ImageCMP, getSATTmpDIR
 
 # 测试加头文件;
 from TData import CTData
@@ -30,47 +30,80 @@ class COptionOCR(CBaseLog):
     '''
     函数:获取指定原图中指定的文本区域内的文本内容;
     参数:
+        optionName: Option名称;
         src_pid:    原图文件路径;
         texb_box:   原图中要获取文本内容的区域坐标;
         ocr_config: 使用何种方式识别文本;   示例:{"lan": "ChinesePRC+English", "type": 4}
     返回:
         返回文本内容;
     '''
-    def getOptionFocusTextByFile(self, src_pic, text_box, ocr_config):
-        src_img = cv.imread(src_pic)
+    def getOptionText(self, optionName, src_pic, text_box, ocr_config):
+        src_img = src_pic
+        # 如果是字符串,认为是路径;
+        if type(src_pic) == str:
+            src_img = cv.imread(src_pic)
+
+        # 判断图片对象是否空;
         if src_img is None:
-            self.error("读取图片失败:%s" % src_pic)
+            self.error(u"读取图片失败:%s" % src_pic)
             return None
 
-        return self.getOptionFocusText(src_img, text_box, ocr_config)
+        return self.__getOptionText(optionName, src_img, text_box, ocr_config)
     
     '''
     函数:获取指定原图中指定的文本区域内的文本内容;
     参数:
+        optionName: Option名称;
         src_img:    原图图片对象(cv2.imread的结果值);
         texb_box:   原图中要获取文本内容的区域坐标;
         ocr_config: 使用何种方式识别文本;   示例:{"lan": "ChinesePRC+English", "type": 4}
     返回:
         返回文本内容;
     '''
-    def getOptionFocusText(self, src_img, text_box, ocr_config):
-        pass
+    def __getOptionText(self, optionName, src_img, text_box, ocr_config):
+        if text_box.__len__() == 0:
+            self.error(u"【%s】文本区域空" % optionName)
+            return None
 
+        # 配置文本图片路径,保存文本区域截图;
+        text_pic = os.path.join(getSATTmpDIR(), "meuttree_area_text.png")
+        self.__imgCMP.saveCropImage(src_img, text_pic, text_box)
+        # 判断文本图片是否存在;
+        if not os.path.exists(text_pic):
+            self.error(u"【%s】保存文本区域截图失败" % optionName)
+            return None
+        
+        # 获取ocr识别时阈值(疑似问题:不是使用optionName,而是对应的first_parent)
+        # 示例:threshold = {"Threshold":[25,255,0]}
+        threshold = self.__optionConfig.getThresholdDict(optionName)
+
+        # 识别文本图片内容;
+        optionText = self.__ocr.getStrWithImgProcess(text_pic, threshold, ocr_config['lan'], ocr_config['type'], reconTimes=1)
+
+        self.info(u"【%s】识别结果=%s"%(optionName, optionText))
+        # 返回结果;
+        return optionText
 
     '''
     函数:识别指定图片文件的文本内容;
     参数:
         text_pic:      要识别文本的图片路径;
         ocr_config:     使用何种方式识别文本;   示例:{"lan": "ChinesePRC+English", "type": 4}
+        threshold:      orc识别时的阈值,示例:{"Threshold":[25,255,0]}
     返回:
     '''
-    def getImageText(self, text_pic, ocr_config):
-        text_img = cv.imread(text_pic)
-        if text_img is None:
-            self.error("读取图片失败:%s" % text_pic)
+    def getImageText(self, text_pic, ocr_config, threshold):
+        # 判断文本图片是否存在;
+        if not os.path.exists(text_pic):
+            self.error(u"【%s】文本图片不存在" % text_pic)
             return None
         
-        return self.getImageText(text_img, ocr_config)
+         # 识别文本图片内容;
+        imgText = self.__ocr.getStrWithImgProcess(text_pic, threshold, ocr_config['lan'], ocr_config['type'], reconTimes=1)
+
+        self.info(u"【%s】识别结果=%s"%(text_pic, imgText))
+        # 返回结果;
+        return imgText
 
     
 # end-class
@@ -79,5 +112,5 @@ class COptionOCR(CBaseLog):
 if __name__ == "__main__":
     tData = CTData()
     upath = UITPathManage(tData)
-    opcfg = CoptionConfig(r'D:\SAT\resource\MenuTree\RT2851\2851', upath)
+    opcfg = COptionConfig(r'D:\SAT\resource\MenuTree\RT2851\2851', upath)
     opfocus = COptionOCR(opcfg, upath)