107 lines
4.5 KiB
Python
107 lines
4.5 KiB
Python
import base64
|
|
import requests
|
|
import json
|
|
|
|
def image_to_base64(image_path):
|
|
"""
|
|
将图片文件转换为base64编码
|
|
"""
|
|
with open(image_path, "rb") as image_file:
|
|
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
|
return encoded_string
|
|
|
|
def test_ocr_api(image_path, api_url="http://10.0.0.202:12342/api/v1/ocr_from_base64"):
|
|
"""
|
|
测试OCR API接口
|
|
"""
|
|
# 将图片转换为base64
|
|
image_base64 = image_to_base64(image_path)
|
|
|
|
with open("base64.txt", "w") as f:
|
|
f.write(repr(image_base64))
|
|
|
|
# 准备请求数据
|
|
payload = {
|
|
"image_base64": image_base64,
|
|
"image_type": "jpg" # 根据实际图片类型修改
|
|
}
|
|
|
|
# 发送POST请求
|
|
headers = {
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
try:
|
|
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
print("OCR识别结果:")
|
|
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
return result
|
|
else:
|
|
print(f"请求失败,状态码: {response.status_code}")
|
|
print(response.text)
|
|
return None
|
|
except Exception as e:
|
|
print(f"请求异常: {str(e)}")
|
|
return None
|
|
|
|
def test_detect(image_path: str, api_url:str = "http://10.0.0.202:12342/api/v1/detect_from_base64_0"):
|
|
"""
|
|
测试yolov8消防区域侵占接口
|
|
"""
|
|
image_base64 = image_to_base64(image_path)
|
|
# 准备请求数据
|
|
payload = {
|
|
"image_base64": image_base64,
|
|
"image_type": "jpg" # 根据实际图片类型修改
|
|
}
|
|
|
|
# 发送POST请求
|
|
headers = {
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
try:
|
|
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
|
|
if response.status_code == 200:
|
|
result = response.json()
|
|
print("detect识别结果:")
|
|
print(json.dumps(result, ensure_ascii=False, indent=2))
|
|
return result
|
|
else:
|
|
print(f"请求失败,状态码: {response.status_code}")
|
|
print(response.text)
|
|
return None
|
|
except Exception as e:
|
|
print(f"请求异常: {str(e)}")
|
|
return None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# 测试图片路径,请根据实际情况修改
|
|
# test_image_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/data_image/001读表图片/2c7cc83019e7388a7041101da92c9829_frame_000000.jpg"
|
|
|
|
#---------------------------------------测试ocr-----------------------------------------
|
|
test_image_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/data_image/001读表图片/632e474452d560edd7004f745319ff00_frame_000730.jpg"
|
|
|
|
api_url="http://10.0.0.202:12342/api/v1/ocr_onnx_from_base64"
|
|
# 调用测试函数
|
|
test_ocr_api(test_image_path, api_url)
|
|
#---------------------------------------测试ocrender-----------------------------------------
|
|
|
|
# #-----------------------------------------测试yolov8 侵占消防区域检测-----------------------------------------
|
|
# # test_image_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/YoloV8Obj/dataset_20250819/train/images/1e4c75b76e531606e2adc491a8f09ae8_frame_000000.jpg"
|
|
# test_image_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/YoloV8Obj/dataset_20250819/train/images/1e4c75b76e531606e2adc491a8f09ae8_frame_000720.jpg"
|
|
# api_url = "http://10.0.0.202:12342/api/v1/detect_from_base64_0"
|
|
# test_detect(test_image_path)
|
|
# #-----------------------------------------测试yolov8 侵占消防区域检测 end-----------------------------------------
|
|
|
|
|
|
# #-----------------------------------------测试yolov8 灭火器检测-----------------------------------------
|
|
# test_image_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/YoloV8Obj/dataset_20250819/train/images/ce81420a27cdaff14fe42f967eaa49a3_frame_001060.jpg"
|
|
# # test_image_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/YoloV8Obj/dataset_20250819/train/images/1e4c75b76e531606e2adc491a8f09ae8_frame_000120.jpg"
|
|
# # test_image_path = "/home/admin-root/haotian/康达瑞贝斯机器狗/YoloV8Obj/dataset_20250819/train/images/1e4c75b76e531606e2adc491a8f09ae8_frame_000120.jpg"
|
|
# api_url = "http://10.0.0.202:12342/api/v1/detect_from_base64_1"
|
|
# test_detect(test_image_path, api_url=api_url)
|
|
# #-----------------------------------------测试yolov8 灭火器检测 end----------------------------------------- |