seal_test.py 1020 B

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. import base64
  3. import requests
  4. import json
  5. import time
  6. # img dir
  7. image_dir = "/home/stf/miner_pdf/test_img"
  8. # access token
  9. access_token = "24.6bbe9987c6bd19ba65e4402917811657.2592000.1724573148.282335-86574608"
  10. # request url
  11. request_url = f"https://aip.baidubce.com/rest/2.0/ocr/v1/seal?access_token={access_token}"
  12. # headers
  13. headers = {'content-type': 'application/x-www-form-urlencoded'}
  14. # seal save path
  15. seal_save_path = "/home/stf/miner_pdf/seal.json"
  16. data = {}
  17. # 图片文件
  18. for img_name in os.listdir(image_dir):
  19. img_path = os.path.join(image_dir, img_name)
  20. print(f'processing {img_path} ...')
  21. f = open(img_path, 'rb')
  22. img = base64.b64encode(f.read())
  23. params = {"image":img}
  24. #印章检测
  25. response = requests.post(request_url, data=params, headers=headers)
  26. if response:
  27. print(response.json())
  28. data[img_name] = response.json()
  29. time.sleep(3)
  30. with open(seal_save_path, 'w', encoding='utf-8') as f:
  31. json.dump(data, f, ensure_ascii=False, indent=4)