提交 a98c002c authored 作者: 贺阳's avatar 贺阳

1、ocr识别之后查找是否还包含文字,包含的话用ai处理,再检查,还有的话返回错误提示,不显示同步按钮,没有的话出现同步按钮

上级 264bf0fd
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import base64 import base64
import requests import requests
from dashscope import MultiModalConversation from dashscope import ImageSynthesis
import dashscope import dashscope
import logging import logging
from http import HTTPStatus
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
...@@ -12,11 +13,13 @@ dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1' ...@@ -12,11 +13,13 @@ dashscope.base_http_api_url = 'https://dashscope.aliyuncs.com/api/v1'
class AIImageEditService: class AIImageEditService:
"""AI图片编辑服务 - 使用阿里云百炼的qwen-image-edit模型""" """AI图片编辑服务 - 使用阿里云百炼的wan2.5-i2i-preview模型"""
def __init__(self, api_key='sk-e41914f0d9c94035a5ae1322e9a61fb1'): def __init__(self, api_key='sk-e41914f0d9c94035a5ae1322e9a61fb1'):
self.api_key = api_key self.api_key = api_key
self.model = "qwen-image-edit" self.model = "wan2.5-i2i-preview"
# 设置DashScope的API密钥
dashscope.api_key = self.api_key
def edit_image_remove_text(self, image_base64, text_to_remove="AGN UCLINK LOGISITICS LTD"): def edit_image_remove_text(self, image_base64, text_to_remove="AGN UCLINK LOGISITICS LTD"):
""" """
...@@ -25,40 +28,60 @@ class AIImageEditService: ...@@ -25,40 +28,60 @@ class AIImageEditService:
:param text_to_remove: 要移除的文字 :param text_to_remove: 要移除的文字
:return: 处理后的图片base64编码,失败返回None :return: 处理后的图片base64编码,失败返回None
""" """
import time
start_time = time.time()
_logger.info(f"开始AI图片编辑,目标文字: {text_to_remove}")
try: try:
# 构建消息 # 根据要移除的文字构建不同的提示词,强调保持清晰度
messages = [ if "AGN" in text_to_remove and "UCLINK" in text_to_remove:
{ prompt = "将图片中的AGN和UCLINK LOGISITICS LTD这一段文字完全抹去,保持背景完全一致,保持图片清晰度和分辨率,不要模糊"
"role": "user", elif "AGN" in text_to_remove:
"content": [ prompt = "将图片中的AGN这一段文字完全抹去,保持背景完全一致,保持图片清晰度和分辨率,不要模糊"
{"image": f"data:image/png;base64,{image_base64}"}, elif "UCLINK" in text_to_remove:
{"text": f"将图片中的{text_to_remove}这一段文字抹去,保持背景完全一致"} prompt = "将图片中的UCLINK LOGISITICS LTD这一段文字完全抹去,保持背景完全一致,保持图片清晰度和分辨率,不要模糊"
] else:
} prompt = f"将图片中的{text_to_remove}这一段文字完全抹去,保持背景完全一致,保持图片清晰度和分辨率,不要模糊"
]
# 调用AI模型 # 调用ImageSynthesis模型,使用更高质量的设置
response = MultiModalConversation.call( api_start_time = time.time()
api_key=self.api_key, rsp = ImageSynthesis.call(
model=self.model, model=self.model,
messages=messages, prompt=prompt,
stream=False, images=[f"data:image/png;base64,{image_base64}"],
negative_prompt="模糊、不清晰、低质量、文字、文本、字母、数字、噪点、失真",
n=1,
watermark=False, watermark=False,
negative_prompt=" " seed=12345,
# 添加质量相关参数
size="1024*1024", # 使用较高分辨率
quality="hd" # 使用高清质量
) )
api_end_time = time.time()
api_processing_time = api_end_time - api_start_time
_logger.info(f"AI API调用耗时: {api_processing_time:.2f}秒")
if response.status_code == 200: if rsp.status_code == HTTPStatus.OK:
# 获取处理后图片的URL # 获取处理后图片的URL
image_url = response.output.choices[0].message.content[0]['image'] image_url = rsp.output.results[0].url
_logger.info(f"AI图片编辑成功,图片URL: {image_url}") _logger.info(f"AI图片编辑成功,图片URL: {image_url}")
# 下载图片并转换为base64 # 下载图片并转换为base64
download_start_time = time.time()
edited_image_base64 = self.download_and_convert_to_base64(image_url) edited_image_base64 = self.download_and_convert_to_base64(image_url)
download_end_time = time.time()
download_time = download_end_time - download_start_time
_logger.info(f"图片下载耗时: {download_time:.2f}秒")
total_time = time.time() - start_time
_logger.info(f"AI图片编辑总耗时: {total_time:.2f}秒")
return edited_image_base64 return edited_image_base64
else: else:
_logger.error(f"AI图片编辑失败,HTTP返回码:{response.status_code}") _logger.error(f"AI图片编辑失败,HTTP返回码:{rsp.status_code}")
_logger.error(f"错误码:{response.code}") _logger.error(f"错误码:{rsp.code}")
_logger.error(f"错误信息:{response.message}") _logger.error(f"错误信息:{rsp.message}")
return None return None
except Exception as e: except Exception as e:
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<odoo> <odoo>
<data> <data>
<!-- Batch Get POD Info Wizard Form View 批量获取POD信息向导表单视图 --> <!-- Batch Get POD Info Wizard Form View 批量获取POD信息向导表单视图 -->
<record id="view_batch_get_pod_info_wizard_form" model="ir.ui.view"> <record id="view_batch_get_pod_info_wizard_form" model="ir.ui.view">
<field name="name">batch.get.pod.info.wizard.form</field> <field name="name">batch.get.pod.info.wizard.form</field>
...@@ -11,35 +11,54 @@ ...@@ -11,35 +11,54 @@
<sheet> <sheet>
<!-- <group> --> <!-- <group> -->
<group> <group>
<field name="sync_last_mile_pod" widget="boolean_toggle" attrs="{'invisible': [('pdf_file', '=', False)]}"/> <field name="remove_specified_text" readonly="1" widget="boolean_toggle"
attrs="{'invisible': [('pdf_file', '!=', False)]}"/>
</group> </group>
<group> <group>
<field name="remove_specified_text" widget="boolean_toggle" attrs="{'invisible': [('pdf_file', '!=', False)]}"/> <field name="sync_last_mile_pod" widget="boolean_toggle"
attrs="{'invisible': ['|',('pdf_file', '=', False),('show_error_message', '!=', False)]}"/>
</group> </group>
<group> <group>
<field name="sync_match_node" widget="boolean_toggle" attrs="{'invisible': [('pdf_file', '=', False)]}"/> <field name="sync_match_node" widget="boolean_toggle"
attrs="{'invisible': ['|',('pdf_file', '=', False),('show_error_message', '!=', False)]}"/>
</group> </group>
<!-- </group> --> <!-- </group> -->
<div class="alert alert-info" role="alert"> <div class="alert alert-info" role="alert">
<strong>Description:</strong> <!-- 说明: --> <strong>Description:</strong> <!-- 说明: -->
<ul> <ul>
<li><strong>Sync Last Mile POD:</strong> Synchronize POD (Proof of Delivery) attachment information with TK system, including big package quantities and container numbers</li> <!-- 同步尾程POD:向TK同步尾程交接POD(待大包数量和箱号)的附件信息 --> <li>
<li><strong>Remove Specified Text:</strong> Remove specified text (AGN, UCLINK LOGISITICS LTD) from PDF files</li> <!-- 涂抹指定文字:对PDF文件中的指定文字进行涂抹处理 --> <strong>Sync Last Mile POD:</strong>
<li><strong>Sync Push Match Node:</strong> Synchronize and push matched node information based on POD file, extract time from red boxes as node operation time</li> <!-- 同步推送匹配节点:根据POD文件获取对应的清关节点,提取红色框时间作为节点操作时间 --> Synchronize POD (Proof of Delivery) attachment information with TK system, including
big package quantities and container numbers
</li> <!-- 同步尾程POD:向TK同步尾程交接POD(待大包数量和箱号)的附件信息 -->
<li>
<strong>Remove Specified Text:</strong>
Remove specified text (AGN, UCLINK LOGISITICS LTD) from PDF files
</li> <!-- 涂抹指定文字:对PDF文件中的指定文字进行涂抹处理 -->
<li>
<strong>Sync Push Match Node:</strong>
Synchronize and push matched node information based on POD file, extract time from
red boxes as node operation time
</li> <!-- 同步推送匹配节点:根据POD文件获取对应的清关节点,提取红色框时间作为节点操作时间 -->
</ul> </ul>
</div> </div>
<div class="alert alert-danger" role="alert" attrs="{'invisible': [('show_error_message', '=', False)]}"> <div class="alert alert-danger" role="alert"
attrs="{'invisible': [('show_error_message', '=', False)]}">
<field name="show_error_message"/> <field name="show_error_message"/>
</div> </div>
<div> <div>
<field name="pdf_file" filename="pdf_filename" widget="pdf_viewer" readonly="1" attrs="{'invisible': [('pdf_file', '=', False)]}"/> <field name="pdf_file" filename="pdf_filename" widget="pdf_viewer" readonly="1"
attrs="{'invisible': [('pdf_file', '=', False)]}"/>
</div> </div>
<footer> <footer>
<!-- 预览按钮:处理PDF并显示合并后的文件 --> <!-- 预览按钮:处理PDF并显示合并后的文件 -->
<button string="Preview" type="object" name="action_preview" class="btn-primary" attrs="{'invisible': [('pdf_file', '!=', False)]}"/> <button string="Preview" type="object" name="action_preview" class="btn-primary"
attrs="{'invisible': [('pdf_file', '!=', False)]}"/>
<!-- 确认按钮:使用已处理的文件数据进行回写和同步 --> <!-- 确认按钮:使用已处理的文件数据进行回写和同步 -->
<button string="Confirm" type="object" name="confirm" class="btn-primary" attrs="{'invisible': [('pdf_file', '=', False)]}"/> <button string="Confirm" type="object" name="confirm" class="btn-primary"
attrs="{'invisible': [('pdf_file', '=', False)]}"/>
<button string="Close" special="cancel"/> <button string="Close" special="cancel"/>
</footer> </footer>
</sheet> </sheet>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论