Skip to content

Commit e007fb9

Browse files
author
Vladimir Kocheryzhkin
committed
play with rgb in native
1 parent 5205730 commit e007fb9

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed

visionSamples/FaceTracker/app/src/main/cpp/native-lib.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ multiclass_linear_decision_function<lin_kernel, string> df;
6161

6262
typedef struct
6363
{
64-
uint8_t alpha;
65-
uint8_t red;
66-
uint8_t green;
67-
uint8_t blue;
64+
uint8_t alpha;//r
65+
uint8_t red;//g
66+
uint8_t green;//b
67+
uint8_t blue;//a see rgb_pixel assignment
6868
} argb;
6969

70-
70+
int FACE_RECOGNIZE_THRESH = 0.5;
7171

7272
extern "C"
7373
{
@@ -101,14 +101,14 @@ extern "C"
101101
for (y = 0; y < infocolor.height; y++) { //todo: performance
102102
argb *line = (argb *) pixelscolor;
103103
for (x = 0; x < infocolor.width; ++x) {
104-
rgb_pixel p(line[x].red, line[x].green, line[x].blue);
104+
rgb_pixel p(line[x].alpha, line[x].red, line[x].green);
105105
img[y][x] = p;
106106
}
107107
pixelscolor = (char *) pixelscolor + infocolor.stride;
108108
}
109109

110110
//todo: smth wrong with colors
111-
//dlib::save_bmp(img, "/storage/emulated/0/Movies/test2.bmp");
111+
//dlib::save_bmp(img, "/storage/emulated/0/Download/test2.bmp");
112112

113113
std::vector<dlib::rectangle> dets = detector(img);
114114
LOGI("detected size %d", dets.size());
@@ -133,7 +133,7 @@ extern "C"
133133
matrix<float, 0, 1> face_desc = face_descriptors[0];
134134
LOGI("recognized");
135135
for (auto& i : known_faces) {
136-
if( length(face_desc - i.second ) < 0.5) //todo: extrace thresh
136+
if( length(face_desc - i.second ) < FACE_RECOGNIZE_THRESH) //todo: extract thresh
137137
{
138138
return env->NewStringUTF(i.first.c_str());
139139
}
@@ -159,17 +159,17 @@ Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_
159159
FILE *file1 = fopen("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat", "r+");
160160
FILE *file2 = fopen("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat",
161161
"r+");
162-
FILE *file3 = fopen("/storage/emulated/0/Download/faces_linear.svm", "r+");
162+
//FILE *file3 = fopen("/storage/emulated/0/Download/faces_linear.svm", "r+");
163163

164-
if (file1 != NULL && file2 != NULL && file3 != NULL) {
164+
if (file1 != NULL && file2 != NULL ) {
165165
fclose(file1);
166166
fclose(file2);
167-
fclose(file3);
167+
//fclose(file3);
168168
dlib::deserialize("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat")
169169
>> sp;
170170
dlib::deserialize("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat")
171171
>> net;
172-
dlib::deserialize("/storage/emulated/0/Download/faces_linear.svm") >> df;
172+
//dlib::deserialize("/storage/emulated/0/Download/faces_linear.svm") >> df;
173173

174174
DIR *d;
175175
char *p1,*p2;

visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/CustomDetector.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import java.io.ByteArrayOutputStream;
1717
import java.io.File;
1818
import java.io.FileOutputStream;
19+
import java.io.FileWriter;
20+
import java.io.IOException;
21+
22+
import static android.os.Environment.getExternalStorageDirectory;
1923

2024
interface RecognitionInterface
2125
{
@@ -44,17 +48,11 @@ public class CustomDetector extends Detector<Face> {
4448
}
4549

4650
void startRecognition(int faceId, int _x, int _y, int _w, int _h) {
47-
// if (!IsRunning)
48-
// {
4951
faceid = faceId;
5052
x = _x;
5153
y = _y;
5254
w = _w;
5355
h = _h;
54-
// IsRecognitionRequested = true;
55-
// } else{
56-
// IsRecognitionRequested = false;
57-
// }
5856
}
5957

6058
void resetRecognition()
@@ -83,7 +81,16 @@ public SparseArray<Face> detect(Frame frame) {
8381
byte[] jpegArray = byteArrayOutputStream.toByteArray();
8482
Bitmap tmpBitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);
8583
final Bitmap cropped = Bitmap.createBitmap(tmpBitmap, x, y, w, h);
86-
84+
// try {
85+
//
86+
// File file = new File (getExternalStorageDirectory(), "/Download/test1.bmp");
87+
// FileOutputStream out = new FileOutputStream(file);
88+
// cropped.compress(Bitmap.CompressFormat.JPEG, 100, out);
89+
// out.flush();
90+
// out.close();
91+
// } catch (IOException e) {
92+
// e.printStackTrace();
93+
// }
8794
mT = new Thread(new Runnable() {
8895
@Override
8996
public void run() {

visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/FaceGraphic.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.graphics.Bitmap;
1919
import android.graphics.Canvas;
2020
import android.graphics.Color;
21+
import android.graphics.DashPathEffect;
2122
import android.graphics.Paint;
2223
import android.os.Environment;
2324
import android.util.Log;
@@ -61,18 +62,21 @@ class FaceGraphic extends GraphicOverlay.Graphic implements RecognitionInterface
6162
private Paint mFacePositionPaint;
6263
private Paint mIdPaint;
6364
private Paint mBoxPaint;
64-
65+
6566
public volatile Face mFace;
6667
private int mFaceId;
6768
private float mFaceHappiness;
6869

69-
public int frame_cx = 5; //start after 5 frames
70+
7071

7172
private Thread mT;
7273
private CustomDetector mCustomDetector;
7374
private volatile boolean IsRecognized;
7475
private String mIdentity = "";
7576

77+
private int FRAMES_TO_SKIP = 15;
78+
public int frame_cx = FRAMES_TO_SKIP; //start after n frames
79+
7680
FaceGraphic(GraphicOverlay overlay, CustomDetector customDetector) {
7781
super(overlay);
7882
mCustomDetector = customDetector;
@@ -83,11 +87,11 @@ class FaceGraphic extends GraphicOverlay.Graphic implements RecognitionInterface
8387
mFacePositionPaint.setColor(selectedColor);
8488

8589
mIdPaint = new Paint();
86-
mIdPaint.setColor(selectedColor);
90+
mIdPaint.setColor(Color.GREEN);
8791
mIdPaint.setTextSize(ID_TEXT_SIZE);
8892

8993
mBoxPaint = new Paint();
90-
mBoxPaint.setColor(selectedColor);
94+
mBoxPaint.setColor(Color.MAGENTA);
9195
mBoxPaint.setStyle(Paint.Style.STROKE);
9296
mBoxPaint.setStrokeWidth(BOX_STROKE_WIDTH);
9397
}
@@ -106,6 +110,7 @@ void updateFace(Face face) {
106110
postInvalidate();
107111

108112
if (frame_cx > 0) {
113+
mBoxPaint.setColor(Color.MAGENTA);
109114
frame_cx--;
110115
} else {
111116
if (!mCustomDetector.IsBusy && !IsRecognized) { //one face at time
@@ -115,6 +120,7 @@ void updateFace(Face face) {
115120
int h = (int)face.getHeight();
116121
mCustomDetector.setHandlerListener(this);
117122
mCustomDetector.startRecognition(mFaceId, x, y, w, h);
123+
mBoxPaint.setColor(Color.BLUE);
118124
}
119125
}
120126
}
@@ -136,13 +142,11 @@ public void draw(Canvas canvas) {
136142
// Draws a circle at the position of the detected face, with the face's track id below.
137143
float x = translateX(face.getPosition().x + face.getWidth() / 2);
138144
float y = translateY(face.getPosition().y + face.getHeight() / 2);
139-
canvas.drawCircle(x, y, FACE_POSITION_RADIUS, mFacePositionPaint);
140-
canvas.drawText("id: " + mFaceId, x + ID_X_OFFSET, y + ID_Y_OFFSET, mIdPaint);
145+
//canvas.drawCircle(x, y, FACE_POSITION_RADIUS, mFacePositionPaint);
146+
//canvas.drawText("id: " + mFaceId, x + ID_X_OFFSET, y + ID_Y_OFFSET, mIdPaint);
141147
if(mIdentity != ""){
142148
canvas.drawText("identity: " + mIdentity, x - ID_X_OFFSET, y - ID_Y_OFFSET, mIdPaint);
143149
}
144-
//canvas.drawText("right eye: " + String.format("%.2f", face.getIsRightEyeOpenProbability()), x + ID_X_OFFSET * 2, y + ID_Y_OFFSET * 2, mIdPaint);
145-
//canvas.drawText("left eye: " + String.format("%.2f", face.getIsLeftEyeOpenProbability()), x - ID_X_OFFSET*2, y - ID_Y_OFFSET*2, mIdPaint);
146150

147151
// Draws a bounding box around the face.
148152
float xOffset = scaleX(face.getWidth() / 2.0f);
@@ -152,21 +156,27 @@ public void draw(Canvas canvas) {
152156
float right = x + xOffset;
153157
float bottom = y + yOffset;
154158
canvas.drawRect(left, top, right, bottom, mBoxPaint);
159+
160+
//canvas.drawText("right eye: " + String.format("%.2f", face.getIsRightEyeOpenProbability()), x + ID_X_OFFSET * 2, y + ID_Y_OFFSET * 2, mIdPaint);
161+
//canvas.drawText("left eye: " + String.format("%.2f", face.getIsLeftEyeOpenProbability()), x - ID_X_OFFSET*2, y - ID_Y_OFFSET*2, mIdPaint);
162+
155163
}
156164

157165
@Override
158166
public void onRecognized(String str) {
159-
frame_cx = 5; //reset
167+
frame_cx = FRAMES_TO_SKIP; //reset
160168
mCustomDetector.setHandlerListener(null); //unsubscribe
161169
mCustomDetector.resetRecognition();
162170
if (str == "Unknown")
163171
{
164172
Log.w(TAG, "Not Recognized");
173+
mBoxPaint.setColor(Color.MAGENTA);
165174
IsRecognized = false;
166175
mIdentity = str;
167176
}
168177
else{
169178
Log.w(TAG, "Recognized");
179+
mBoxPaint.setColor(Color.GREEN);
170180
IsRecognized = true;
171181
mIdentity = str;
172182
}

visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/FaceTrackerActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ private void createCameraSource() {
206206
//.setFacing(CameraSource.CAMERA_FACING_BACK)
207207
mCameraSource = new CameraSource.Builder(context, customDetector)
208208
.setRequestedPreviewSize(1024, 768)
209-
.setFacing(CameraSource.CAMERA_FACING_FRONT)
210-
.setRequestedFps(15.0f)
209+
.setFacing(CameraSource.CAMERA_FACING_BACK)
210+
.setRequestedFps(20.0f)
211211
.build();
212212
}
213213

0 commit comments

Comments
 (0)