图片识别

This commit is contained in:
李鹏宇
2026-06-12 15:03:27 +08:00
parent bfdab920e1
commit 271bf70ca5
4 changed files with 13 additions and 15 deletions
+1 -1
View File
@@ -15,7 +15,7 @@
'author': 'pengyuthon',
'version': '17.0.2.2.0',
'license': 'LGPL-3',
'depends': ['base', 'web', 'hr', 'sh_backmate_theme_adv_enterprise'],
'depends': ['base', 'web', 'hr', 'documents', 'sh_backmate_theme_adv_enterprise'],
'data': [
'security/ai_security.xml',
'security/ir.model.access.csv',
+6 -8
View File
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
import json
import base64
from odoo import http
from odoo.http import request
@@ -208,13 +207,12 @@ class AiChatController(http.Controller):
if conv.exists():
provider = conv.provider_id
# 创建 ir.attachment
attachment = request.env['ir.attachment'].sudo().create({
# 创建 documents.document(raw 字段接受原始二进制,内部通过 attachment_id 存储)
document = request.env['documents.document'].sudo().create({
'name': filename,
'datas': base64.b64encode(raw_bytes).decode('utf-8'),
'mimetype': mimetype,
'res_model': 'ai.message',
'res_id': 0,
'raw': raw_bytes,
'folder_id': False,
})
# OCR 提取文字
@@ -226,14 +224,14 @@ class AiChatController(http.Controller):
result = ocr.smart_recognize(raw_bytes, mimetype)
if result.get('success'):
ocr_text = result.get('text') or ''
attachment.sudo().write({'ai_ocr_content': ocr_text})
document.sudo().write({'ai_ocr_content': ocr_text})
else:
# OCR 失败不阻塞上传,前端仍可发送(只是没有文字提取)
pass
return request.make_json_response({
'success': True,
'attachment_id': attachment.id,
'attachment_id': document.id,
'filename': filename,
'mimetype': mimetype,
'ocr_text': ocr_text,
+3 -3
View File
@@ -769,7 +769,7 @@ class AiConversation(models.Model):
ocr_result = ''
msg_attachment_ids = []
if attachment_ids:
attachments = self.env['ir.attachment'].sudo().browse(attachment_ids).exists()
attachments = self.env['documents.document'].sudo().browse(attachment_ids).exists()
if attachments:
msg_attachment_ids = [(4, a.id) for a in attachments]
ocr_parts = []
@@ -965,8 +965,8 @@ class AiMessage(models.Model):
], string='角色', required=True)
content = fields.Text(string='内容', required=True)
attachment_ids = fields.Many2many(
'ir.attachment', 'ai_message_attachment_rel',
'message_id', 'attachment_id', string='附件'
'documents.document', 'ai_message_doc_attachment_rel',
'message_id', 'document_id', string='附件'
)
ocr_content = fields.Text(
string='OCR 识别结果',
+3 -3
View File
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
"""扩展 ir.attachment,增加 AI OCR 内容字段"""
"""扩展 documents.document,增加 AI OCR 内容字段"""
from odoo import models, fields
class IrAttachment(models.Model):
_inherit = 'ir.attachment'
class DocumentsDocument(models.Model):
_inherit = 'documents.document'
ai_ocr_content = fields.Text(
string='AI OCR 识别内容',