Face++を使用したAPIインタフェース-顔認識

245991 ワード

本住所:http://blog.csdn.net/tiandixuanwuliang/article/details/78018687
ここではface++のAPIインタフェースを用いて顔認識を実現する方法について説明する.
一、face++のAPIサポートを取得する
1.1 face++公式サイト:https://www.faceplusplus.com.cn/
1.2 face++公式サイトにアカウントを登録し、開発者の資料を記入する.以下の図である.
使用face++的API接口-人脸识别_第1张图片
二、API Keyの作成とBundle IDのバインド
2.1下図のような位置で、API Keyをクリックする


使用face++的API接口-人脸识别_第2张图片
2.2 APIキーの作成をクリックし、必要に応じて記入する(試用選択):
使用face++的API接口-人脸识别_第3张图片
2.3アプリケーションのバインド:
使用face++的API接口-人脸识别_第4张图片
これで、私たちの準備はできました.API KeyとAPI Secretを使用します.

三、Androidプロジェクトの作成
3.1 Androidプロジェクトを設立する.本稿ではFragment+Activityモードを用い,Activityコードは以下のように簡単なfragmentである.

     
     
     
     

     
     
     
     
  1. package com.wllfengshu.boyandgirl;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.FragmentManager;
  5. import android.app.FragmentTransaction;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. public class MainActivity extends Activity {
  9. private FragmentManager fm;
  10. private FragmentTransaction transaction;
  11. @SuppressLint( "NewApi")
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. fm = getFragmentManager();
  17. transaction = fm.beginTransaction();
  18. transaction.replace(R.id.ll_fregment, new FaceFragment());
  19. transaction.commit();
  20. }
  21. public void change(View v) {
  22. transaction = fm.beginTransaction();
  23. switch (v.getId()) {
  24. case R.id.ib_main_face:
  25. transaction.replace(R.id.ll_fregment, new FaceFragment());
  26. break;
  27. case R.id.ib_main_gesture:
  28. transaction.replace(R.id.ll_fregment, new GestureFragment());
  29. break;
  30. case R.id.ib_main_other:
  31. transaction.replace(R.id.ll_fregment, new OtherFragment());
  32. break;
  33. }
  34. transaction.commit();
  35. }
  36. }
 
    3.2fragment    :(              ) 
    


     
     
     
     
  1. package com.wllfengshu.boyandgirl;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8. import android.annotation.SuppressLint;
  9. import android.app.Fragment;
  10. import android.content.Intent;
  11. import android.graphics.Bitmap;
  12. import android.graphics.BitmapFactory;
  13. import android.graphics.Paint;
  14. import android.os.Bundle;
  15. import android.os.Handler;
  16. import android.os.Message;
  17. import android.view.LayoutInflater;
  18. import android.view.View;
  19. import android.view.View.OnClickListener;
  20. import android.view.ViewGroup;
  21. import android.widget.Button;
  22. import android.widget.ImageView;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25. import com.wllfengshu.util.Constant;
  26. import com.wllfengshu.util.DrawUtil;
  27. import com.wllfengshu.util.GifView;
  28. import com.wllfengshu.util.HttpUtils;
  29. import com.wllfengshu.util.ImageUtil;
  30. @SuppressLint({ "NewApi", "HandlerLeak" })
  31. public class FaceFragment extends Fragment implements OnClickListener {
  32. private ImageView iv_face; // �
  33. private Button ib_face_enter; //
  34. private Button ib_face_choice; // )
  35. private TextView tv_face_gender;
  36. private TextView tv_face_age;
  37. private TextView tv_face_beauty;
  38. private Bitmap scalingPhoto; //
  39. private String gender; //
  40. private int age; //
  41. private int beauty; //
  42. private Paint paint; //
  43. private View view;
  44. private GifView gif;
  45. @Override
  46. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  47. Bundle savedInstanceState) {
  48. view = inflater.inflate(R.layout.fragment_face, container, false);
  49. iv_face = (ImageView) view.findViewById(R.id.iv_face);
  50. ib_face_enter = (Button) view.findViewById(R.id.ib_face_enter);
  51. ib_face_choice = (Button) view.findViewById(R.id.ib_face_choice);
  52. tv_face_gender = (TextView) view.findViewById(R.id.tv_face_gender);
  53. tv_face_age = (TextView) view.findViewById(R.id.tv_face_age);
  54. tv_face_beauty = (TextView) view.findViewById(R.id.tv_face_beauty);
  55. gif = (GifView) view.findViewById(R.id.gif);
  56. ib_face_enter.setOnClickListener( this);
  57. ib_face_choice.setOnClickListener( this);
  58. paint = new Paint(); //
  59. scalingPhoto = BitmapFactory.decodeResource( this.getResources(),
  60. R.drawable.defualt);
  61. return view;
  62. }
  63. @SuppressLint( "HandlerLeak")
  64. private Handler handler = new Handler() {
  65. @Override
  66. public void handleMessage(Message msg) {
  67. String str = (String) msg.obj;
  68. System.out.println( "*******face:" + str);
  69. if (str.equals( "403") || str.equals( "400") || str.equals( "413")
  70. || str.equals( "500")) {
  71. Toast.makeText(getActivity(), "Please Try Again",
  72. Toast.LENGTH_SHORT).show();
  73. } else {
  74. try {
  75. JSONObject resultJSON = new JSONObject(str);
  76. System.out.println(resultJSON.getString( "faces") + "=====");
  77. if (resultJSON.getString( "faces").equals( "[]")) {
  78. Toast.makeText(getActivity(),
  79. "There is no face picture", Toast.LENGTH_SHORT)
  80. .show();
  81. } else {
  82. @SuppressWarnings( "rawtypes")
  83. List res = DrawUtil.FacePrepareBitmap(resultJSON,
  84. scalingPhoto, paint, iv_face);
  85. gender = (String) res.get( 0);
  86. age = (Integer) res.get( 1);
  87. beauty = (Integer) res.get( 2);
  88. System.out.println( "------------" + gender + " " + age
  89. + " " + " " + beauty);
  90. tv_face_gender.setText( " :"
  91. + ImageUtil.getFaceGender(gender));
  92. tv_face_age.setText( " :" + age);
  93. tv_face_beauty.setText( " :" + beauty);
  94. }
  95. } catch (JSONException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. gif.setVisibility(View.GONE); // gif
  100. };
  101. };
  102. @Override
  103. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  104. if (requestCode == 1) {
  105. //  ata
  106. if (data != null) {
  107. //
  108. String photoPath = ImageUtil.getPhotoPath(getActivity(), data);
  109. //
  110. scalingPhoto = ImageUtil.getScalingPhoto(photoPath);
  111. //
  112. iv_face.setImageBitmap(scalingPhoto);
  113. }
  114. }
  115. super.onActivityResult(requestCode, resultCode, data);
  116. }
  117. @Override
  118. public void onClick(View v) {
  119. switch (v.getId()) {
  120. case R.id.ib_face_choice:
  121. //
  122. Intent intent = new Intent();
  123. intent.setAction(Intent.ACTION_PICK);
  124. intent.setType( "image/*");
  125. startActivityForResult(intent, 1); //  �
  126. break;
  127. case R.id.ib_face_enter:
  128. //
  129. gif.setVisibility(View.VISIBLE);
  130. gif.setMovieResource(R.raw.red); // gif
  131. String base64ImageEncode = ImageUtil
  132. .getBase64ImageEncode(scalingPhoto);
  133. System.out.println(base64ImageEncode);
  134. // �
  135. final Map map = new HashMap();
  136. map.put( "api_key", Constant.API_KEY);
  137. map.put( "api_secret", Constant.API_SECRET);
  138. map.put( "return_attributes", "gender,age,beauty");
  139. map.put( "image_base64", base64ImageEncode);
  140. //
  141. new Thread( new Runnable() {
  142. @Override
  143. public void run() {
  144. try {
  145. String result = HttpUtils
  146. .post(Constant.URL_DETECT, map);
  147. Message message = new Message();
  148. message.obj = result;
  149. handler.sendMessage(message);
  150. } catch (IOException e) {
  151. e.printStackTrace();
  152. }
  153. }
  154. }).start();
  155. break;
  156. }
  157. }
  158. }
:fragment , “ ”, “ ”。 , POST face++ , 。
POST :


     
     
     
     
  1. public static String post(String url, Map args) throws IOException {
  2. URL host = new URL(url);
  3. HttpURLConnection connection = HttpURLConnection.class.cast(host.openConnection());
  4. connection.setRequestMethod( "POST");
  5. connection.setDoOutput( true);
  6. connection.setDoInput( true);
  7. connection.setUseCaches( false);
  8. connection.setRequestProperty( "Connection", "Keep-Alive");
  9. connection.setRequestProperty( "Charsert", "UTF-8");
  10. DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
  11. if (args != null) {
  12. for (Entry entry : args.entrySet()) {
  13. String key = entry.getKey();
  14. Object value = entry.getValue();
  15. if (value instanceof File) {
  16. value = new FileInputStream(File.class.cast(value));
  17. }
  18. if (value instanceof InputStream) {
  19. dos.write((key + "=").getBytes());
  20. InputStream is = InputStream.class.cast(value);
  21. byte[] data = new byte[is.available()];
  22. is.read(data);
  23. dos.write(URLEncoder.encode(Base64.encodeToString(data, data.length), "UTF-8").getBytes());
  24. is.close();
  25. } else {
  26. dos.write((key + "=" + URLEncoder.encode(String.valueOf(value), "UTF-8")).getBytes());
  27. }
  28. dos.write( "&".getBytes());
  29. }
  30. }
  31. dos.flush();
  32. dos.close();
  33. int resultCode = connection.getResponseCode();
  34. StringBuilder response = new StringBuilder();
  35. if (resultCode == HttpURLConnection.HTTP_OK) {
  36. BufferedReader br = new BufferedReader( new InputStreamReader(connection.getInputStream()));
  37. String line;
  38. while ((line = br.readLine()) != null) {
  39. response.append(line);
  40. }
  41. br.close();
  42. } else {
  43. response.append(resultCode);
  44. }
  45. return response.toString();
  46. }
base64 , :


     
     
     
     
  1. public static String getBase64ImageEncode(Bitmap myImage) {
  2. Bitmap bmSmall = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(), myImage.getHeight());
  3. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  4. bmSmall.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  5. byte[] arrays = stream.toByteArray();
  6. // base64 encode
  7. byte[] encode = Base64.encode(arrays, Base64.DEFAULT);
  8. String base64Encode = new String(encode);
  9. return base64Encode;
  10. }
        (activity_main.xml  ):
     
     
     
     

     
     
     
     

     
     
     
     
  1. "1.0" encoding= "utf-8"?>
  2. "http://schemas.android.com/apk/res/android"
  3. android:layout_width= "match_parent"
  4. android:layout_height= "match_parent"
  5. android:background= "@drawable/bg" >
  6. android:id= "@+id/ll_fregment"
  7. android:layout_width= "match_parent"
  8. android:layout_height= "match_parent"
  9. android:orientation= "vertical" >
  10. android:layout_width= "match_parent"
  11. android:layout_height= "wrap_content"
  12. android:layout_alignParentBottom= "true"
  13. android:orientation= "horizontal" >
  14. android:layout_width= "match_parent"
  15. android:layout_height= "50dp"
  16. android:orientation= "horizontal" >
  17. android:id= "@+id/ib_main_face"
  18. android:layout_width= "match_parent"
  19. android:layout_height= "match_parent"
  20. android:layout_weight= "1"
  21. android:background= "@drawable/rb_select"
  22. android:button= "@null"
  23. android:checked= "true"
  24. android:gravity= "center"
  25. android:onClick= "change"
  26. android:text= " " />
  27. android:id= "@+id/ib_main_gesture"
  28. android:layout_width= "match_parent"
  29. android:layout_height= "match_parent"
  30. android:layout_weight= "1"
  31. android:background= "@drawable/rb_select"
  32. android:button= "@null"
  33. android:gravity= "center"
  34. android:onClick= "change"
  35. android:text= " " />
  36. android:id= "@+id/ib_main_other"
  37. android:layout_width= "match_parent"
  38. android:layout_height= "match_parent"
  39. android:layout_weight= "1"
  40. android:background= "@drawable/rb_select"
  41. android:button= "@null"
  42. android:gravity= "center"
  43. android:onClick= "change"
  44. android:text= " " />

  45. 
         
         
         
         
    1. "1.0" encoding= "utf-8"?>
    2. "http://schemas.android.com/apk/res/android"
    3. android:layout_width= "match_parent"
    4. android:layout_height= "match_parent" >
    5. android:layout_width= "match_parent"
    6. android:layout_height= "wrap_content"
    7. android:gravity= "center"
    8. android:text= " "
    9. android:textSize= "20sp" />
    10. android:id= "@+id/iv_face"
    11. android:layout_width= "200dp"
    12. android:layout_height= "300dp"
    13. android:layout_alignParentTop= "true"
    14. android:layout_centerHorizontal= "true"
    15. android:contentDescription= " "
    16. android:paddingTop= "40dp"
    17. android:src= "@drawable/defualt" />
    18. android:id= "@+id/ll_button"
    19. android:layout_width= "match_parent"
    20. android:layout_height= "wrap_content"
    21. android:layout_below= "@id/iv_face"
    22. android:layout_marginLeft= "10dp"
    23. android:layout_marginRight= "10dp"
    24. android:gravity= "center_horizontal"
    25. android:orientation= "horizontal" >
    26. android:id= "@+id/ib_face_enter"
    27. android:layout_width= "wrap_content"
    28. android:layout_height= "wrap_content"
    29. android:background= "@drawable/enter"
    30. android:text= " " />
    31. android:id= "@+id/ib_face_choice"
    32. android:layout_width= "wrap_content"
    33. android:layout_height= "wrap_content"
    34. android:background= "@drawable/choose"
    35. android:text= " " />
    36. android:layout_width= "match_parent"
    37. android:layout_height= "wrap_content"
    38. android:layout_alignParentBottom= "true"
    39. android:layout_below= "@id/ll_button"
    40. android:orientation= "horizontal"
    41. android:paddingTop= "20dp" >
    42. android:id= "@+id/tv_face_gender"
    43. android:layout_width= "wrap_content"
    44. android:layout_height= "wrap_content"
    45. android:layout_weight= "1"
    46. android:gravity= "center"
    47. android:src= "@drawable/ic_launcher"
    48. android:text= " "
    49. android:textSize= "20sp" />
    50. android:id= "@+id/tv_face_age"
    51. android:layout_width= "wrap_content"
    52. android:layout_height= "wrap_content"
    53. android:layout_weight= "1"
    54. android:gravity= "center"
    55. android:src= "@drawable/ic_launcher"
    56. android:text= " "
    57. android:textSize= "20sp" />
    58. android:id= "@+id/tv_face_beauty"
    59. android:layout_width= "wrap_content"
    60. android:layout_height= "wrap_content"
    61. android:layout_weight= "1"
    62. android:gravity= "center"
    63. android:src= "@drawable/ic_launcher"
    64. android:text= " "
    65. android:textSize= "20sp" />
    66. android:id= "@+id/gif"
    67. android:layout_width= "200dp"
    68. android:layout_height= "200dp"
    69. android:layout_centerHorizontal= "true"
    70. android:layout_gravity= "center_horizontal"
    71. android:layout_marginTop= "60dp"
    72. android:enabled= "false" />

    73. , , :
      :http://download.csdn.net/download/tiandixuanwuliang/9984027

があるかもしれません
  • Java
    lijingyao8206
    Java ThreadPool java
  • java
    ユリはお じゃない
    java
  • [ ]
    comsci


  • を びて ぶ.

  • Oracle
    IT
    oracle  
  • oracle
    chu
    oracle
  • python ( )
    みかん
    pthon
  • svn
    aichenglong
    SVN
  • アルファベットで :
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    -
    -
    -
    Sitemap -

    すべてのIT ベースCopyRight© 2000-2050 IT ベースIT 610.com , All Rights Reserved.
    ICP 09083238










    CI環境でAndroid SDKのダウンロードを自動化する2016
    C++パラレルプログラミング1