身份证OCR识别,支持矫正及头像提取

张开发
2026/4/16 9:40:40 15 分钟阅读

分享文章

身份证OCR识别,支持矫正及头像提取
接到客户需求传入身份证的照片要得到矫正后的证件照片以及证件上的头像。石榴智能的身份证ocr接口不仅提供文字识别功能还能返回上面的信息。下面介绍如何调用该API。1获得appcode在石榴智能API网站上注册可以得到appcode在身份证ocr产品可以获得免费积分。2调用API以下是python调用api并展示矫正的证件照片和头像的代码# API文档https://market.shiliuai.com/doc/id-card-ocr # -*- coding: utf8 -*- import requests import base64 import json import numpy as np import cv2 URL https://ocr-api.shiliuai.com/api/id_card_ocr/v2 APPCODE 你的appcode HEADERS { Authorization: APPCODE %s % APPCODE, Content-Type: application/json } def get_base64(file_path): with open(file_path, rb) as fp: b64 base64.b64encode(fp.read()).decode(utf8) return b64 def post_api(data, name): print(test name) response requests.post(urlURL, headersHEADERS, jsondata) response json.loads(response.content) for k in response.keys(): if k ! data: print(k, response[k]) else: print(k) d response[data] for kk in d.keys(): if base64 in kk: print( , kk, d[kk][:100], ......) rectified_base64 d.get(kk) file_bytes base64.b64decode(rectified_base64) rectified np.asarray(bytearray(file_bytes), dtypenp.uint8) rectified cv2.imdecode(rectified, cv2.IMREAD_UNCHANGED) cv2.imshow(kk, rectified) else: print( , kk, d[kk]) cv2.waitKey(0) print(\n ) # 默认参数 def post_default(): f ../data/1.jpg b64 get_base64(f) data { image_base64: b64 } post_api(data, default) # 返回矫正证件 def post_return_card(): f ../data/1.jpg b64 get_base64(f) data { image_base64: b64, return_rectified_card: True } post_api(data, rectify) # 设置矫正证件的边距 def post_return_card_margin(): f ../data/1.jpg b64 get_base64(f) data { image_base64: b64, return_rectified_card: True, card_margin_ratio: 0.1 } post_api(data, rectify_margin) # 设置矫正证件的尺寸 def post_return_card_resize(): f ../data/1.jpg b64 get_base64(f) data { image_base64: b64, return_rectified_card: True, card_width: 420, card_height: 270 } post_api(data, rectify_resize) # 设置返回头像 def post_return_head(): f ../data/1.jpg b64 get_base64(f) data { image_base64: b64, return_rectified_head: True, head_width: 358 } post_api(data, head) if __name__ __main__: post_default() post_return_card() post_return_card_margin() post_return_card_resize() post_return_head()

更多文章