123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import requests
- import json
- class OCRClient:
- def __init__(self, base_url):
- self.base_url = base_url
- def detect_barcode(self, image_path):
- url = f"{self.base_url}/detect_barcode"
- with open(image_path, 'rb') as f:
- files = {'file': f}
- response = requests.post(url, files=files)
- return response.json()
- def get_barcode(self, image_path=None, message=None):
- url = f"{self.base_url}/get_barcode"
- data = {}
- files = {}
-
- if image_path:
- with open(image_path, 'rb') as f:
- files = {'file': f}
-
- if message:
- data = {'message': json.dumps(message)}
-
- response = requests.post(url, data=data, files=files)
- return response.json()
- def search_info(self, search_params):
- url = f"{self.base_url}/search"
- response = requests.post(url, json=search_params)
- return response.json()
- def get_matio_id(self, image_path):
- url = f"{self.base_url}/get_matio_id"
- with open(image_path, 'rb') as f:
- files = {'file': f}
- response = requests.post(url, files=files)
- return response.json()
- def show_info(self, id):
- url = f"{self.base_url}/show"
- data = {'id': id}
- response = requests.post(url, json=data)
- return response.json()
- def history_info(self, id):
- url = f"{self.base_url}/history"
- data = {'id': id}
- response = requests.post(url, json=data)
- return response.json()
- # 使用示例
- if __name__ == "__main__":
- client = OCRClient("http://localhost:8000") # 替换为实际的服务器地址和端口
- # 检测条形码
- barcode_result = client.detect_barcode("path/to/image.jpg")
- print("条形码检测结果:", barcode_result)
- # 获取条形码信息
- barcode_info = client.get_barcode(image_path="path/to/image.jpg")
- print("条形码信息:", barcode_info)
- # 搜索信息
- search_params = {
- "barcode_type": "EAN13",
- "matio_id": "123456",
- "pageNum": 1,
- "pageSize": 10
- }
- search_result = client.search_info(search_params)
- print("搜索结果:", search_result)
- # 获取matio_id
- matio_id_result = client.get_matio_id("path/to/image.jpg")
- print("Matio ID 结果:", matio_id_result)
- # 显示信息
- show_result = client.show_info("1")
- print("显示信息:", show_result)
- # 历史信息
- history_result = client.history_info("1")
- print("历史信息:", history_result)
- # 使用图片路径调用get_barcode
- barcode_info_with_image = client.get_barcode(image_path="path/to/image.jpg")
- print("使用图片的条形码信息:", barcode_info_with_image)
- # 使用message调用get_barcode
- message = {
- 'barcode': '1C3JAA57000BL',
- 'image_origin_path': '/clt/ocr_workspace/tmp_images/origin_images_2024_09_29-16__43__55.png',
- 'upload_timestamp': '2024_09_29-16__43__55',
- 'barcode_type': '普通吊牌'
- }
- barcode_info_with_message = client.get_barcode(message=message)
- print("使用message的条形码信息:", barcode_info_with_message)
- # 同时使用图片和message调用get_barcode
- barcode_info_combined = client.get_barcode(
- image_path="path/to/image.jpg",
- message=message
- )
- print("同时使用图片和message的条形码信息:", barcode_info_combined)
|