Created
September 3, 2017 17:46
-
-
Save sansuke05/7d873c2c31b9ef7e3dc2116795619f64 to your computer and use it in GitHub Desktop.
AndroidからEmotion APIを使ってみる ref: http://qiita.com/sansuke05/items/d6262cae0a2a65ca6f93
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ConnectToEmotionAPI extends AsyncTask<Void, Void, JSONObject> { | |
| @Override | |
| protected void onPreExecute() { | |
| } | |
| @Override | |
| protected JSONObject doInBackground(Void... params) { | |
| } | |
| @Override | |
| protected void onPostExecute(JSONObject result) { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HttpURLConnection con = null; | |
| URL url = null; | |
| String urlStr = "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize"; | |
| String key = "{Your Key}"; //Subscription Key | |
| DataOutputStream os = null; | |
| BufferedReader reader; | |
| JSONObject json = null; | |
| try { | |
| url = new URL(urlStr); | |
| con = (HttpURLConnection)url.openConnection(); | |
| con.setReadTimeout(10000); | |
| con.setConnectTimeout(20000); | |
| con.setRequestMethod("POST"); | |
| con.setDoInput(true); | |
| con.setDoOutput(true); | |
| //リクエストヘッダーの設定 | |
| con.setRequestProperty("Content-Type", "application/octet-stream"); | |
| con.setRequestProperty("Ocp-Apim-Subscription-Key", key); | |
| // リクエストボディの作成 | |
| Resources r = main_.getResources(); | |
| Bitmap bmp = BitmapFactory.decodeResource(r, R.drawable.face_small); | |
| // 画像をバイナリデータに変換 | |
| byte[] byteArray; | |
| ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
| bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos); | |
| byteArray = bos.toByteArray(); | |
| os = new DataOutputStream(con.getOutputStream()); | |
| for(int i = 0 ; i < byteArray.length;i++){ | |
| os.writeByte(byteArray[i]); | |
| } | |
| // APIに接続 | |
| con.connect(); | |
| os.close(); | |
| int status = con.getResponseCode(); | |
| switch (status) { | |
| case HttpURLConnection.HTTP_OK: | |
| InputStream in = con.getInputStream(); | |
| reader = new BufferedReader(new InputStreamReader(in)); | |
| String line; | |
| String readStr = new String(); | |
| while (null != (line = reader.readLine())){ | |
| readStr += line; | |
| } | |
| Log.d("EmotionAPI","read string: " + readStr); | |
| in.close(); | |
| json = new JSONArray(readStr).getJSONObject(0); | |
| break; | |
| case HttpURLConnection.HTTP_UNAUTHORIZED: | |
| break; | |
| default: | |
| break; | |
| } | |
| } catch (MalformedURLException e){ | |
| e.printStackTrace(); | |
| } catch (JSONException e){ | |
| e.printStackTrace(); | |
| } catch (IOException e){ | |
| e.printStackTrace(); | |
| } | |
| return json; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Override | |
| protected void onPostExecute(JSONObject result) { | |
| super.onPostExecute(result); | |
| JSONObject jsonData; | |
| String[] str = new String[2]; | |
| try { | |
| jsonData = result.getJSONObject("scores"); | |
| str[0] = jsonData.getString("happiness"); | |
| str[1] = jsonData.getString("anger"); | |
| } catch (Exception e){ | |
| e.printStackTrace(); | |
| } | |
| if (isSmile(str[0])) { | |
| Log.d("EmotionAPI","素敵な笑顔です!"); | |
| } else if (isAnger(str[1])) { | |
| Log.d("EmotionAPI","そんなに怒らないでくださいよ~"); | |
| } else { | |
| Log.d("EmotionAPI","つまんないです。何かリアクションしてください"); | |
| } | |
| } | |
| public boolean isSmile(String strValue){ | |
| double value = Double.parseDouble(strValue); | |
| if (value > 0.5) return true; | |
| else return false; | |
| } | |
| public boolean isAnger(String strValue){ | |
| double value = Double.parseDouble(strValue); | |
| if (value > 0.5) return true; | |
| else return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment