commit 0bbf3daf74ec61ae5f1627426c13258453340c1c Author: wangzixiang <17839623189@163.com> Date: Sun Oct 20 11:08:24 2024 +0800 init diff --git a/docs/0bec4563f67e5c03c7bf8c75a1618dd.png b/docs/0bec4563f67e5c03c7bf8c75a1618dd.png new file mode 100644 index 0000000..9203a55 Binary files /dev/null and b/docs/0bec4563f67e5c03c7bf8c75a1618dd.png differ diff --git a/docs/14da2209e097daa5deda08fa4f5123e.png b/docs/14da2209e097daa5deda08fa4f5123e.png new file mode 100644 index 0000000..45a4483 Binary files /dev/null and b/docs/14da2209e097daa5deda08fa4f5123e.png differ diff --git a/img/1.jpg b/img/1.jpg new file mode 100644 index 0000000..df87ebb Binary files /dev/null and b/img/1.jpg differ diff --git a/img/2.jpg b/img/2.jpg new file mode 100644 index 0000000..6a67efa Binary files /dev/null and b/img/2.jpg differ diff --git a/img2text.py b/img2text.py new file mode 100644 index 0000000..c9204b9 --- /dev/null +++ b/img2text.py @@ -0,0 +1,62 @@ +# usr/bin/python3 +import requests +import json + +# 需填写appkey +appkey = "app-XXX" + +# 上传图片 +def upload_img(imgpath="test.jpg"): + + headers = { + 'Authorization': f'Bearer {appkey}', + } + + files = { + 'file': (imgpath, open(imgpath, 'rb'), 'image/jpg'), # 使用实际文件路径和类型替换 + } + + response = requests.post(url = 'http://sl.vrgon.com:6062/v1/files/upload', headers=headers, files=files, data={'user': 'robot'}) + + img_id = json.loads(response.content)["id"] + + return img_id + +# 计算 +def img2text(path="./img/test.jpg"): + img_id = upload_img(path) + payload = { + "inputs": {}, + "query": "请问这道数学题的答案是多少,请直接给出数字答案", + "response_mode": "blocking", + "conversation_id": "", + "user": "robot", + "files": [ + { + "type": "image", + "transfer_method": "local_file", + "upload_file_id": img_id + } + ] + } + + headers = { + 'Authorization': f'Bearer {appkey}', + 'Content-Type': 'application/json', + } + + response = requests.post(url = "http://sl.vrgon.com:6062/v1/chat-messages", headers=headers, data=json.dumps(payload)) + + ans = "" + if response.status_code == 200: + for line in response.iter_lines(): + # print(line.decode('unicode_escape')) + temp = json.loads(line) + ans += temp["answer"] + + return ans + + + +if __name__=="__main__": + print(img2text(path="./img/2.jpg")) \ No newline at end of file diff --git a/img2text2.py b/img2text2.py new file mode 100644 index 0000000..d921b26 --- /dev/null +++ b/img2text2.py @@ -0,0 +1,57 @@ +# usr/bin/python2.7 +import requests +import json + +# 需填写appkey +appkey = "app-XXX" + +# 上传图片 +def upload_img(imgpath="test.jpg"): + headers = { + 'Authorization': 'Bearer {0}'.format(appkey), + } + + with open(imgpath, 'rb') as f: + files = {'file': (imgpath, f, 'image/jpg')} + + response = requests.post(url = 'http://sl.vrgon.com:6062/v1/files/upload', headers=headers, files=files, data={'user': 'robot'}) + + img_id = json.loads(response.content)["id"] + + return img_id + +# 计算 +def img2text(path="./img/test.jpg"): + img_id = upload_img(path) + payload = { + "inputs": {}, + "query": "请问这道数学题的答案是多少,请直接给出数字答案", + "response_mode": "blocking", + "conversation_id": "", + "user": "robot", + "files": [ + { + "type": "image", + "transfer_method": "local_file", + "upload_file_id": img_id + } + ] + } + + headers = { + 'Authorization': 'Bearer {0}'.format(appkey), + 'Content-Type': 'application/json', + } + + response = requests.post(url = "http://sl.vrgon.com:6062/v1/chat-messages", headers=headers, data=json.dumps(payload)) + + ans = "" + if response.status_code == 200: + for line in response.iter_lines(): + temp = json.loads(line) + ans += temp["answer"] + + return ans + +if __name__=="__main__": + print img2text(path="./img/2.jpg") \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e6ce808 --- /dev/null +++ b/readme.md @@ -0,0 +1,10 @@ +# 根据图片生成答案 + +## img2text.py 为python3 版本 (python3.8测试可用) + +## img2text2.py 为python2 版本 + +# sample +![](docs/0bec4563f67e5c03c7bf8c75a1618dd.png) + +![](docs/14da2209e097daa5deda08fa4f5123e.png)