最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python的百度OCR代码报错:TypeError string indices must be integers

Python crifan 600浏览 0评论
折腾:
【未解决】用Vivo的iQOO U1x重新测试VivoGame的魔域类型的游戏20201208
期间报错:
[201209 17:57:18][BaiduOCR.py 108] respJson={'log_id': 1644317052777815721, 'error_code': 282000, 'error_msg': 'internal error'}
[201209 17:57:18][AppCrawler.py 100] string indices must be integers
    Traceback (most recent call last):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 90, in start
        self.set_InitialUrl()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 173, in set_InitialUrl
        self.doGameAutoTest()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 796, in doGameAutoTest
        self.gameAutomation_SequentialCheck()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 806, in gameAutomation_SequentialCheck
        while (curRetryNum <= intoHomePageCheckMaxRetryNum) and (not self.waitStartToHome(curRetryNum)):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 254, in waitStartToHome
        while self.isBlackPage():
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/common/MainUtils.py", line 5182, in isBlackPage
        wordsResultNum = respJson["words_result_num"]
    TypeError: string indices must be integers
发现是代码:
src/common/MainUtils.py
    def isBlackPage(self):
        """is black page or not"""
        screenImgPath = self.getCurScreenshot()
        respJson = self.baiduImageToWords(screenImgPath)
        allWordsEmpty = True
。。。
        wordsResultNum = respJson["words_result_num"]
那:此处respJson看来是None?
去log中看看
[201209 17:57:18][BaiduOCR.py 108] respJson={'log_id': 1644317052777815721, 'error_code': 282000, 'error_msg': 'internal error'}
且注意到:
[201209 17:57:18][BaiduOCR.py 140] OCR time: 0:00:07.267716
此处百度OCR耗时高达7秒才返回
说明要么内容多,要么有问题
前面的log是:
[201209 17:57:18][BaiduOCR.py 105] baidu OCR: imgage=debug/Android/app/游戏app/screenshot/20201209_175710.jpg -> respJson={'log_id': 1644317052777815721, 'error_code': 282000, 'error_msg': 'internal error’}
去看看图片是啥样的:
是黑屏:
所以此处按道理应该返回空的。
不过无所谓,需要代码支持这种情况
去加上:
src/common/BaiduOCR.py
        # respEmpty = None
        respEmpty = {}
。。。
            # {'error_code': 282000, 'error_msg': 'internal error', 'log_id': 1644317052777815721}
。。。
            else:
                errMsg = "Unknown error"
                if "error_msg" in respJson:
                    errMsg = respJson["error_msg"]
                logging.error("baidu OCR resp error: %s", errMsg)
                respJson = respEmpty
改为:
src/common/MainUtils.py
    def isBlackPage(self):
        """is black page or not"""
        screenImgPath = self.getCurScreenshot()
        respJson = self.baiduImageToWords(screenImgPath)
        if respJson:
            allWordsEmpty = True


            wordsResultNum = respJson["words_result_num"]
            if wordsResultNum > 0:
                allWordsEmpty = False


            isBlack = False
            if allWordsEmpty:
                isBlack = True
        else:
            # empty resp consider as Black page
            isBlack = True


        logging.info("isBlack=%s, imgPath=%s", isBlack, screenImgPath)
        return isBlack
即可。
只不过,其他地方对于baiduImageToWords返回空时,也需要去注意处理即可。

转载请注明:在路上 » 【已解决】Python的百度OCR代码报错:TypeError string indices must be integers

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.188 seconds, using 23.41MB memory