-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ocr): 添加精准识别 OCR 功能并更新相关配置 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- 新增 GeneralAccurateOCR 类,提供更精准的 OCR 识别功能 - 更新 .cnb.yml 文件中的制品库地址 - 修改 mzapi/tencent/ocr/__init__.py,引入新的 OCR 模块 - 更新 mzapi/__init__.py,增加新功能并修改版本号 - 调整 Dockerfile 中的安装命令 -移除 GitHub Actions 中的阿里云 PyPI 上传步骤 - 更新 setup.py 文件版本号
- 修改发布到 PyPI 的条件,只在 push 或合并 pull request 时执行 - 更新代码同步到多个仓库的条件,只在 push 或合并 pull request时执行 - 修复提取版本号的正则表达式 - 优化 GitHub Release 创建步骤 - 统一配置 Git 用户信息的方式
|
|
Reviewer's GuideThis PR implements precise OCR capabilities by adding a new GeneralAccurateOCR client, refactors the existing Basic OCR logging logic, overhauls CI workflows to trigger only on push or merged PRs with improved release steps, bumps the package version to 0.0.5, and corrects auxiliary configuration typos. Entity relationship diagram for all exports in mzapi packageerDiagram
mzapi {
string __version__
string __author__
string __email__
list __all__
}
mzapi ||--o| GeneralBasicOCR : exports
mzapi ||--o| GeneralAccurateOCR : exports
Class diagram for new and updated OCR clientsclassDiagram
class GeneralBasicOCR {
- logger
- cred
- client
__init__(secret_id, secret_key, token, log_level=None)
recognize(ImageBase64, ImageUrl, Scene, LanguageType, IsPdf, PdfPageNumber, IsWords)
}
class GeneralAccurateOCR {
- logger
- cred
- client
- validate_url
__init__(secret_id, secret_key, token, log_level=None)
recognize(ImageBase64, ImageUrl, IsWords, EnableDetectSplit, IsPdf, PdfPageNumber, EnableDetectText, ConfigID)
}
GeneralAccurateOCR --> ImageValidator
Class diagram for ImageValidator usage in GeneralAccurateOCRclassDiagram
class ImageValidator {
validate_url(url, formats)
}
GeneralAccurateOCR --> ImageValidator
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issues in the PR:
-
Missing Newline at End of File:
.cnb.yml.github/workflows/publish-to-pypi.yml.github/workflows/release.ymlmzapi/tencent/ocr/GeneralAccurateOCR.pymzapi/tencent/ocr/__init__.py
Files should always end with a newline.
-
Typo in Dockerfile:
.ide/Dockerfile
-RUN apt-get update && apt-get install -y curl wget unzip openssh-server sduo +RUN apt-get update && apt-get install -y curl wget unzip openssh-server sudo
-
Inconsistent Indentation and Formatting:
mzapi/tencent/ocr/GeneralAccurateOCR.py
def recognize(self,ImageBase64,ImageUrl,IsWords,EnableDetectSplit,IsPdf,PdfPageNumber,EnableDetectText,ConfigID):
The parameters should be spaced properly for better readability:
def recognize(self, ImageBase64, ImageUrl, IsWords, EnableDetectSplit, IsPdf, PdfPageNumber, EnableDetectText, ConfigID):
-
Unnecessary Commented Code:
.github/workflows/publish-to-pypi.yml
- - name: Upload package to Aliyun PyPI repository - run: | - twine upload --repository-url https://packages.aliyun.com/686a57a36024b2147d89fbc0/pypi/repo-ssctu -u ${{secrets.USERNAME }} -p ${{ secrets.PASSWORD }} dist/mzapi_python-${{ env.version }}-py3-none-any.whl - env: - ALIYUN_PYPI_USERNAME: ${{ secrets.USERNAME }} - ALIYUN_PYPI_PASSWORD: ${{ secrets.PASSWORD }}
If this code is no longer needed, it should be removed instead of being commented out.
-
Inconsistent Logging Levels:
mzapi/tencent/ocr/GeneralBasicOCR.pymzapi/tencent/ocr/GeneralAccurateOCR.py
Ensure that the logging levels and their usage are consistent across both files.
-
Hardcoded Secrets:
- Ensure that no secrets are hardcoded in the code. Although it seems like secrets are being used from GitHub secrets, double-check to ensure no secrets are exposed.
Recommendations:
- Ensure all files end with a newline.
- Fix the typo in the Dockerfile.
- Improve the readability of function parameters by adding spaces.
- Remove any unnecessary commented-out code.
- Ensure consistent logging practices across the codebase.
- Double-check for any hardcoded secrets and ensure they are securely managed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @xiaomizhoubaobei, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
此拉取请求主要引入了腾讯云OCR服务的精准识别功能,扩展了SDK的能力。同时,它还包含了一些维护性更新,如PyPI发布配置的调整、Dockerfile中拼写错误的修正以及日志配置的优化,旨在提升项目的可用性和稳定性。
Highlights
- 新增精准识别 OCR 功能: 引入了 GeneralAccurateOCR 类,为腾讯云OCR服务提供了更精确的识别能力,并将其集成到SDK中。
- 更新 PyPI 发布配置: 修改了 .cnb.yml 文件中 PyPI 制品库的 URL,以指向新的发布路径。
- 修复 Dockerfile 中的拼写错误: 将 .ide/Dockerfile 中的 sduo 命令更正为正确的 sudo,确保了容器环境的正确配置。
- 版本号更新: 将项目版本从 0.0.4 升级到 0.0.5,反映了新功能的加入和现有代码的优化。
- 优化日志级别设置: 调整了 GeneralBasicOCR 类的初始化逻辑,现在 log_level 默认为 None,即默认不输出日志,提供了更灵活的日志控制。
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
xiaomizhoubaobei
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 感谢您提交首个Pull Request!我们感谢您的贡献,团队将尽快审核您的代码。
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Consider extracting the common logging initialization logic in both OCR clients into a shared helper to avoid code duplication.
- The GeneralAccurateOCR.recognize signature has many positional parameters—refactor to use keyword-only args or a config object for better readability and maintainability.
- Your GitHub Actions workflows repeat the same
if: github.event.pull_request.mergedconditions across multiple jobs—consider using reusable workflows or YAML anchors to DRY up the CI configuration.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the common logging initialization logic in both OCR clients into a shared helper to avoid code duplication.
- The GeneralAccurateOCR.recognize signature has many positional parameters—refactor to use keyword-only args or a config object for better readability and maintainability.
- Your GitHub Actions workflows repeat the same `if: github.event.pull_request.merged` conditions across multiple jobs—consider using reusable workflows or YAML anchors to DRY up the CI configuration.
## Individual Comments
### Comment 1
<location> `mzapi/tencent/ocr/GeneralAccurateOCR.py:78` </location>
<code_context>
+ self.logger.info("开始执行OCR识别")
+ self.logger.debug(f"输入参数: ImageBase64={ImageBase64}, ImageUrl={ImageUrl}, IsWords={IsWords}, EnableDetectSplit={EnableDetectSplit}, IsPdf={IsPdf}, PdfPageNumber={PdfPageNumber}, EnableDetectText={EnableDetectText}, ConfigID={ConfigID}")
+
+ if ImageBase64 is None and ImageUrl is None:
+ error_msg = "ImageBase64和ImageUrl必须提供一个"
+ self.logger.error(error_msg)
</code_context>
<issue_to_address>
The check for both ImageBase64 and ImageUrl being None is appropriate, but does not handle empty strings.
Update the condition to also check for empty strings to ensure a valid image source is provided.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
if ImageBase64 is None and ImageUrl is None:
error_msg = "ImageBase64和ImageUrl必须提供一个"
self.logger.error(error_msg)
raise ValueError(error_msg)
=======
if (ImageBase64 is None or str(ImageBase64).strip() == "") and (ImageUrl is None or str(ImageUrl).strip() == ""):
error_msg = "ImageBase64和ImageUrl必须提供一个"
self.logger.error(error_msg)
raise ValueError(error_msg)
>>>>>>> REPLACE
</suggested_fix>
### Comment 2
<location> `mzapi/tencent/ocr/GeneralAccurateOCR.py:83` </location>
<code_context>
+ self.logger.error(error_msg)
+ raise ValueError(error_msg)
+
+ if ImageUrl:
+ self.logger.debug(f"验证图片URL: {ImageUrl}")
+ self.validate_url.validate_url(ImageUrl, ["png", "jpg", "jpeg", "bmp", "pdf"])
+ self.logger.debug("图片URL验证通过")
+ req = models.GeneralAccurateOCRRequest()
+ params = {
</code_context>
<issue_to_address>
ImageValidator is only used for ImageUrl, not for ImageBase64.
If ImageBase64 has specific format or size requirements, add validation to ensure compliance with API expectations and improve user feedback.
</issue_to_address>
<suggested_fix>
<<<<<<< SEARCH
if ImageUrl:
self.logger.debug(f"验证图片URL: {ImageUrl}")
self.validate_url.validate_url(ImageUrl, ["png", "jpg", "jpeg", "bmp", "pdf"])
self.logger.debug("图片URL验证通过")
=======
if ImageBase64:
self.logger.debug("验证图片Base64格式")
import base64
try:
# 检查是否为有效的Base64字符串
base64_bytes = base64.b64decode(ImageBase64, validate=True)
# 检查大小限制(例如腾讯OCR API最大支持4MB)
max_size_bytes = 4 * 1024 * 1024
if len(base64_bytes) > max_size_bytes:
error_msg = "ImageBase64图片大小超过4MB限制"
self.logger.error(error_msg)
raise ValueError(error_msg)
except Exception as e:
error_msg = f"ImageBase64格式无效: {str(e)}"
self.logger.error(error_msg)
raise ValueError(error_msg)
self.logger.debug("图片Base64验证通过")
if ImageUrl:
self.logger.debug(f"验证图片URL: {ImageUrl}")
self.validate_url.validate_url(ImageUrl, ["png", "jpg", "jpeg", "bmp", "pdf"])
self.logger.debug("图片URL验证通过")
>>>>>>> REPLACE
</suggested_fix>
### Comment 3
<location> `mzapi/tencent/ocr/GeneralAccurateOCR.py:87` </location>
<code_context>
+ self.logger.debug(f"验证图片URL: {ImageUrl}")
+ self.validate_url.validate_url(ImageUrl, ["png", "jpg", "jpeg", "bmp", "pdf"])
+ self.logger.debug("图片URL验证通过")
+ req = models.GeneralAccurateOCRRequest()
+ params = {
+ "ImageBase64": ImageBase64,
</code_context>
<issue_to_address>
No validation is performed for other parameters such as IsWords, EnableDetectSplit, etc.
Validate the types and acceptable values for parameters like IsWords and EnableDetectSplit before the API call to improve error handling.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| if ImageBase64 is None and ImageUrl is None: | ||
| error_msg = "ImageBase64和ImageUrl必须提供一个" | ||
| self.logger.error(error_msg) | ||
| raise ValueError(error_msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: The check for both ImageBase64 and ImageUrl being None is appropriate, but does not handle empty strings.
Update the condition to also check for empty strings to ensure a valid image source is provided.
| if ImageBase64 is None and ImageUrl is None: | |
| error_msg = "ImageBase64和ImageUrl必须提供一个" | |
| self.logger.error(error_msg) | |
| raise ValueError(error_msg) | |
| if (ImageBase64 is None or str(ImageBase64).strip() == "") and (ImageUrl is None or str(ImageUrl).strip() == ""): | |
| error_msg = "ImageBase64和ImageUrl必须提供一个" | |
| self.logger.error(error_msg) | |
| raise ValueError(error_msg) |
| self.logger.debug(f"验证图片URL: {ImageUrl}") | ||
| self.validate_url.validate_url(ImageUrl, ["png", "jpg", "jpeg", "bmp", "pdf"]) | ||
| self.logger.debug("图片URL验证通过") | ||
| req = models.GeneralAccurateOCRRequest() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: No validation is performed for other parameters such as IsWords, EnableDetectSplit, etc.
Validate the types and acceptable values for parameters like IsWords and EnableDetectSplit before the API call to improve error handling.
| self.logger.info("OCR客户端初始化完成") | ||
| except Exception as e: | ||
| self.logger.error(f"初始化失败: {str(e)}") | ||
| raise TencentCloudSDKException("初始化失败", str(e)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Explicitly raise from a previous error (raise-from-previous-error)
| raise TencentCloudSDKException("初始化失败", str(e)) | |
| raise TencentCloudSDKException("初始化失败", str(e)) from e |
| raise err | ||
| except Exception as e: | ||
| self.logger.error(f"处理OCR请求时发生意外错误: {str(e)}", exc_info=True) | ||
| raise TencentCloudSDKException("OCR处理错误", str(e)) No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Explicitly raise from a previous error (raise-from-previous-error)
| raise TencentCloudSDKException("OCR处理错误", str(e)) | |
| raise TencentCloudSDKException("OCR处理错误", str(e)) from e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
本次代码审查主要关注新增的精准识别 OCR 功能。整体来看,功能实现完整,但新代码在遵循 Python 编码规范(PEP 8)、日志处理和代码效率方面有提升空间。我提供了一些具体的代码建议来改进命名、优化请求构建过程,并使日志配置更加健壮且避免副作用。此外,我还指出了日志配置逻辑在不同类之间的重复问题,并建议进行重构。这些修改将有助于提高代码的可读性、性能和可维护性。
| def recognize(self,ImageBase64,ImageUrl,IsWords,EnableDetectSplit,IsPdf,PdfPageNumber,EnableDetectText,ConfigID): | ||
| """' | ||
| :param ImageBase64: 图片/PDF的 Base64 值。要求图片经Base64编码后不超过 10M,分辨率建议600*800以上,支持PNG、JPG、JPEG、BMP、PDF格式。图片的 ImageUrl、ImageBase64 必须提供一个,如果都提供,只使用 ImageUrl。 | ||
| :param ImageUrl: 图片/PDF的 Url 地址。要求图片经Base64编码后不超过10M,分辨率建议600*800以上,支持PNG、JPG、JPEG、BMP、PDF格式。图片下载时间不超过 3 秒。图片存储于腾讯云的 Url 可保障更高的下载速度和稳定性,建议图片存储于腾讯云。非腾讯云存储的 Url 速度和稳定性可能受一定影响。 | ||
| :param IsWords: 是否返回单字信息,默认关 | ||
| :param EnableDetectSplit: 是否开启原图切图检测功能,开启后可提升“整图面积大,但单字符占比面积小”(例如:试卷)场景下的识别效果,默认关 | ||
| :param IsPdf: 是否开启PDF识别,默认值为false,开启后可同时支持图片和PDF的识别。 | ||
| :param PdfPageNumber: 需要识别的PDF页面的对应页码,仅支持PDF单页识别,当上传文件为PDF且IsPdf参数值为true时有效,默认值为1。 | ||
| :param EnableDetectText: 文本检测开关,默认为true。设置为false可直接进行单行识别,适用于仅包含正向单行文本的图片场景。 | ||
| :param ConfigID: 配置ID支持: OCR -- 通用场景 MulOCR--多语种场景 | ||
| """ | ||
| try: | ||
| self.logger.info("开始执行OCR识别") | ||
| self.logger.debug(f"输入参数: ImageBase64={ImageBase64}, ImageUrl={ImageUrl}, IsWords={IsWords}, EnableDetectSplit={EnableDetectSplit}, IsPdf={IsPdf}, PdfPageNumber={PdfPageNumber}, EnableDetectText={EnableDetectText}, ConfigID={ConfigID}") | ||
|
|
||
| if ImageBase64 is None and ImageUrl is None: | ||
| error_msg = "ImageBase64和ImageUrl必须提供一个" | ||
| self.logger.error(error_msg) | ||
| raise ValueError(error_msg) | ||
|
|
||
| if ImageUrl: | ||
| self.logger.debug(f"验证图片URL: {ImageUrl}") | ||
| self.validate_url.validate_url(ImageUrl, ["png", "jpg", "jpeg", "bmp", "pdf"]) | ||
| self.logger.debug("图片URL验证通过") | ||
| req = models.GeneralAccurateOCRRequest() | ||
| params = { | ||
| "ImageBase64": ImageBase64, | ||
| "ImageUrl":ImageUrl, | ||
| "IsWords":IsWords, | ||
| "EnableDetectSplit":EnableDetectSplit, | ||
| "IsPdf":IsPdf, | ||
| "PdfPageNumber":PdfPageNumber, | ||
| "EnableDetectText":EnableDetectText, | ||
| "ConfigID":ConfigID | ||
| } | ||
| req.from_json_string(json.dumps(params)) | ||
| self.logger.info("正在向腾讯云OCR API发送请求...") | ||
| resp = self.client.GeneralAccurateOCR(req) | ||
| self.logger.info("OCR识别请求成功完成") | ||
| self.logger.debug(f"响应数据: {resp.to_json_string()}") | ||
| return resp.to_json_string() | ||
|
|
||
| except TencentCloudSDKException as err: | ||
| self.logger.error(f"OCR识别失败: {str(err)}", exc_info=True) | ||
| raise err | ||
| except Exception as e: | ||
| self.logger.error(f"处理OCR请求时发生意外错误: {str(e)}", exc_info=True) | ||
| raise TencentCloudSDKException("OCR处理错误", str(e)) No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个方法存在几个可以改进的地方,以提高代码质量和可维护性:
- 命名规范: 根据 PEP 8 编码规范,Python 函数的参数和变量名应使用
snake_case(例如image_base64),而不是PascalCase(例如ImageBase64)。这有助于提高代码的可读性和与 Python 生态系统的一致性。1 - Docstring 格式: 文档字符串 (docstring) 的起始
"""后多了一个单引号,应予以修正。 - 日志记录: 在调试日志中直接记录完整的
ImageBase64字符串可能会导致日志文件变得非常庞大,并可能暴露敏感数据。更好的做法是只记录其是否存在或其长度。 - 请求对象构建: 通过
json.dumps和req.from_json_string来构造请求对象,相比直接设置属性,效率较低且不够直观。
以下是结合了上述建议的重构版本,它还为参数添加了默认值 None,这与项目中其他类的做法保持了一致。
def recognize(self, image_base64=None, image_url=None, is_words=None, enable_detect_split=None, is_pdf=None, pdf_page_number=None, enable_detect_text=None, config_id=None):
"""
:param image_base64: 图片/PDF的 Base64 值。要求图片经Base64编码后不超过 10M,分辨率建议600*800以上,支持PNG、JPG、JPEG、BMP、PDF格式。图片的 ImageUrl、ImageBase64 必须提供一个,如果都提供,只使用 ImageUrl。
:param image_url: 图片/PDF的 Url 地址。要求图片经Base64编码后不超过10M,分辨率建议600*800以上,支持PNG、JPG、JPEG、BMP、PDF格式。图片下载时间不超过 3 秒。图片存储于腾讯云的 Url 可保障更高的下载速度和稳定性,建议图片存储于腾讯云。非腾讯云存储的 Url 速度和稳定性可能受一定影响。
:param is_words: 是否返回单字信息,默认关
:param enable_detect_split: 是否开启原图切图检测功能,开启后可提升“整图面积大,但单字符占比面积小”(例如:试卷)场景下的识别效果,默认关
:param is_pdf: 是否开启PDF识别,默认值为false,开启后可同时支持图片和PDF的识别。
:param pdf_page_number: 需要识别的PDF页面的对应页码,仅支持PDF单页识别,当上传文件为PDF且IsPdf参数值为true时有效,默认值为1。
:param enable_detect_text: 文本检测开关,默认为true。设置为false可直接进行单行识别,适用于仅包含正向单行文本的图片场景。
:param config_id: 配置ID支持: OCR -- 通用场景 MulOCR--多语种场景
"""
try:
self.logger.info("开始执行OCR识别")
self.logger.debug(f"输入参数: image_base64 is set: {image_base64 is not None}, image_url={image_url}, is_words={is_words}, enable_detect_split={enable_detect_split}, is_pdf={is_pdf}, pdf_page_number={pdf_page_number}, enable_detect_text={enable_detect_text}, config_id={config_id})
if image_base64 is None and image_url is None:
error_msg = "image_base64 和 image_url 必须提供一个"
self.logger.error(error_msg)
raise ValueError(error_msg)
if image_url:
self.logger.debug(f"验证图片URL: {image_url}")
self.validate_url.validate_url(image_url, ["png", "jpg", "jpeg", "bmp", "pdf"])
self.logger.debug("图片URL验证通过")
req = models.GeneralAccurateOCRRequest()
req.ImageBase64 = image_base64
req.ImageUrl = image_url
req.IsWords = is_words
req.EnableDetectSplit = enable_detect_split
req.IsPdf = is_pdf
req.PdfPageNumber = pdf_page_number
req.EnableDetectText = enable_detect_text
req.ConfigID = config_id
self.logger.info("正在向腾讯云OCR API发送请求...")
resp = self.client.GeneralAccurateOCR(req)
self.logger.info("OCR识别请求成功完成")
self.logger.debug(f"响应数据: {resp.to_json_string()}")
return resp.to_json_string()
except TencentCloudSDKException as err:
self.logger.error(f"OCR识别失败: {str(err)}", exc_info=True)
raise err
except Exception as e:
self.logger.error(f"处理OCR请求时发生意外错误: {str(e)}", exc_info=True)
raise TencentCloudSDKException("OCR处理错误", str(e))Style Guide References
Footnotes
-
根据 PEP 8 指南,函数和变量名应使用 snake_case(小写字母和下划线)。 ↩
| self.logger.setLevel(log_level) | ||
| # 只在没有处理器时添加处理器 | ||
| if not self.logger.handlers: | ||
| handler = logging.StreamHandler() | ||
| handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) | ||
| self.logger.addHandler(handler) | ||
| else: | ||
| # 确保现有处理器的格式一致 | ||
| for h in self.logger.handlers: | ||
| h.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) | ||
| self.logger.info("初始化腾讯云OCR客户端,日志级别: %s", logging.getLevelName(log_level)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当前的日志配置逻辑存在一些潜在问题,并且在 GeneralAccurateOCR 类中出现了代码重复。
- 修改现有处理器: 代码中遍历
self.logger.handlers并重置它们的格式化程序 (setFormatter) 的行为可能会产生意料之外的副作用。如果使用此库的应用程序对日志有自定义配置,这种做法会强制覆盖它们,破坏了模块化。库代码应避免修改不属于自己的配置。 - 代码重复: 这段逻辑与新添加的
GeneralAccurateOCR类中的__init__方法几乎完全相同。代码重复会增加未来的维护成本。
建议将此日志设置逻辑提取到一个辅助函数中,并进行修改,使其只在 logger 没有任何处理器时才添加新的处理器,而不是修改现有的。一个更优的实践是,库本身只负责获取 logger 实例并使用它,将所有配置(如设置级别和添加处理器)的责任留给库的使用者(即应用程序)。
self.logger.setLevel(log_level)
# 只在没有处理器时添加处理器,以避免修改已有的处理器
if not self.logger.handlers:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
self.logger.addHandler(handler)
self.logger.info("初始化腾讯云OCR客户端,日志级别: %s", logging.getLevelName(log_level))
PR Code Suggestions ✨Explore these optional code suggestions:
|
|||||||||||||||||
User description
PR Type
Enhancement, Other
Description
Add GeneralAccurateOCR class for precise OCR recognition
Update version from 0.0.4 to 0.0.5
Optimize CI/CD workflows with conditional execution
Fix configuration and deployment settings
Diagram Walkthrough
File Walkthrough
4 files
Update version and export new OCR classAdd new precise OCR recognition classOptimize logging configuration and parametersImport new GeneralAccurateOCR module5 files
Bump version to 0.0.5Update repository URL configurationAdd conditional execution and remove Aliyun uploadAdd conditional execution and fix version extractionAdd conditional execution for all sync jobs1 files
Fix typo in package installation command