This commit is contained in:
2023-11-16 06:25:12 +08:00
parent 50e45944d9
commit 627300d8fd

15
main.py
View File

@@ -19,6 +19,7 @@ def download_image(url:str) -> Image:
oss_auth = oss2.Auth(config['OSS_ACCESS_KEY_ID'], config['OSS_ACCESS_KEY_SECRET']) oss_auth = oss2.Auth(config['OSS_ACCESS_KEY_ID'], config['OSS_ACCESS_KEY_SECRET'])
return Image.open(io.BytesIO(oss2.Bucket(oss_auth, f'http://{config["OSS_HOST"]}', config['OSS_BUCKET_NAME']).get_object(url).read())) return Image.open(io.BytesIO(oss2.Bucket(oss_auth, f'http://{config["OSS_HOST"]}', config['OSS_BUCKET_NAME']).get_object(url).read()))
except Exception: except Exception:
print('图片下载失败:', url)
return None return None
else: else:
try: try:
@@ -31,16 +32,26 @@ def download_image(url:str) -> Image:
import pymysql import pymysql
import pymysql.cursors import pymysql.cursors
import cnocr
ocr = cnocr.CnOcr(rec_model_name='ch_PP-OCRv3')
conn = pymysql.connect(host=config['MYSQL_HOST'], user=config['MYSQL_USER'], password=config['MYSQL_PASSWORD'], database=config['MYSQL_NAME'], cursorclass=pymysql.cursors.DictCursor) conn = pymysql.connect(host=config['MYSQL_HOST'], user=config['MYSQL_USER'], password=config['MYSQL_PASSWORD'], database=config['MYSQL_NAME'], cursorclass=pymysql.cursors.DictCursor)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute("SELECT id, content FROM web_images LIMIT 10") cursor.execute("SELECT id, content FROM web_images LIMIT 5")
# 获取查询结果 # 获取查询结果
rows = cursor.fetchall() rows = cursor.fetchall()
for row in rows: for row in rows:
print(row) #print(row)
image = download_image(row['content']) image = download_image(row['content'])
if image is None:
print('图片下载失败,跳过')
continue
out = ocr.ocr(image)
# 这段代码将只包含那些非空、不是纯数字且长度大于1的'text'值
texts = [item['text'] for item in out if item['text'] and not item['text'].isdigit() and len(item['text']) > 1]
print(texts)
# 关闭游标和连接 # 关闭游标和连接
cursor.close() cursor.close()