dlxj commited on
Commit ·
1ac68eb
1
Parent(s): d53f288
- ocr/find_paragraph.py +0 -56
- ocr/getdata.py +1 -1
- ocr/kandianguji_ocr.py +0 -98
- ocr/ppcor_aliocr_convert.py +0 -501
- ocr/qwen_ocr.py +0 -43
ocr/find_paragraph.py
DELETED
|
@@ -1,56 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Find paragraphs in loader response
|
| 3 |
-
"""
|
| 4 |
-
import time
|
| 5 |
-
from selenium import webdriver
|
| 6 |
-
from selenium.webdriver.common.by import By
|
| 7 |
-
from selenium.webdriver.support import expected_conditions as EC
|
| 8 |
-
from selenium.webdriver.support.ui import WebDriverWait
|
| 9 |
-
import json
|
| 10 |
-
|
| 11 |
-
options = webdriver.ChromeOptions()
|
| 12 |
-
options.add_argument("--headless=new")
|
| 13 |
-
options.add_argument("--no-sandbox")
|
| 14 |
-
options.add_argument("--disable-dev-shm-usage")
|
| 15 |
-
options.add_argument("--disable-blink-features=AutomationControlled")
|
| 16 |
-
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
|
| 17 |
-
options.add_experimental_option("excludeSwitches", ["enable-automation"])
|
| 18 |
-
options.add_experimental_option("useAutomationExtension", False)
|
| 19 |
-
options.set_capability("goog:loggingPrefs", {"performance": "ALL"})
|
| 20 |
-
driver = webdriver.Chrome(options=options)
|
| 21 |
-
driver.set_window_size(1400, 900)
|
| 22 |
-
driver.execute_cdp_cmd("Network.enable", {})
|
| 23 |
-
|
| 24 |
-
driver.get("https://www.shidianguji.com/zh/book/SWX0005")
|
| 25 |
-
time.sleep(5)
|
| 26 |
-
wait = WebDriverWait(driver, 10)
|
| 27 |
-
try:
|
| 28 |
-
els = driver.find_elements(By.XPATH, "//*[contains(text(), '第二回')]")
|
| 29 |
-
if els:
|
| 30 |
-
print("Clicking 第二回")
|
| 31 |
-
driver.execute_script("arguments[0].click();", els[0])
|
| 32 |
-
except Exception as e:
|
| 33 |
-
pass
|
| 34 |
-
|
| 35 |
-
time.sleep(5)
|
| 36 |
-
logs = driver.get_log("performance")
|
| 37 |
-
for entry in logs:
|
| 38 |
-
msg = json.loads(entry["message"])["message"]
|
| 39 |
-
method = msg.get("method")
|
| 40 |
-
if method == "Network.responseReceived":
|
| 41 |
-
req_id = msg.get("params", {}).get("requestId")
|
| 42 |
-
url = msg.get("params", {}).get("response", {}).get("url", "")
|
| 43 |
-
if "__loader=__session" in url:
|
| 44 |
-
try:
|
| 45 |
-
body = driver.execute_cdp_cmd("Network.getResponseBody", {"requestId": req_id})
|
| 46 |
-
data = json.loads(body.get("body", ""))
|
| 47 |
-
print("Loader Response JSON keys:", list(data.keys()))
|
| 48 |
-
if "paragraphList" in data:
|
| 49 |
-
print("Found paragraphList in Loader!")
|
| 50 |
-
print("Type:", type(data["paragraphList"]))
|
| 51 |
-
if isinstance(data["paragraphList"], list) and len(data["paragraphList"]) > 0:
|
| 52 |
-
print("First item keys:", list(data["paragraphList"][0].keys()))
|
| 53 |
-
print("First item content snippet:", str(data["paragraphList"][0])[:150])
|
| 54 |
-
except:
|
| 55 |
-
pass
|
| 56 |
-
driver.quit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ocr/getdata.py
CHANGED
|
@@ -376,7 +376,7 @@ def _enter_image_mode(driver: webdriver.Chrome) -> None:
|
|
| 376 |
def main() -> int:
|
| 377 |
ap = argparse.ArgumentParser()
|
| 378 |
ap.add_argument("--book-id", default="TPM0001")
|
| 379 |
-
ap.add_argument("--chapter", default="新刻金瓶梅詞話卷之一_第
|
| 380 |
ap.add_argument("--out", default="out")
|
| 381 |
ap.add_argument("--headless", action="store_true")
|
| 382 |
ap.add_argument("--timeout", type=int, default=20000)
|
|
|
|
| 376 |
def main() -> int:
|
| 377 |
ap = argparse.ArgumentParser()
|
| 378 |
ap.add_argument("--book-id", default="TPM0001")
|
| 379 |
+
ap.add_argument("--chapter", default="新刻金瓶梅詞話卷之一_第三回")
|
| 380 |
ap.add_argument("--out", default="out")
|
| 381 |
ap.add_argument("--headless", action="store_true")
|
| 382 |
ap.add_argument("--timeout", type=int, default=20000)
|
ocr/kandianguji_ocr.py
DELETED
|
@@ -1,98 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import base64
|
| 3 |
-
import json
|
| 4 |
-
import requests
|
| 5 |
-
|
| 6 |
-
API_URL = "https://ocr.kandianguji.com/ocr_api"
|
| 7 |
-
TOKEN = "8be3d75a-8a66-4b5d-9047-d4895e4c8000"
|
| 8 |
-
EMAIL = "13788325535"
|
| 9 |
-
|
| 10 |
-
def get_kandianguji_ocr_result(image_path: str, **kwargs):
|
| 11 |
-
"""
|
| 12 |
-
调用看典古籍OCR API识别图像。
|
| 13 |
-
|
| 14 |
-
参数:
|
| 15 |
-
image_path (str): 需要识别的图像文件路径。
|
| 16 |
-
**kwargs: 其他可选API参数 (如 version='v2', det_layout=True 等)。
|
| 17 |
-
详细参数请参考 readme.txt 文档。
|
| 18 |
-
|
| 19 |
-
返回:
|
| 20 |
-
dict: API响应的JSON数据。
|
| 21 |
-
"""
|
| 22 |
-
if not os.path.exists(image_path):
|
| 23 |
-
raise FileNotFoundError(f"指定的图像文件不存在: {image_path}")
|
| 24 |
-
|
| 25 |
-
# 1. 将图像转换为base64编码
|
| 26 |
-
with open(image_path, "rb") as img_file:
|
| 27 |
-
image_base64 = base64.b64encode(img_file.read()).decode('utf-8')
|
| 28 |
-
|
| 29 |
-
# 2. 构建基础请求数据
|
| 30 |
-
payload = {
|
| 31 |
-
"token": TOKEN,
|
| 32 |
-
"email": EMAIL,
|
| 33 |
-
"image": image_base64
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
# 3. 合并可选参数
|
| 37 |
-
# 根据 readme.txt 提取的默认可选参数
|
| 38 |
-
default_options = {
|
| 39 |
-
"char_ocr": True,
|
| 40 |
-
"det_mode": "sp",
|
| 41 |
-
"det_layout": True,
|
| 42 |
-
"image_size": 0,
|
| 43 |
-
"return_position": True,
|
| 44 |
-
"return_choices": True,
|
| 45 |
-
"version": "v2",
|
| 46 |
-
"only_plain_text": False,
|
| 47 |
-
"return_layout": True,
|
| 48 |
-
"auto_insert_space": False,
|
| 49 |
-
"hp_line_words_angel": "left2right",
|
| 50 |
-
"sp_line_words_angel": "top2bottom"
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
# 更新默认参数(如果 kwargs 中提供了新的值)
|
| 54 |
-
for key, value in default_options.items():
|
| 55 |
-
payload[key] = kwargs.get(key, value)
|
| 56 |
-
|
| 57 |
-
# 4. 发送POST请求
|
| 58 |
-
headers = {
|
| 59 |
-
"Content-Type": "application/json"
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
try:
|
| 63 |
-
response = requests.post(API_URL, json=payload, headers=headers)
|
| 64 |
-
# 检查响应状态码
|
| 65 |
-
response.raise_for_status()
|
| 66 |
-
return response.json()
|
| 67 |
-
except requests.exceptions.RequestException as e:
|
| 68 |
-
print(f"API请求失败: {e}")
|
| 69 |
-
# 如果有响应体,打印出来以便调试
|
| 70 |
-
if 'response' in locals() and response is not None:
|
| 71 |
-
print(f"响应内容: {response.text}")
|
| 72 |
-
return None
|
| 73 |
-
|
| 74 |
-
if __name__ == "__main__":
|
| 75 |
-
# 测试用例 (请替换为真实的古籍图片路径)
|
| 76 |
-
sample_image = "0375.jpg"
|
| 77 |
-
|
| 78 |
-
print("看典古籍OCR API 接口测试")
|
| 79 |
-
print("-" * 30)
|
| 80 |
-
|
| 81 |
-
if os.path.exists(sample_image):
|
| 82 |
-
print(f"正在识别图片: {sample_image}")
|
| 83 |
-
# 调用示例,可按需传入 v2 版本的特有参数
|
| 84 |
-
result = get_kandianguji_ocr_result(
|
| 85 |
-
sample_image,
|
| 86 |
-
version="v2",
|
| 87 |
-
det_layout=True
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
if result:
|
| 91 |
-
print("\n识别结果:")
|
| 92 |
-
s = json.dumps(result, indent=2, ensure_ascii=False)
|
| 93 |
-
with open("out.json", 'w', encoding='utf-8') as f:
|
| 94 |
-
f.write(s)
|
| 95 |
-
print(s)
|
| 96 |
-
else:
|
| 97 |
-
print(f"提示: 请在当前目录下放置一张名为 '{sample_image}' 的图片用于测试。")
|
| 98 |
-
print("或者修改 sample_image 变量的值指向有效的图片路径。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ocr/ppcor_aliocr_convert.py
DELETED
|
@@ -1,501 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
# pip install numpy==1.23.5 opencv-python==4.10.0.84 -i https://mirrors.aliyun.com/pypi/simple/
|
| 3 |
-
|
| 4 |
-
PPOCRLabel --lang ch # 启动标注工具
|
| 5 |
-
|
| 6 |
-
cp autodl-tmp/train_data.zip . && \
|
| 7 |
-
unzip train_data.zip -d PaddleOCR
|
| 8 |
-
|
| 9 |
-
# https://github.com/PaddlePaddle/PaddleOCR/blob/static/doc/doc_ch/FAQ.md
|
| 10 |
-
|
| 11 |
-
# 7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on data.7z data/
|
| 12 |
-
|
| 13 |
-
新建文件夹 train_data, 要标注的图片全部放在里面
|
| 14 |
-
|
| 15 |
-
新建 train_data/Label.txt 内容如下
|
| 16 |
-
行中用 \t 分隔 , points 的标记顺序是 左上 右上 右下 左下
|
| 17 |
-
train_data/0093.bmp [{"transcription":"参考答案及解析","points":[[525,179],[1295,167],[1295,268],[521,292]],"difficult":false}]
|
| 18 |
-
train_data/0094.bmp [{"transcription":"其他内容","points":[[525,179],[1295,167],[1295,268],[521,292]],"difficult":false}]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
给 PaddleOCR 用,前面是坐标和图片都变换;这里图像不变,坐标不变
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
将阿里OCR 的识别结果(图片和标注)转换成 icdar2015 格式 (注意:它的文本是含 utf8 bom 的)
|
| 25 |
-
|
| 26 |
-
给 mmocr 训练用。格式是 icdar2015 的格式,文件夹的组织方式是按照 mmocr 的要求创建的
|
| 27 |
-
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"""
|
| 32 |
-
|
| 33 |
-
! unzip ./GD500.zip -d DB/datasets
|
| 34 |
-
|
| 35 |
-
icdar2015 文本检测数据集
|
| 36 |
-
标注格式: x1,y1,x2,y2,x3,y3,x4,y4,text
|
| 37 |
-
|
| 38 |
-
其中, x1,y1为左上角坐标,x2,y2为右上角坐标,x3,y3为右下角坐标,x4,y4为左下角坐标。
|
| 39 |
-
|
| 40 |
-
# 表示text难以辨认。
|
| 41 |
-
"""
|
| 42 |
-
|
| 43 |
-
import random
|
| 44 |
-
from pathlib import Path
|
| 45 |
-
import os
|
| 46 |
-
import shutil
|
| 47 |
-
import glob
|
| 48 |
-
import base64
|
| 49 |
-
from importlib.resources import path
|
| 50 |
-
import math
|
| 51 |
-
import numpy as np
|
| 52 |
-
import cv2
|
| 53 |
-
import json
|
| 54 |
-
import decimal
|
| 55 |
-
import datetime
|
| 56 |
-
from pickletools import uint8
|
| 57 |
-
class DecimalEncoder(json.JSONEncoder):
|
| 58 |
-
def default(self, o):
|
| 59 |
-
if isinstance(o, decimal.Decimal):
|
| 60 |
-
return float(o)
|
| 61 |
-
elif isinstance(o, datetime.datetime):
|
| 62 |
-
return str(o)
|
| 63 |
-
super(DecimalEncoder, self).default(o)
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
def save_json(filename, dics):
|
| 67 |
-
with open(filename, 'w', encoding='utf-8') as fp:
|
| 68 |
-
json.dump(dics, fp, indent=4, cls=DecimalEncoder, ensure_ascii=False)
|
| 69 |
-
fp.close()
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
def load_json(filename):
|
| 73 |
-
with open(filename, encoding='utf-8') as fp:
|
| 74 |
-
js = json.load(fp)
|
| 75 |
-
fp.close()
|
| 76 |
-
return js
|
| 77 |
-
|
| 78 |
-
# convert string to json
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
def parse(s):
|
| 82 |
-
return json.loads(s, strict=False)
|
| 83 |
-
|
| 84 |
-
# convert dict to string
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def string(d):
|
| 88 |
-
return json.dumps(d, cls=DecimalEncoder, ensure_ascii=False)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
def transform(points, M):
|
| 92 |
-
# points 算出四个点变换后移动到哪里了
|
| 93 |
-
# points = np.array([[word_x, word_y], # 左上
|
| 94 |
-
# [word_x + word_width, word_y], # 右上
|
| 95 |
-
# [word_x + word_width, word_y + word_height], # 右下
|
| 96 |
-
# [word_x, word_y + word_height], # 左下
|
| 97 |
-
# ])
|
| 98 |
-
# add ones
|
| 99 |
-
ones = np.ones(shape=(len(points), 1))
|
| 100 |
-
|
| 101 |
-
points_ones = np.hstack([points, ones])
|
| 102 |
-
|
| 103 |
-
# transform points
|
| 104 |
-
transformed_points = M.dot(points_ones.T).T
|
| 105 |
-
|
| 106 |
-
transformed_points_int = np.round(
|
| 107 |
-
transformed_points, decimals=0).astype(np.int32) # 批量四舍五入
|
| 108 |
-
|
| 109 |
-
return transformed_points_int
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
def cutPoly(img, pts):
|
| 113 |
-
# img = cv2.imdecode(np.fromfile('./t.png', dtype=np.uint8), -1)
|
| 114 |
-
# pts = np.array([[10,150],[150,100],[300,150],[350,100],[310,20],[35,10]])
|
| 115 |
-
|
| 116 |
-
# (1) Crop the bounding rect
|
| 117 |
-
rect = cv2.boundingRect(pts)
|
| 118 |
-
x, y, w, h = rect
|
| 119 |
-
croped = img[y:y+h, x:x+w].copy()
|
| 120 |
-
|
| 121 |
-
# (2) make mask
|
| 122 |
-
pts = pts - pts.min(axis=0)
|
| 123 |
-
|
| 124 |
-
mask = np.zeros(croped.shape[:2], np.uint8)
|
| 125 |
-
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
|
| 126 |
-
|
| 127 |
-
# (3) do bit-op
|
| 128 |
-
dst = cv2.bitwise_and(croped, croped, mask=mask)
|
| 129 |
-
|
| 130 |
-
# (4) add the white background
|
| 131 |
-
bg = np.ones_like(croped, np.uint8)*255
|
| 132 |
-
cv2.bitwise_not(bg, bg, mask=mask)
|
| 133 |
-
dst2 = bg + dst
|
| 134 |
-
|
| 135 |
-
# cv2.imwrite("croped.png", croped)
|
| 136 |
-
# cv2.imwrite("mask.png", mask)
|
| 137 |
-
# cv2.imwrite("dst.png", dst)
|
| 138 |
-
# cv2.imwrite("dst2.png", dst2)
|
| 139 |
-
|
| 140 |
-
return dst2
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
def md5(fname):
|
| 144 |
-
import hashlib
|
| 145 |
-
hash_md5 = hashlib.md5()
|
| 146 |
-
with open(fname, "rb") as f:
|
| 147 |
-
for chunk in iter(lambda: f.read(4096), b""):
|
| 148 |
-
hash_md5.update(chunk)
|
| 149 |
-
return hash_md5.hexdigest()
|
| 150 |
-
|
| 151 |
-
def get_all_md5():
|
| 152 |
-
m5s = []
|
| 153 |
-
import glob
|
| 154 |
-
jpgs = glob.glob('./bookimage/**/*.jpg', recursive=True)
|
| 155 |
-
for jpg in jpgs:
|
| 156 |
-
m5 = md5(jpg)
|
| 157 |
-
m5s.append( m5 )
|
| 158 |
-
return m5s
|
| 159 |
-
|
| 160 |
-
def get_json_paths():
|
| 161 |
-
json_paths = []
|
| 162 |
-
m5s = get_all_md5()
|
| 163 |
-
for m5 in m5s:
|
| 164 |
-
j_pth = f"/yingedu/project/ocr_server_test/data/json/{m5.lower()}.json"
|
| 165 |
-
if os.path.exists(j_pth):
|
| 166 |
-
json_paths.append(j_pth)
|
| 167 |
-
return json_paths
|
| 168 |
-
|
| 169 |
-
def do_convert_aliocr():
|
| 170 |
-
|
| 171 |
-
# 24HLZYZG64/0001.jpg c04d111ef69b9892d39f9430b6906047 md5是这个
|
| 172 |
-
# ls /yingedu/project/ocr_server_test/data/json/0bf0383ece9a533683e615bf57525812.json aliocr 原始识别结果
|
| 173 |
-
# ls /yingedu/project/ocr_server_test/data/img/0bf0383ece9a533683e615bf57525812.txt aliocr 原始识别图像(二值化降燥后压缩成2M)
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
root = 'train_data'
|
| 178 |
-
tmp = 'tmp'
|
| 179 |
-
label_path = os.path.join(root, 'Label.txt')
|
| 180 |
-
|
| 181 |
-
key_path = os.path.join(root, 'keys.txt')
|
| 182 |
-
|
| 183 |
-
fileState_path = os.path.join(root, 'fileState.txt')
|
| 184 |
-
|
| 185 |
-
if os.path.exists(root):
|
| 186 |
-
shutil.rmtree(root)
|
| 187 |
-
# os.rmdir(root)
|
| 188 |
-
if os.path.exists(tmp):
|
| 189 |
-
shutil.rmtree(tmp)
|
| 190 |
-
|
| 191 |
-
if not os.path.exists(root):
|
| 192 |
-
os.makedirs(root)
|
| 193 |
-
|
| 194 |
-
if not os.path.exists(tmp):
|
| 195 |
-
os.makedirs(tmp)
|
| 196 |
-
|
| 197 |
-
label = ''
|
| 198 |
-
keys = ''
|
| 199 |
-
states = ''
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
dic_words = {} # 所有词
|
| 203 |
-
|
| 204 |
-
# 开始转换
|
| 205 |
-
|
| 206 |
-
# https://help.aliyun.com/document_detail/294540.html 阿里云ocr结果字段定义
|
| 207 |
-
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
|
| 208 |
-
|
| 209 |
-
dir_json = './测试图片/json' # '/yingedu/project/ocr_server_test/data/json' # './data/json'
|
| 210 |
-
dir_img = './测试图片/img' # '/yingedu/project/ocr_server_test/data/img' # './data/img'
|
| 211 |
-
|
| 212 |
-
g_count = 1
|
| 213 |
-
g_count2 = 1
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
json_paths = glob.glob('{}/*.json'.format(dir_json), recursive=False)
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
#json_paths = get_json_paths()
|
| 220 |
-
|
| 221 |
-
for json_path in json_paths:
|
| 222 |
-
|
| 223 |
-
arr = []
|
| 224 |
-
|
| 225 |
-
base = Path(json_path).stem
|
| 226 |
-
|
| 227 |
-
# if base == '0bf0383ece9a533683e615bf57525812':
|
| 228 |
-
# continue
|
| 229 |
-
|
| 230 |
-
img_path = os.path.join(dir_img, '{}.txt'.format(base))
|
| 231 |
-
|
| 232 |
-
if not os.path.exists(img_path): # 没有相应的图片,可能被删除了
|
| 233 |
-
print(f'Warnnig: no image {img_path}')
|
| 234 |
-
continue
|
| 235 |
-
|
| 236 |
-
jsn = load_json(json_path)
|
| 237 |
-
|
| 238 |
-
if not ('prism_wordsInfo' in jsn):
|
| 239 |
-
print(f'Warning: no charater in {img_path}')
|
| 240 |
-
continue
|
| 241 |
-
|
| 242 |
-
with open(img_path, "r", encoding="utf-8") as fp:
|
| 243 |
-
imgdata = fp.read()
|
| 244 |
-
imgdata = base64.b64decode(imgdata)
|
| 245 |
-
imgdata = np.frombuffer(imgdata, np.uint8)
|
| 246 |
-
img = cv2.imdecode(imgdata, cv2.IMREAD_UNCHANGED)
|
| 247 |
-
|
| 248 |
-
# cv2.imshow('img', img)
|
| 249 |
-
# cv2.waitKey(0)
|
| 250 |
-
|
| 251 |
-
if len(img.shape) != 3: # 转彩图
|
| 252 |
-
img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 253 |
-
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # DBNet 原版只能处理彩图,这里转一下
|
| 254 |
-
|
| 255 |
-
else:
|
| 256 |
-
img_color = img.copy()
|
| 257 |
-
|
| 258 |
-
img_color_origin = img_color.copy()
|
| 259 |
-
img_color_origin2 = img_color.copy()
|
| 260 |
-
|
| 261 |
-
name = f'{g_count}.jpg'
|
| 262 |
-
dst_img_path = f'{root}/{name}'
|
| 263 |
-
states += f'G:\\train_data\\{name}\t1\n'
|
| 264 |
-
g_count += 1
|
| 265 |
-
|
| 266 |
-
cv2.imwrite(dst_img_path, img)
|
| 267 |
-
|
| 268 |
-
wordsInfo = jsn['prism_wordsInfo']
|
| 269 |
-
for j in range(len(wordsInfo)):
|
| 270 |
-
jo = wordsInfo[j]
|
| 271 |
-
word = jo["word"]
|
| 272 |
-
|
| 273 |
-
for w in list(word):
|
| 274 |
-
if not (w in dic_words):
|
| 275 |
-
dic_words[w] = True
|
| 276 |
-
|
| 277 |
-
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
|
| 278 |
-
angle = jo['angle']
|
| 279 |
-
|
| 280 |
-
img_color = img_color_origin.copy()
|
| 281 |
-
|
| 282 |
-
word_x = jo['x']
|
| 283 |
-
word_y = jo['y']
|
| 284 |
-
word_width = jo['width']
|
| 285 |
-
word_height = jo['height']
|
| 286 |
-
|
| 287 |
-
if abs(angle) == 90 or abs(angle) == 270:
|
| 288 |
-
word_width = jo['height']
|
| 289 |
-
word_height = jo['width']
|
| 290 |
-
elif angle != 0:
|
| 291 |
-
|
| 292 |
-
# 变换前画出绿框,方便追踪点的前后变化
|
| 293 |
-
# img_color = cv2.rectangle(img_color, (word_x, word_y), (
|
| 294 |
-
# word_x + word_width, word_y + word_height), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
|
| 295 |
-
|
| 296 |
-
# cv2.imshow("green", img_color)
|
| 297 |
-
# cv2.waitKey(0)
|
| 298 |
-
|
| 299 |
-
# 变换前的多边形蓝框
|
| 300 |
-
points = np.array([
|
| 301 |
-
[word_x, word_y], # 左上
|
| 302 |
-
[word_x + word_width, word_y], # 右上
|
| 303 |
-
[word_x + word_width, word_y + word_height], # 右下
|
| 304 |
-
[word_x, word_y + word_height], # 左下
|
| 305 |
-
])
|
| 306 |
-
|
| 307 |
-
# # cv2.fillPoly(img_color, pts=[points], color=(255, 0, 0)) # 填充
|
| 308 |
-
# cv2.polylines(img_color, [points], isClosed=True, color=(
|
| 309 |
-
# 255, 0, 0), thickness=1) # 只画线,不填充
|
| 310 |
-
|
| 311 |
-
# cv2.imshow("polys", img_color)
|
| 312 |
-
# cv2.waitKey(0)
|
| 313 |
-
|
| 314 |
-
# 获取图像的维度,并计算中心
|
| 315 |
-
(h, w) = img_color.shape[:2]
|
| 316 |
-
(cX, cY) = (w // 2, h // 2)
|
| 317 |
-
|
| 318 |
-
# - (cX,cY): 旋转的中心点坐标
|
| 319 |
-
# - 180: 旋转的度数,正度数表示逆时针旋转,而负度数表示顺时针旋转。
|
| 320 |
-
# - 1.0:旋转后图像的大小,1.0原图,2.0变成原来的2倍,0.5变成原来的0.5倍
|
| 321 |
-
# 1° = π/180弧度 1 弧度 = 180 / 3.1415926 // 0.0190033 是Mathematica 算出来的弧度,先转换成角度 // -0.0190033 * (180 / 3.1415926)
|
| 322 |
-
M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0)
|
| 323 |
-
img_color = cv2.warpAffine(img_color, M, (w, h))
|
| 324 |
-
img_color_transform = img_color.copy()
|
| 325 |
-
|
| 326 |
-
# cv2.imshow("after trans", img_color)
|
| 327 |
-
# cv2.waitKey(0)
|
| 328 |
-
|
| 329 |
-
# https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html # 原理
|
| 330 |
-
# https://stackoverflow.com/questions/30327659/how-can-i-remap-a-point-after-an-image-rotation # How can I remap a point after an image rotation?
|
| 331 |
-
# 如何得到移动后的坐标点
|
| 332 |
-
|
| 333 |
-
# points 算出四个点变换后移动到哪里了
|
| 334 |
-
points = np.array([[word_x, word_y], # 左上
|
| 335 |
-
# 右上
|
| 336 |
-
[word_x + word_width, word_y],
|
| 337 |
-
[word_x + word_width, word_y + \
|
| 338 |
-
word_height], # 右下
|
| 339 |
-
[word_x, word_y + word_height], # 左下
|
| 340 |
-
])
|
| 341 |
-
# add ones
|
| 342 |
-
ones = np.ones(shape=(len(points), 1))
|
| 343 |
-
|
| 344 |
-
points_ones = np.hstack([points, ones])
|
| 345 |
-
|
| 346 |
-
# transform points
|
| 347 |
-
transformed_points = M.dot(points_ones.T).T
|
| 348 |
-
|
| 349 |
-
transformed_points_int = np.round(
|
| 350 |
-
transformed_points, decimals=0).astype(np.int32) # 批量四舍五入
|
| 351 |
-
|
| 352 |
-
# cv2.polylines(img_color, [transformed_points_int], isClosed=True, color=(
|
| 353 |
-
# 0, 0, 255), thickness=2) # 画转换后的点
|
| 354 |
-
|
| 355 |
-
# cv2.polylines(img_color_origin, [points], isClosed=True, color=(
|
| 356 |
-
# random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), thickness=2) # 画转换前的点
|
| 357 |
-
|
| 358 |
-
# cv2.imshow("orgin", img_color_origin)
|
| 359 |
-
# cv2.waitKey(0)
|
| 360 |
-
|
| 361 |
-
# 四个角的位置 # 左上、右上、右下、左下,当NeedRotate为true时,如果最外层的angle不为0,需要按照angle矫正图片后,坐标才准确(错,经验证不需要)
|
| 362 |
-
pos = jo["pos"]
|
| 363 |
-
x = int(pos[0]["x"]) # 左上
|
| 364 |
-
y = int(pos[0]["y"])
|
| 365 |
-
|
| 366 |
-
x2 = int(pos[2]["x"]) # 右下
|
| 367 |
-
y2 = int(pos[2]["y"])
|
| 368 |
-
|
| 369 |
-
lu = [pos[0]['x'], pos[0]['y']] # left up 四个角顺时针方向数
|
| 370 |
-
ru = [pos[1]['x'], pos[1]['y']]
|
| 371 |
-
rd = [pos[2]['x'], pos[2]['y']]
|
| 372 |
-
ld = [pos[3]['x'], pos[3]['y']]
|
| 373 |
-
|
| 374 |
-
# 生成 icdar2015 格式的人工标记训练数据(用于训练 mmocr)
|
| 375 |
-
# gt_txt_list.append( "{},{},{},{},{},{},{},{},{}".format(lu[0], lu[1], ru[0], ru[1], rd[0], rd[1], ld[0], ld[1], word) )
|
| 376 |
-
|
| 377 |
-
# 绘制矩形
|
| 378 |
-
start_point = (x, y) # 矩形的左上角
|
| 379 |
-
|
| 380 |
-
end_point = (x2, y2) # 矩形的右下角
|
| 381 |
-
|
| 382 |
-
color = (0, 0, 255) # BGR
|
| 383 |
-
|
| 384 |
-
thickness = 2
|
| 385 |
-
|
| 386 |
-
# 逐行画框
|
| 387 |
-
# img_color_origin2 = cv2.rectangle(img_color_origin2, start_point, end_point, color, thickness)
|
| 388 |
-
# cv2.imshow("box", img_color_origin2)
|
| 389 |
-
|
| 390 |
-
# cv2.waitKey(0)
|
| 391 |
-
|
| 392 |
-
points = [lu, ru, rd, ld]
|
| 393 |
-
|
| 394 |
-
points0 = np.array([[word_x, word_y], # 左上
|
| 395 |
-
# 右上
|
| 396 |
-
[word_x + word_width, word_y],
|
| 397 |
-
[word_x + word_width, word_y + \
|
| 398 |
-
word_height], # 右下
|
| 399 |
-
[word_x, word_y + word_height], # 左下
|
| 400 |
-
])
|
| 401 |
-
points1 = np.array([lu, ru, rd, ld])
|
| 402 |
-
|
| 403 |
-
if not (abs(angle) == 90 or abs(angle) == 270) and angle != 0:
|
| 404 |
-
points = transform(points, M)
|
| 405 |
-
else:
|
| 406 |
-
points = np.array(points)
|
| 407 |
-
|
| 408 |
-
ps3 = np.array(
|
| 409 |
-
[
|
| 410 |
-
[min(points[0][0], points1[0][0]), min(
|
| 411 |
-
points[0][1], points1[0][1])], # 左上(取最两者中最小的)
|
| 412 |
-
|
| 413 |
-
[max(points[1][0], points1[1][0]), min(
|
| 414 |
-
points[1][1], points1[1][1])], # 右上
|
| 415 |
-
|
| 416 |
-
[max(points[2][0], points1[2][0]), max(
|
| 417 |
-
points[2][1], points1[2][1])], # 右下
|
| 418 |
-
|
| 419 |
-
[min(points[3][0], points1[3][0]), max(
|
| 420 |
-
points[3][1], points1[3][1])] # 左下
|
| 421 |
-
]
|
| 422 |
-
)
|
| 423 |
-
|
| 424 |
-
# img_cuted = cutPoly(img, points1)
|
| 425 |
-
# cv2.imwrite(f'./tmp/{g_count2}.jpg', img_cuted)
|
| 426 |
-
# with open(f'./tmp/{g_count2}.txt', 'w', encoding='utf-8') as f:
|
| 427 |
-
# f.write(word)
|
| 428 |
-
# g_count2 += 1
|
| 429 |
-
|
| 430 |
-
# cv2.polylines(img_color_origin, [points], isClosed=True, color=( # 多边形,框得比较全
|
| 431 |
-
# 100, 0, 255), thickness=2) # 只画线,不填充
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
arr.append( {"transcription":f"{word}","points":[lu, ru, rd, ld],"difficult":False} )
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
cv2.polylines(img_color_origin, [points1], isClosed=True, color=(
|
| 438 |
-
random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), thickness=2) # 画转换前的点
|
| 439 |
-
|
| 440 |
-
# cv2.polylines(img_color_origin, [ps3], isClosed=True, color=(255, 0, 0), thickness=2)
|
| 441 |
-
|
| 442 |
-
# cv2.imshow("orgin", img_color_origin)
|
| 443 |
-
# cv2.waitKey(0)
|
| 444 |
-
|
| 445 |
-
# break
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
arr_str = string(arr)
|
| 449 |
-
line = f'{dst_img_path}\t{arr_str}\n'
|
| 450 |
-
label += line
|
| 451 |
-
|
| 452 |
-
print( f'{g_count - 1} / {len(json_paths)} task done.' )
|
| 453 |
-
|
| 454 |
-
ks = list( dic_words.keys() )
|
| 455 |
-
|
| 456 |
-
keys = '\n'.join(ks)
|
| 457 |
-
|
| 458 |
-
with open(label_path, "w", encoding='utf-8') as fp:
|
| 459 |
-
fp.write(label)
|
| 460 |
-
|
| 461 |
-
with open(key_path, "w", encoding='utf-8') as fp:
|
| 462 |
-
fp.write(keys)
|
| 463 |
-
|
| 464 |
-
with open(fileState_path, "w", encoding='utf-8') as fp:
|
| 465 |
-
fp.write(states)
|
| 466 |
-
|
| 467 |
-
print('all task done.')
|
| 468 |
-
|
| 469 |
-
def show_box_shidianguji(pth_img):
|
| 470 |
-
imgData = np.fromfile(pth_img, dtype=np.uint8)
|
| 471 |
-
img = cv2.imdecode(imgData, cv2.IMREAD_UNCHANGED)
|
| 472 |
-
|
| 473 |
-
if len(img.shape) != 3: # 转彩图
|
| 474 |
-
img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 475 |
-
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # DBNet 原版只能处理彩图,这里转一下
|
| 476 |
-
else:
|
| 477 |
-
img_color = img.copy()
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
charPoly = {
|
| 481 |
-
"x0": 537, "y0": 67, "x1": 578, "y1": 66, "x2": 578, "y2": 122, "x3": 537,"y3": 123
|
| 482 |
-
}
|
| 483 |
-
|
| 484 |
-
lu = [charPoly["x0"], charPoly["y0"]]
|
| 485 |
-
ru = [charPoly["x1"], charPoly["y1"]]
|
| 486 |
-
rd = [charPoly["x2"], charPoly["y2"]]
|
| 487 |
-
ld = [charPoly["x3"], charPoly["y3"]]
|
| 488 |
-
points = np.array([lu, ru, rd, ld])
|
| 489 |
-
|
| 490 |
-
cv2.polylines(img_color, [points], isClosed=True, color=( # 多边形,框得比较全
|
| 491 |
-
100, 0, 255), thickness=2) # 只画线,不填充
|
| 492 |
-
|
| 493 |
-
cv2.imshow("box", img_color)
|
| 494 |
-
cv2.waitKey(0)
|
| 495 |
-
|
| 496 |
-
pass
|
| 497 |
-
|
| 498 |
-
if __name__ == "__main__":
|
| 499 |
-
# do_convert_aliocr()
|
| 500 |
-
show_box_shidianguji("out/images/0000_1kzz90e16h5ic.webp")
|
| 501 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ocr/qwen_ocr.py
DELETED
|
@@ -1,43 +0,0 @@
|
|
| 1 |
-
# 如果没有安装OpenAI Python SDK,可使用命令安装:pip install OpenAI
|
| 2 |
-
|
| 3 |
-
import base64
|
| 4 |
-
from openai import OpenAI
|
| 5 |
-
|
| 6 |
-
# 读取图片并转为 base64
|
| 7 |
-
image_path = "0375.jpg"
|
| 8 |
-
with open(image_path, "rb") as f:
|
| 9 |
-
image_data = base64.b64encode(f.read()).decode("utf-8")
|
| 10 |
-
|
| 11 |
-
# 初始化客户端
|
| 12 |
-
client = OpenAI(
|
| 13 |
-
base_url="https://www.autodl.art/api/v1",
|
| 14 |
-
api_key="7bPZpONEQgxsP7kniD1AzmP3Tr0e84nmfExGroS1v11CBUKi",
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
# 调用接口(这里使用了stream=True进行流式响应)
|
| 18 |
-
stream = client.chat.completions.create(
|
| 19 |
-
model="qwen3.6-plus",
|
| 20 |
-
messages=[
|
| 21 |
-
{
|
| 22 |
-
"role": "user",
|
| 23 |
-
"content": [
|
| 24 |
-
{
|
| 25 |
-
"type": "text",
|
| 26 |
-
"text": "提取图片中的文字,按阅读顺序输出"
|
| 27 |
-
},
|
| 28 |
-
{
|
| 29 |
-
"type": "image_url",
|
| 30 |
-
"image_url": {
|
| 31 |
-
"url": f"data:image/jpeg;base64,{image_data}"
|
| 32 |
-
}
|
| 33 |
-
}
|
| 34 |
-
]
|
| 35 |
-
}
|
| 36 |
-
],
|
| 37 |
-
stream=True,
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
# 流式打印输出
|
| 41 |
-
for chunk in stream:
|
| 42 |
-
if chunk.choices and chunk.choices[0].delta.content:
|
| 43 |
-
print(chunk.choices[0].delta.content, end="")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|