Add database functionality and update image
processing
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -129,6 +129,8 @@ ENV/
 | 
				
			|||||||
env.bak/
 | 
					env.bak/
 | 
				
			||||||
venv.bak/
 | 
					venv.bak/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					database
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Spyder project settings
 | 
					# Spyder project settings
 | 
				
			||||||
.spyderproject
 | 
					.spyderproject
 | 
				
			||||||
.spyproject
 | 
					.spyproject
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										55
									
								
								main.py
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										55
									
								
								main.py
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							@@ -1,9 +1,35 @@
 | 
				
			|||||||
 | 
					#!/usr/bin/env python3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import io
 | 
					import io
 | 
				
			||||||
import oss2
 | 
					 | 
				
			||||||
import requests
 | 
					import requests
 | 
				
			||||||
 | 
					import oss2
 | 
				
			||||||
 | 
					import plyvel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from PIL import Image, ImageFile
 | 
					from PIL import Image, ImageFile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 创建或打开一个数据库
 | 
				
			||||||
 | 
					db = plyvel.DB('database', create_if_missing=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					'''
 | 
				
			||||||
 | 
					# 写入一个键值对
 | 
				
			||||||
 | 
					db.put(b'key', b'value')
 | 
				
			||||||
 | 
					# 获取一个键的值
 | 
				
			||||||
 | 
					value = db.get(b'key')
 | 
				
			||||||
 | 
					# 删除一个键值对
 | 
				
			||||||
 | 
					db.delete(b'key')
 | 
				
			||||||
 | 
					# 批量写入
 | 
				
			||||||
 | 
					with db.write_batch() as wb:
 | 
				
			||||||
 | 
					    for i in range(10000):
 | 
				
			||||||
 | 
					        wb.put(b'key' + str(i).encode(), b'value' + str(i).encode())
 | 
				
			||||||
 | 
					# 迭代数据库中的所有键值对
 | 
				
			||||||
 | 
					for key, value in db:
 | 
				
			||||||
 | 
					    print(key, value)
 | 
				
			||||||
 | 
					# 关闭数据库
 | 
				
			||||||
 | 
					db.close()
 | 
				
			||||||
 | 
					'''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 读取 .env
 | 
					# 读取 .env
 | 
				
			||||||
from dotenv import dotenv_values
 | 
					from dotenv import dotenv_values
 | 
				
			||||||
config = dotenv_values(".env")
 | 
					config = dotenv_values(".env")
 | 
				
			||||||
@@ -19,39 +45,44 @@ def download_image(url:str) -> Image.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)
 | 
					            print('图片从OSS下载失败:', url)
 | 
				
			||||||
            return None
 | 
					            return None
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            response = requests.get(url)
 | 
					            response = requests.get(url)
 | 
				
			||||||
            return Image.open(io.BytesIO(response.content))
 | 
					            return Image.open(io.BytesIO(response.content))
 | 
				
			||||||
        except Exception:
 | 
					        except Exception:
 | 
				
			||||||
            print('图片下载失败:', url)
 | 
					            print('图片从URL下载失败:', url)
 | 
				
			||||||
            return None
 | 
					            return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
import pymysql
 | 
					import pymysql
 | 
				
			||||||
import pymysql.cursors
 | 
					import pymysql.cursors
 | 
				
			||||||
import cnocr
 | 
					import cnocr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 打开 mysql
 | 
				
			||||||
ocr = cnocr.CnOcr(rec_model_name='ch_PP-OCRv3')
 | 
					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 5")
 | 
					cursor.execute("SELECT id, content FROM web_images LIMIT 5")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 获取查询结果
 | 
					# 获取查询结果(跳过下载失败的)
 | 
				
			||||||
rows = cursor.fetchall()
 | 
					for item in cursor.fetchall():
 | 
				
			||||||
for row in rows:
 | 
					    image = download_image(item['content'])
 | 
				
			||||||
    image = download_image(row['content'])
 | 
					 | 
				
			||||||
    if image is None:
 | 
					    if image is None:
 | 
				
			||||||
        print('图片下载失败,跳过')
 | 
					 | 
				
			||||||
        continue
 | 
					        continue
 | 
				
			||||||
 | 
					    # 将只包含那些非空非纯数字且长度大于1的'text'值
 | 
				
			||||||
    out = ocr.ocr(image)
 | 
					    out = ocr.ocr(image)
 | 
				
			||||||
    # 这段代码将只包含那些非空、不是纯数字且长度大于1的'text'值
 | 
					    texts = [x['text'] for x in out if x['text'] and not x['text'].isdigit() and len(x['text']) > 1]
 | 
				
			||||||
    texts = [item['text'] for item in out if item['text'] and not item['text'].isdigit() and len(item['text']) > 1]
 | 
					    print(item['id'], texts)
 | 
				
			||||||
    print(row['id'], texts)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # 将结果存入 leveldb
 | 
				
			||||||
 | 
					    # db.put(str(row['id']).encode(), ','.join(texts).encode())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 关闭游标和连接
 | 
					# 关闭游标和连接
 | 
				
			||||||
cursor.close()
 | 
					cursor.close()
 | 
				
			||||||
conn.close()
 | 
					conn.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 关闭数据库
 | 
				
			||||||
 | 
					db.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print('Done')
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user