之前的随笔中,已经通过相机或相册获取到了我们想要的图片,接下来进行识图api的配置工作。我使用的是百度的api,利用python获取信息,并在MainActivity中进行调用来输出信息。
一、首先我们需要申请创建一个应用(管理控制台->产品服务->图像识别),百度智能云,得到api key和secret key
利用百度api进行识图的python代码,因为我们一会需要在as中调用函数,所以不需要写主函数了
# coding=utf-8
import sys
import json
import base64
IS_PY3 = sys.version_info.major == 3
if IS_PY3:
from urllib.request import urlopen
from urllib.request import Request
from urllib.error import URLError
from urllib.parse import urlencode
from urllib.parse import quote_plus
else:
import urllib2
from urllib import quote_plus
from urllib2 import urlopen
from urllib2 import Request
from urllib2 import URLError
from urllib import urlencode
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
API_KEY = '你的API Key'
SECRET_KEY = '你的Secret Key'
IMAGE_RECOGNIZE_URL = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
""" TOKEN start """
TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token'
"""
获取token
"""
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
post_data = urlencode(params)
if (IS_PY3):
post_data = post_data.encode('utf-8')
req = Request(TOKEN_URL, post_data)
try:
f = urlopen(req, timeout=5)
result_str = f.read()
except URLError as err:
print(err)
if (IS_PY3):
result_str = result_str.decode()
result = json.loads(result\_str)
if ('access\_token' in result.keys() and 'scope' in result.keys()):
if not 'brain\_all\_scope' in result\['scope'\].split(' '):
print ('please ensure has check the ability')
exit()
return result\['access\_token'\]
else:
print ('please overwrite the correct API\_KEY and SECRET\_KEY')
exit()
"""
读取文件
"""
def read_file(image_path):
f = None
try:
f = open(image_path, 'rb')
return f.read()
except:
print('read image file fail')
return None
finally:
if f:
f.close()
"""
调用远程服务
"""
def request(url, data):
req = Request(url, data.encode('utf-8'))
has_error = False
try:
f = urlopen(req)
result_str = f.read()
if (IS_PY3):
result_str = result_str.decode()
return result_str
except URLError as err:
print(err)
"""
调用识别接口并打印结果
"""
def print_result(filename,url):
# 获取图片
file_content = read_file(filename)
response = request(url, urlencode(
{
'image': base64.b64encode(file\_content),
'top\_num': 1
}))
result\_json = json.loads(response)
#返回识别结果中得分最高的"keyword"
for data in result\_json\["result"\]:
return data\["keyword"\]
#返回结果示例
二、将这个python代码插入到as中,需要用到chaquopy,具体配置教程
三、在MainActivity中调用python函数
// 初始化Python环境
void initPython(){
if (! Python.isStarted()) {
Python.start(new AndroidPlatform(this));
}
}
mBtnFinish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTvPath.getText().toString().equals("图片路径:")){
// 如果一张图片都没选
ToastUtil.showMsg(getApplicationContext(), "您还没有选择图片呢!");
}else{
Python py = Python.getInstance();
PyObject token = py.getModule("hello").callAttr("fetch\_token");//获取token
String token2 = token.toJava(String.class);//将获取的token转换为Java中的string类型
String url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced\_general?access\_token=" + token2;//拼接url
PyObject pyObject = py.getModule("hello").callAttr("print\_result",mTvPath.getText(),url);//调用python函数
String returnString = pyObject.toString();
System.out.println(returnString);
mTvResult.setText("识别成功!结果为:"+returnString);
}
}
});
遇到的问题:
点击拍照后,照片不会更新,但是并不影响识别结果
原因分析:在安卓7.0以上,使用了共享文件的形式,每次拍照使用了相同的路径,旧图片会被新图片顶替掉,于是在
Glide.with(MainActivity.this).load(outputUri).into(mIvPic);//使用Glide进行图片展示
的时候,glide使用了相同的uri,导致imageview没有刷新图片,但是并不影响使用python函数进行图片识别的结果,因为uri是正确的。
暂时还没有解决
手机扫一扫
移动阅读更方便
你可能感兴趣的文章