解码base64图片后ocr图片成功

This commit is contained in:
haotian 2025-08-19 10:08:15 +08:00
parent 5269053e1a
commit ec049c3a17
4 changed files with 111 additions and 1 deletions

View File

@ -4,14 +4,20 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import get_db
from app.services.imageServices import ImageService
from app.schemas.image import ImageBase
from app.schemas.ocr import OCRRequest, OCRResponse
from app.util.responseHttp import ResponseUtil
from app.util.baiduOCR import BaiduOCR
import base64
import io
from PIL import Image
import os
import uuid
# from app.crud.event import event
# from app.schemas.event import EventList, EventDetail, EventUpdate, EventQuery, TestEvent
baiduOCR = BaiduOCR()
router = APIRouter()
router = APIRouter(prefix="/api/v1", tags=["ocr"])
@router.get("/hello")
async def get_hello():
@ -45,3 +51,41 @@ async def test_ocr(
# print(result)
return ResponseUtil.success(msg="success", data=result)
# return ResponseUtil.success(msg="success")
@router.post("/ocr_from_base64")
async def ocr_from_base64(request: OCRRequest):
"""
从base64图片数据进行OCR识别
"""
try:
# 移除base64数据的前缀如果有
image_base64 = request.image_base64
if image_base64.startswith('data:image'):
# 格式如: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...
image_base64 = image_base64.split(',')[1]
# 解码base64数据
image_data = base64.b64decode(image_base64)
# 将字节数据转换为PIL Image对象
image = Image.open(io.BytesIO(image_data))
# 创建临时文件路径
temp_dir = "./tmp/ocr_images"
os.makedirs(temp_dir, exist_ok=True)
temp_filename = f"{uuid.uuid4()}.{request.image_type or 'jpg'}"
temp_path = os.path.join(temp_dir, temp_filename)
# 保存图片到临时文件
image.save(temp_path)
# 使用PaddleOCR进行识别
result = baiduOCR.ocr(temp_path)
# 删除临时文件
os.remove(temp_path)
return ResponseUtil.success(msg="OCR识别成功", data=result[0])
except Exception as e:
return ResponseUtil.error(msg=f"OCR识别失败: {str(e)}", data=None)

View File

@ -0,0 +1,11 @@
from pydantic import BaseModel
from typing import Optional
class OCRRequest(BaseModel):
image_base64: str
image_type: Optional[str] = "jpg" # 图片类型,如 jpg, png 等
class OCRResponse(BaseModel):
success: bool
message: str
data: Optional[list] = None

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,54 @@
import base64
import requests
import json
def image_to_base64(image_path):
"""
将图片文件转换为base64编码
"""
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return encoded_string
def test_ocr_api(image_path, api_url="http://10.0.0.202:12342/api/v1/ocr_from_base64"):
"""
测试OCR API接口
"""
# 将图片转换为base64
image_base64 = image_to_base64(image_path)
with open("base64.txt", "w") as f:
f.write(repr(image_base64))
# 准备请求数据
payload = {
"image_base64": image_base64,
"image_type": "jpg" # 根据实际图片类型修改
}
# 发送POST请求
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
if response.status_code == 200:
result = response.json()
print("OCR识别结果:")
print(json.dumps(result, ensure_ascii=False, indent=2))
return result
else:
print(f"请求失败,状态码: {response.status_code}")
print(response.text)
return None
except Exception as e:
print(f"请求异常: {str(e)}")
return None
if __name__ == "__main__":
# 测试图片路径,请根据实际情况修改
test_image_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/data_image/001读表图片/2c7cc83019e7388a7041101da92c9829_frame_000000.jpg"
# 调用测试函数
test_ocr_api(test_image_path)