背景描述:
在对监控录像进行分析时,希望直接获取到视频上面的时间信息,以视频上的时间戳对关心的帧命名等,本文通过调用百度文字识别API获取到图片上的文字信息(主要是时间+地点)。
主要步骤:
1. 前往百度API中心申请自己的Api Key 和 Secret Key;
2. 获取自己的 Access token;
3. 调用API进行文字识别,目前通用文字识别可免费试用,每天限调用5W次。
获取Access token
import base64
import requests
API_KEY = "FuwbiOjQ2UkOuDZhsj****" # 自己去申请
SECRET_KEY = "xhq07lmabjNvQ028rCGpjRDFbitF****" # 自己去申请
API_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
def get_access_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&' \
'client_id=FuwbiOjQ2UkOuDZhsj****&client_secret=xhq07lmabjNvQ028rCGpjRDFbitF***'
# 将client_id 和 client_secret替换自己的值
headers = {'Content-Type': 'application/json; charset=UTF-8'}
res = requests.get(url=host, headers=headers).json()
access_token = res["access_token"]
print(access_token)
return access_token
调用API获得结果
"""
利用百度的OCR API 提取视频画面上的时间
"""
import base64
import requests
API_KEY = "FuwbiOjQ2UkOuDZhsjg***"
SECRET_KEY = "xhq07lmabjNvQ028rCGpjRDFbit**"
API_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
def get_access_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&' \
'client_id=FuwbiOjQ2UkOuDZhsjg***&client_secret=xhq07lmabjNvQ028rCGpjRDFbi**'
headers = {'Content-Type': 'application/json; charset=UTF-8'}
res = requests.get(url=host, headers=headers).json()
access_token = res["access_token"]
print(access_token)
return access_token
def text_extract(img_path, access_token):
data = {}
data["access_token"] = access_token
data["image"] = base64.b64encode(open(img_path, "rb").read())
headers = {"Content-Type": "application/x-www-form-urlencoded"}
res = requests.post(url=API_URL, headers=headers, data=data)
result = res.json()
img_show_time = result["words_result"][0]["words"]
return img_show_time
if __name__ == "__main__":
img_path = "./4.jpg"
access_token = get_access_token()
time_on_image = text_extract(img_path=img_path, access_token=access_token)
结果展示
# 返回结果
{'log_id': 7056746947134263375, 'words_result_num': 2,
'words_result': [{'words': '2019-06-1309:39:35'},
{'words': '****墩处桥面灯杆5m处)'}]}