百度AI攻略:货币识别
1.需求及方案:
近兩年用外幣進行詐騙的案件很多。例如:2015年12月,一安徽詐騙團伙,用不值1角人民幣的50印蒂(intis,秘魯舊貨幣,1991年發行新貨幣后已停止流通,目前無貨幣價值,僅有"收藏"價值),在豫皖魯蘇四省行騙,卻屢屢得手。該團伙今年以來已詐騙20多起,至12月8日,警方已查明落實了6起,騙款達20多萬元。
同時隨著我國經濟的發展,出國旅游的人越來越多,在兌換貨幣的時候也經常有國人被騙的消息。
能否開發一款方便的應用,借AI的力量,給大家一雙慧眼呢?正好現在百度推出了貨幣識別API(百度貨幣識別,識別圖像中的貨幣類型,返回貨幣名稱、代碼、面值、年份信息,可識別百余種國內外常見貨幣。),我在這個基礎上集成其他API,開發了這個貨幣估值及防詐騙的應用。
以下為具體方案:
2.平臺接入
貨幣識別接入網址:https://ai.baidu.com/tech/imagerecognition/currency?現在需要進行邀測申請。
具體接入方式比較簡單,可以參考我的另一個帖子,這里就不重復了:
http://ai.baidu.com/forum/topic/show/943327
3.貨幣識別調用攻略(Python3)
3.1首先認證授權:
在開始調用任何API之前需要先進行認證授權,具體的說明請參考:
http://ai.baidu.com/docs#/Auth/top
具體Python3代碼如下:
# -*- coding: utf-8 -*- #!/usr/bin/env pythonimport urllib import base64 import json #client_id 為官網獲取的AK, client_secret 為官網獲取的SK client_id =【百度云應用的AK】 client_secret =【百度云應用的SK】#獲取token def get_token():host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secretrequest = urllib.request.Request(host)request.add_header('Content-Type', 'application/json; charset=UTF-8')response = urllib.request.urlopen(request)token_content = response.read()if token_content:token_info = json.loads(token_content)token_key = token_info['access_token']return token_key3.2 接口調用:
詳細說明請參考:https://ai.baidu.com/docs#/ImageClassify-API/f3c9e21c
接口描述
識別圖像中的貨幣類型,以紙幣為主,正反面均可準確識別,接口返回貨幣的名稱、代碼、面值、年份信息;可識別各類近代常見貨幣,如美元、歐元、英鎊、法郎、澳大利亞元、俄羅斯盧布、日元、韓元、泰銖、印尼盧比等。
說明的比較清晰,這里就不重復了。
大家需要注意的是:
API訪問URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/currency
圖片格式:Base64編碼字符串,以圖片文件形式請求時必填。(支持圖片格式:jpg,bmp,png),圖片大小不超過4M。最短邊至少15px,最長邊最大4096px。 注意:圖片的base64編碼是不包含圖片頭的
Python3調用代碼如下:
#貨幣識別 #filename:圖片名(本地存儲包括路徑) def currency(filename):request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/currency"# 二進制方式打開圖片文件f = open(filename, 'rb')img = base64.b64encode(f.read())params = dict()params['image'] = imgparams['show'] = 'true'params = urllib.parse.urlencode(params).encode("utf-8")#params = json.dumps(params).encode('utf-8')access_token = get_token()request_url = request_url + "?access_token=" + access_tokenrequest = urllib.request.Request(url=request_url, data=params)request.add_header('Content-Type', 'application/x-www-form-urlencoded')response = urllib.request.urlopen(request)content = response.read()if content:#print(content)content=content.decode('utf-8')#print(content)data = json.loads(content)#print(data)result=data['result']print ('貨幣名稱:',result['currencyName'])print ('貨幣代碼:',result['currencyCode'])print ('貨幣年份:',result['year'])print ('貨幣面值:',result['currencyDenomination'])return result['currencyCode'],float(result['currencyDenomination']),result['year']4.應用方案:
4.1 整體方案:
首先調用貨幣識別API對貨幣圖片進行識別。
然后對貨幣年份及該國貨幣情況對比,判斷是否為廢幣。如果為廢幣,提出警告。如果是流通貨幣調用匯率API,給出對應的人民幣價值。
4.2 具體實現代碼:
# -*- coding: utf-8 -*- #!/usr/bin/env pythonimport urllib import base64 import json #client_id 為官網獲取的AK, client_secret 為官網獲取的SK client_id =【百度云應用的AK】 client_secret =【百度云應用的SK】#獲取token def get_token():host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secretrequest = urllib.request.Request(host)request.add_header('Content-Type', 'application/json; charset=UTF-8')response = urllib.request.urlopen(request)token_content = response.read()if token_content:token_info = json.loads(token_content)token_key = token_info['access_token']return token_key#貨幣識別 #filename:圖片名(本地存儲包括路徑) def currency(filename):request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/currency"# 二進制方式打開圖片文件f = open(filename, 'rb')img = base64.b64encode(f.read())params = dict()params['image'] = imgparams['show'] = 'true'params = urllib.parse.urlencode(params).encode("utf-8")#params = json.dumps(params).encode('utf-8')access_token = get_token()request_url = request_url + "?access_token=" + access_tokenrequest = urllib.request.Request(url=request_url, data=params)request.add_header('Content-Type', 'application/x-www-form-urlencoded')response = urllib.request.urlopen(request)content = response.read()if content:#print(content)content=content.decode('utf-8')#print(content)data = json.loads(content)#print(data)result=data['result']print ('貨幣名稱:',result['currencyName'])print ('貨幣代碼:',result['currencyCode'])print ('貨幣年份:',result['year'])print ('貨幣面值:',result['currencyDenomination'])return result['currencyCode'],float(result['currencyDenomination']),result['year']import re #調用匯率API提取匯率 def get_currency_rate(fromcurrency,tocurrency):fp = urllib.request.urlopen('http://webforex.hermes.hexun.com/forex/quotelist?code=FOREX'+fromcurrency+tocurrency+',&column=code,LastClose,UpdownRate&callback=ongetjsonpforex&_=1451543515359')html = fp.read().decode("utf-8")#print(html)fp.close()s = re.findall("\((.*)\)",str(html))[0]sjson = json.loads(s)rate = sjson["Data"][0][0][1]/10000#print (rate)return rate#貨幣是否流通判斷及對應人民幣 def currency_value(filename):code,value,year=currency(filename)#對于舊秘魯幣等停止兌換的貨幣進行判斷#目前只寫了秘魯幣的邏輯,后續需要不斷的加入其他廢幣的信息if code=='PEN':if year<='1991年':print ('已經作廢不可兌換,謹防詐騙!')returnif code=='DEI':print ('已經作廢不可兌換,謹防詐騙!')returnrate=get_currency_rate(code,'CNY')result=round(value*rate,3)print ('匯率(昨收盤):',rate)print ('對應人民幣(昨收盤):',result)return resultcurrency_value('bilu1000.jpg')5 效果測試
5.1 防詐騙
“大娘,您看這個秘魯幣老值錢了”。“大娘別信他,拿來讓我看看”:
貨幣名稱: 秘魯新索爾
貨幣代碼: PEN
貨幣年份: 1986年
貨幣面值: 1000
已經作廢不可兌換,謹防詐騙!
“大娘,快報警!”
再來個面值更高的:
貨幣名稱: 秘魯印蒂
貨幣代碼: DEI
貨幣年份: 1988年
貨幣面值: 5000
已經作廢不可兌換,謹防詐騙!
?
5.2 正常貨幣確認價值
貨幣名稱: 澳大利亞元
貨幣代碼: AUD
貨幣年份: -
貨幣面值: 100
匯率: 4.7503
對應人民幣: 475.03
貨幣名稱: 美元
貨幣代碼: USD
貨幣年份: 1993年
貨幣面值: 50
匯率: 6.7332
對應人民幣: 336.66
總結
以上是生活随笔為你收集整理的百度AI攻略:货币识别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最全的货币代码中文翻译.
- 下一篇: SAP FI 系列 (022) - 货币