From 0c0d09fa80f78b0d2084b648f1978a2b62342309 Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Fri, 28 Mar 2025 15:40:39 +0800 Subject: [PATCH] 'commit' --- AI/WxMini/Start.py | 18 ++++------ AI/WxMini/Test/T7_TianQi2.py | 3 -- AI/WxMini/Test/getImageWidthHeight.py | 5 +++ AI/WxMini/Utils/ImageUtil.py | 33 ++++++++++++++---- AI/WxMini/Utils/MySQLUtil.py | 11 +++--- .../__pycache__/ImageUtil.cpython-310.pyc | Bin 2933 -> 3276 bytes 6 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 AI/WxMini/Test/getImageWidthHeight.py 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 9579bb0ce92b1ead0737be101add761fb1dbe084..ab06ac1023cfb50c4f486e13db83cc368b4bbb1c 100644 GIT binary patch delta 959 zcmaKqTTc@~6vyYxUc0@>O`u!^yg(Xh0W}2^E)Vk1CO|ZeHf}To*`?jm?v~xHLfxhq ze9*+0;3mEhB0TzN{0zSN3HAe+_~46)5Bj2WiW-fHGnwC>`On!iXJ*cq?)UMS6^$x{ zwcPa&g~_h{*Z|zwzqoXoXiyHTVJ)mh9uqa9L5?J%Z@h61zWJ}Y1xWZGxmPgmKjoi8 z%I_5h;j^C>7McD~=x=5{vBYVyI4SWZVUgVJXh@!>drb$C^#Un{; zI|d+I>8;^3sdu`v2Ojmf1LmVewV2k9eJ_+HS|r2%%(HKf@Wc)6Yy)Y?KD^ea_HAqE2=@6zw~)Z1mr z1VxqBs>bT3zN7l@xP?~D_QeF4=`goHTH5QtUV zs#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