import os
import base64
import urllib
import requests
import re
import pandas as pd
# 百度圖片識(shí)別API,填入自己申請(qǐng)的API_KEY和SECRET_KEY
API_KEY = "XXXXXXXXX"
SECRET_KEY = "XXXXXXXXX"
def extract_word(path):
url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=" + get_access_token()
image = get_file_content_as_base64(path, urlencoded=True)
payload = 'image=' + image
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
return(response)
def get_file_content_as_base64(path, urlencoded=False):
"""
獲取文件base64編碼
:param path: 文件路徑
:param urlencoded: 是否對(duì)結(jié)果進(jìn)行urlencoded
:return: base64編碼信息
"""
with open(path, "rb") as f:
content = base64.b64encode(f.read()).decode("utf8")
if urlencoded:
content = urllib.parse.quote_plus(content)
return content
def get_access_token():
"""
使用 AK,SK 生成鑒權(quán)簽名(Access Token)
:return: access_token,或是None(如果錯(cuò)誤)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
def transform_word(response):
for item in response["words_result"]:
item.pop("location", None)
data = response["words_result"]
result_data = {}
current_key = None
for item in data:
words = item['words']
for key_start in ('經(jīng)度', '緯度', '地址', '時(shí)間', '海拔', '天氣', '備注'):
if words.startswith(key_start):
current_key = key_start
result_data[current_key] = words[len(current_key)+1:] # Extract the value after the key and colon
break
else:
if current_key is not None:
result_data[current_key] += words
df = pd.DataFrame([result_data])
return(df)
def get_file_extension(file_path):
_, file_extension = os.path.splitext(file_path)
return file_extension.lower()
if __name__ == '__main__':
image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp']
# 遍歷文件夾中所有文件
folder_path = os.path.dirname(os.path.abspath(__file__))
info_pic = pd.DataFrame()
for root, _, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
extension = get_file_extension(file_path)
if extension in image_extensions:
try:
print('正在提取' + file_path + '...')
res = extract_word(file_path).json()
info_pic_temp = transform_word(res)
if info_pic_temp.shape[1] > 2:
info_pic_temp.insert(0, '文件路徑', file_path)
info_pic = info_pic.append(info_pic_temp, ignore_index=True)
except Exception as e:
pass
info_pic.to_csv('picture_information.csv', index=False, encoding='utf_8_sig')
print('提取完成')
元道經(jīng)緯相機(jī)照片文字提取2023-07-27
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 今天上午,縣水利的幾位同志專(zhuān)程前來(lái),在鎮(zhèn)中心小學(xué)會(huì)議室給我們開(kāi)了一個(gè)專(zhuān)題會(huì)。會(huì)后布置的任務(wù)中,有一項(xiàng)是下載并...
- ?? 前幾天,一位朋友說(shuō),通過(guò)一張圖片可以定位到你的位置且泄露個(gè)人信息,在此之前,我沒(méi)了解過(guò),對(duì)此很吃驚,于是這...
- 查看相冊(cè)照片列表:采用TZImagePickerController 多了序號(hào)選中,系統(tǒng)默認(rèn)的沒(méi)有個(gè)數(shù)統(tǒng)計(jì) 查看視...
- 1:打開(kāi)備忘錄長(zhǎng)按底部的+,一定記得是長(zhǎng)按。 2:點(diǎn)擊工具欄右側(cè)的虛線方框,再點(diǎn)擊“本地相冊(cè)”。 3:選中圖片之后...
- 快到期末考試了,這兩天忙著出試卷。 去年和前年的語(yǔ)文期末試卷只有紙質(zhì)的,沒(méi)有電子稿。我把一些題目打出來(lái)變成電子稿。...