diff --git a/main.py b/main.py index 59ab0d1..1003864 100644 --- a/main.py +++ b/main.py @@ -19,6 +19,7 @@ def download_image(url:str) -> Image: 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())) except Exception: + print('图片下载失败:', url) return None else: try: @@ -31,16 +32,26 @@ def download_image(url:str) -> Image: import pymysql 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) 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() for row in rows: - print(row) + #print(row) 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()