diff --git a/AI/WxMini/Start.py b/AI/WxMini/Start.py index a4c2aef2..a9f3cbe5 100644 --- a/AI/WxMini/Start.py +++ b/AI/WxMini/Start.py @@ -602,12 +602,9 @@ async def generate_upload_params(current_user: dict = Depends(get_current_user)) @app.get("/aichat/recognize_content") -async def web_recognize_content(image_url: str - #, current_user: dict = Depends(get_current_user) - ): - #logger.info(f"current_user:{current_user['login_name']}") - #person_id = current_user['person_id'] - person_id = "1" +async def web_recognize_content(image_url: str, current_user: dict = Depends(get_current_user)): + logger.info(f"current_user:{current_user['login_name']}") + person_id = current_user['person_id'] try: async def generate_stream(): # 假设 recognize_content 是一个异步生成器,逐条返回识别结果 @@ -647,12 +644,9 @@ async def web_recognize_text(image_url: str, current_user: dict = Depends(get_cu @app.get("/aichat/recognize_math") -async def web_recognize_math(image_url: str - #, current_user: dict = Depends(get_current_user) - ): - #logger.info(f"current_user:{current_user['login_name']}") - #person_id = current_user['person_id'] - person_id="1" +async def web_recognize_math(image_url: str, current_user: dict = Depends(get_current_user)): + logger.info(f"current_user:{current_user['login_name']}") + person_id = current_user['person_id'] try: async def generate_stream(): # 假设 recognize_content 是一个异步生成器,逐条返回识别结果 diff --git a/AI/WxMini/Test/T7_TianQi2.py b/AI/WxMini/Test/T7_TianQi2.py index 81a9d842..63dad268 100644 --- a/AI/WxMini/Test/T7_TianQi2.py +++ b/AI/WxMini/Test/T7_TianQi2.py @@ -1,6 +1,3 @@ -import requests -from bs4 import BeautifulSoup - from WxMini.Utils.TianQiUtil import * diff --git a/AI/WxMini/Test/getImageWidthHeight.py b/AI/WxMini/Test/getImageWidthHeight.py new file mode 100644 index 00000000..babb9177 --- /dev/null +++ b/AI/WxMini/Test/getImageWidthHeight.py @@ -0,0 +1,5 @@ +img_url = 'https://hzkc.oss-cn-beijing.aliyuncs.com/Upload/upsUnGrUnDPjsAXrRXR2pwVtKLZMVf1L.jpg' + +from WxMini.Utils.ImageUtil import * + +print(getImgWidthHeight(img_url)) diff --git a/AI/WxMini/Utils/ImageUtil.py b/AI/WxMini/Utils/ImageUtil.py index 657cbaab..915a8d88 100644 --- a/AI/WxMini/Utils/ImageUtil.py +++ b/AI/WxMini/Utils/ImageUtil.py @@ -1,7 +1,6 @@ import time - +import requests from openai import OpenAI, AsyncOpenAI - from WxMini.Milvus.Config.MulvusConfig import MODELSCOPE_ACCESS_TOKEN from WxMini.Utils.MySQLUtil import save_chat_to_mysql @@ -44,9 +43,12 @@ async def recognize_text(client, pool, person_id, image_url): print(char, end='') time.sleep(0.1) # 控制输出速度 + # 获取图片宽高 + image_width, image_height = getImgWidthHeight(image_url) + # 记录到数据库 try: - await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 1) + await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 1, image_width, image_height) except Exception as e: print(f"记录到数据库时出错:{e}") @@ -70,12 +72,14 @@ async def recognize_content(client, pool, person_id, image_url): if char != ' ': yield char # 流式输出字符 full_text += char # 拼接字符 - #print(char, end='') + # print(char, end='') time.sleep(0.1) # 控制输出速度 + # 获取图片宽高 + image_width, image_height = getImgWidthHeight(image_url) # 记录到数据库 try: - await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 2) + await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 2, image_width, image_height) except Exception as e: print(f"记录到数据库时出错:{e}") @@ -98,7 +102,7 @@ async def recognize_math(pool, person_id, image_url): completion = await client.chat.completions.create( model="Qwen/Qwen2.5-VL-32B-Instruct", - #model="Qwen/Qwen2.5-VL-72B-Instruct", + # model="Qwen/Qwen2.5-VL-72B-Instruct", messages=[ { "role": "system", @@ -129,9 +133,24 @@ async def recognize_math(pool, person_id, image_url): full_text += char # 拼接字符 print(char, end='') time.sleep(0.1) # 控制输出速度 + # 获取图片宽高 + image_width, image_height = getImgWidthHeight(image_url) # 记录到数据库 try: - await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 1) + await save_chat_to_mysql(pool, person_id, f'{image_url}', full_text, "", 0, 2, 2, 1, image_width, image_height) except Exception as e: print(f"记录到数据库时出错:{e}") + + +# 获取图片的宽高 +def getImgWidthHeight(img_url): + try: + url = f'{img_url}?x-oss-process=image/info' + response = requests.get(url) + jo = response.json() + width = int(jo['ImageWidth']['value']) + height = int(jo['ImageHeight']['value']) + return width, height + except: + return 182, 182 diff --git a/AI/WxMini/Utils/MySQLUtil.py b/AI/WxMini/Utils/MySQLUtil.py index 24d911ce..7560e77a 100644 --- a/AI/WxMini/Utils/MySQLUtil.py +++ b/AI/WxMini/Utils/MySQLUtil.py @@ -27,13 +27,14 @@ async def init_mysql_pool(): # 保存聊天记录到 MySQL async def save_chat_to_mysql(mysql_pool, person_id, prompt, result, audio_url, duration, input_type=1, output_type=1, - input_image_type=0): + input_image_type=0, image_width=0, image_height=0): async with mysql_pool.acquire() as conn: await conn.ping() # 重置连接 async with conn.cursor() as cur: await cur.execute( - "INSERT INTO t_chat_log (person_id, user_input, model_response,audio_url,duration,input_type,output_type,input_image_type,create_time) VALUES (%s, %s, %s, %s, %s, %s, %s,%s,NOW())", - (person_id, prompt, result, audio_url, duration, input_type, output_type, input_image_type) + "INSERT INTO t_chat_log (person_id, user_input, model_response,audio_url,duration,input_type,output_type,input_image_type,image_width,image_height,create_time) VALUES (%s, %s, %s, %s, %s, %s, %s,%s,%s,%s,NOW())", + (person_id, prompt, result, audio_url, duration, input_type, output_type, input_image_type, image_width, + image_height) ) await conn.commit() @@ -82,7 +83,7 @@ async def get_chat_log_by_session(mysql_pool, person_id, page=1, page_size=10): # 查询分页数据,按 id 降序排列 await cur.execute( - "SELECT id, person_id, user_input, model_response, audio_url, duration,input_type,output_type,input_image_type, create_time " + "SELECT id, person_id, user_input, model_response, audio_url, duration,input_type,output_type,input_image_type,image_width,image_height, create_time " "FROM t_chat_log WHERE person_id = %s ORDER BY id DESC LIMIT %s OFFSET %s", (person_id, page_size, offset) ) @@ -103,6 +104,8 @@ async def get_chat_log_by_session(mysql_pool, person_id, page=1, page_size=10): "duration": record['duration'], "input_type": record['input_type'], "output_type": record['output_type'], + "image_width": record['image_width'], + "image_height": record['image_height'], "input_image_type": record['input_image_type'], "create_time": record['create_time'].strftime("%Y-%m-%d %H:%M:%S") } diff --git a/AI/WxMini/Utils/__pycache__/ImageUtil.cpython-310.pyc b/AI/WxMini/Utils/__pycache__/ImageUtil.cpython-310.pyc index 9579bb0c..ab06ac10 100644 Binary files a/AI/WxMini/Utils/__pycache__/ImageUtil.cpython-310.pyc and b/AI/WxMini/Utils/__pycache__/ImageUtil.cpython-310.pyc differ