liuxiaolong
2019-05-06 573d1e33a32da5c55c638df2ee622d972976c1d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.basic.security.utils;
 
import android.app.Activity;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
 
import com.basic.security.activity.MainActivity;
import com.basic.security.base.BaseApplication;
import com.basic.security.manager.impl.cblite.BaseSettingManager;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
public class ProcessImageAndDrawResults {
    public void setRgb_gray_camera(int rgb_gray_camera) {
        this.rgb_gray_camera = rgb_gray_camera;
    }
 
    public int rgb_gray_camera;
    public int mStopping;
    public int mStopped;
    public byte[] mYUVData;
    public byte[] mRGBData;
    public int mImageWidth, mImageHeight;
    public boolean rotated;
    BitmapHolder bitmapHolder = new BitmapHolder();
    static List<DetectListener> detectListeners = new ArrayList<>();
 
    public static void addDetectedListener(DetectListener detectListener) {
        detectListeners.add(detectListener);
    }
 
    public ProcessImageAndDrawResults(int rgb_gray_camera) {
        this.rgb_gray_camera = rgb_gray_camera;
    }
 
    public void start(Bitmap frameBitmap) {
        if (mRGBData == null) {
            return;
        }
        if (rgb_gray_camera == Constants.RGB_CAMERA) {
            BaseApplication.getApplication().detectLock.lock();
        }
        try {
            detectFace(rgb_gray_camera, frameBitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (rgb_gray_camera == Constants.RGB_CAMERA) {
            BaseApplication.getApplication().detectLock.unlock();
        }
    }
 
    private byte[] cropImage(int x1, int y1, int x2, int y2, String frameJpgPath, Bitmap frameBitmap) {
        float ratio = 0.8f;
        int enlargeWidth = (int) (ratio * (x2 - x1));
        int enlargeHeight = (int) (ratio * (y2 - y1));
        x1 = x1 - enlargeWidth / 2;
        x2 = x2 + enlargeWidth / 2;
        y1 = y1 - enlargeHeight / 2;
        y2 = y2 + enlargeHeight / 2;
        if (x1 < 0) {
            x1 = 0;
        }
        if (y1 < 0) {
            y1 = 0;
        }
        if (x2 > mImageWidth) {
            x2 = mImageWidth;
        }
        if (y2 > mImageHeight) {
            y2 = mImageHeight;
        }
        int width = (int) ((x2 - x1));
        int height = (int) ((y2 - y1));
        if (width > mImageWidth) {
            width = mImageWidth;
        }
        if (height > mImageHeight) {
            height = mImageHeight;
        }
        Bitmap bitmap = null;
        if (frameBitmap != null) {
            bitmap = frameBitmap;
        } else {
            bitmap = BitmapFactory.decodeFile(frameJpgPath);
        }
 
        bitmapHolder.storeBitmap(bitmap);
        bitmapHolder.cropBitmap(x1, y1, x2, y2);
        Bitmap faceBitmap = bitmapHolder.getBitmapAndFree();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        faceBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] faceData = stream.toByteArray();
 
        if (faceBitmap != null && !faceBitmap.isRecycled()) {
            faceBitmap.recycle();
        }
        faceBitmap = null;
        bitmap = null;
 
        return faceData;
    }
 
    private void setPosition(FacePosition facePosition, int x1, int y1, int x2, int y2) {
        float ratio = 0.5f;
        int enlargeWidth = (int) (ratio * (x2 - x1));
        int enlargeHeight = (int) (ratio * (y2 - y1));
        x1 = x1 - enlargeWidth / 2;
        x2 = x2 + enlargeWidth / 2;
        y1 = y1 - enlargeHeight / 2;
        y2 = y2 + enlargeHeight / 2;
        if (x1 < 0) {
            x1 = 0;
        }
        if (y1 < 0) {
            y1 = 0;
        }
        if (x2 > mImageWidth) {
            x2 = mImageWidth;
        }
        if (y2 > mImageHeight) {
            y2 = mImageHeight;
        }
        int width = (int) ((x2 - x1));
        int height = (int) ((y2 - y1));
        if (width > mImageWidth) {
            width = mImageWidth;
        }
        if (height > mImageHeight) {
            height = mImageHeight;
        }
        facePosition.x1 = x1;
        facePosition.y1 = y1;
        facePosition.x2 = x2;
        facePosition.y2 = y2;
        facePosition.faceRatio = (y2-y1)*(x2-x1)*1.0/(640*480);
    }
 
    public void addDetectedResult(DetectedResult detectedResult, int x1, int y1, int x2, int y2, float score, String frameJpgPath,
                                  String faceJpgPath, long trackerId, boolean liveness, String resultText,
                                  int featureId, String featureName,
                                  int yaw,
                                  int pitch,
                                  int roll) {
        try {
            FacePosition facePosition = new FacePosition();
            setPosition(facePosition, x1, y1, x2, y2);
            facePosition.trackerId = trackerId;
            facePosition.liveness = liveness;
            facePosition.faceJpgData = cropImage(x1, y1, x2, y2, frameJpgPath, detectedResult.frameBitmap);
            facePosition.score = score;
            facePosition.yaw = yaw;
            facePosition.pitch = pitch;
            facePosition.roll = roll;
            facePosition.createTime = System.currentTimeMillis();
            if (featureId != -1) {
                facePosition.featureName = featureName;
            }
            detectedResult.facePositions.add(facePosition);
//            try {
//                FileOutputStream output = new FileOutputStream(new File(faceJpgPath));
//                output.write(facePosition.faceJpgData);
//                output.flush();
//                output.close();
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    static boolean loaded =false;
 
    private void detectFace(int rgbGrayCamera, Bitmap frameBitmap) throws Exception {
        String outputFileName = "";
        if (rgbGrayCamera == Constants.RGB_CAMERA) {
            outputFileName = "rgb.jpg";
        } else {
            outputFileName = "gray.jpg";
        }
        String filesPath = new ContextWrapper(FaceId.activity).getFilesDir().getAbsolutePath();
        File jpgFile = new File(filesPath, "image_"+outputFileName);
        File featureFile = new File(filesPath, "feature");
        File targetFile = new File(filesPath, outputFileName);
//        FileOutputStream outputStream = new FileOutputStream(targetFile);
//        outputStream.write(BgrUtils.getPixelsBGR(BitmapFactory.decodeFile(jpgFile.getAbsolutePath())));
//        outputStream.close();
        File imgFile = new File(targetFile.getAbsolutePath());
        if (imgFile.exists()) {
 
        }
        String resultsStr = "";
        if (rgbGrayCamera == Constants.RGB_CAMERA) {
//            resultsStr = FaceId.instance.rgbDetectFace(filesPath, targetFile.getAbsolutePath(),
//                    mImageWidth, mImageHeight, featureFile.getAbsolutePath(),
//                    ((MainActivity) BaseApplication.getApplication().activity).shouldExtractFeature(),Constants.USE_GRAY_CAMERA,
//                    BaseSettingManager.allowMultipleFace() ?20:1
//            );
            resultsStr = FaceId.instance.rgbDetectFace(filesPath, targetFile.getAbsolutePath(),
                    mImageWidth, mImageHeight, featureFile.getAbsolutePath(),
                    0,Constants.USE_GRAY_CAMERA,
                    BaseSettingManager.allowMultipleFace() ?20:1
            );
        } else {
            resultsStr = FaceId.instance.grayDetectFace(filesPath, targetFile.getAbsolutePath(), mImageWidth, mImageHeight,
                    BaseSettingManager.allowMultipleFace() ?20:1);
        }
        if (!loaded) {
            loaded = true;
//            SaveTestData.savePicToDatabase();
        }
        if (!"".equals(resultsStr)) {
//            System.out.println("resultText=" + resultsStr);
        }
        String resultText = "";
        String[] results = resultsStr.split("\\|");
        if(results.length != 0 && !results[0].equals("")){
            System.out.println("有人脸!!!");
            if (rgbGrayCamera == Constants.RGB_CAMERA) {
            resultsStr = FaceId.instance.rgbDetectFace(filesPath, targetFile.getAbsolutePath(),
                    mImageWidth, mImageHeight, featureFile.getAbsolutePath(),
                    1,Constants.USE_GRAY_CAMERA,
                    BaseSettingManager.allowMultipleFace() ?20:1
            );
            } else {
                resultsStr = FaceId.instance.grayDetectFace(filesPath, targetFile.getAbsolutePath(), mImageWidth, mImageHeight,
                        BaseSettingManager.allowMultipleFace() ?20:1);
            }
 
            results = resultsStr.split("\\|");
            DisplayMetrics displayMetrics = new DisplayMetrics();
            ((Activity) FaceId.activity).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
 
            DetectedResult detectedResult = new DetectedResult();
            detectedResult.frameBitmap = frameBitmap;
            detectedResult.width = mImageWidth;
            detectedResult.height = mImageHeight;
            String frameJpgPath = new File(new ContextWrapper(FaceId.activity).getFilesDir().getAbsolutePath(), "image_rgb.jpg").getAbsolutePath();
            detectedResult.frameJpgData = FileUtil.readFile1(frameJpgPath);
            detectedResult.resultText = resultText;
 
            for (String result : results) {
                if (result.contains(",")) {
                    String elem[] = result.split(",", -1);
                    if (elem.length == 13) {
                        int left = Integer.parseInt(elem[0]);
                        int top = Integer.parseInt(elem[1]);
                        int right = Integer.parseInt(elem[2]);
                        int bottom = Integer.parseInt(elem[3]);
                        float score = Float.parseFloat(elem[4]);
                        long trackerId = Long.parseLong(elem[5]);
                        resultText += "" + elem[7] + "\r\n" + elem[5] + "\r\n" + elem[0] + "," + elem[1] + "," + elem[2] + "," + elem[3] + "\r\n" + elem[4] + "\r\n";
                        if (rgbGrayCamera == Constants.RGB_CAMERA) {
                            String faceJpgPath = new File(new ContextWrapper(FaceId.activity).getFilesDir().getAbsolutePath(), "image_rgb_face.jpg").getAbsolutePath();
                            int featureId = Integer.parseInt(elem[8]);
                            String featurePath = elem[9];
                            if (!"".equals(featurePath) && featurePath != null
                                    && featurePath.length() > 5 &&
                                    featureId != -1) {
                                detectedResult.featureCount++;
                            }
                            int yaw = Integer.parseInt(elem[10]);
                            int pitch = Integer.parseInt(elem[11]);
                            int roll = Integer.parseInt(elem[12]);
//                        System.out.println("yaw="+yaw+",pitch="+pitch+",roll="+roll+",score="+score);
                            addDetectedResult(detectedResult, left, top, right, bottom, score,
                                    frameJpgPath, faceJpgPath, trackerId, elem[7].equals("活体"), resultText,
                                    featureId,
                                    featurePath,
                                    yaw,
                                    pitch,
                                    roll
                            );
                        }
                    } else {
                        resultText += "[" + result.substring(0, result.lastIndexOf(',')) + "]="
                                + result.substring(result.lastIndexOf(',') + 1) + "\r\n";
                    }
                }
            }
            for (DetectListener detectListener : detectListeners) {
                detectListener.faceDetected(detectedResult);
            }
        }
 
    }
 
 
}