Skip to content

Ocv instr test #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 90 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
9e6a1bb
call native test method
vkocheryzhkin Apr 15, 2018
17566d2
Add custom detector
Apr 16, 2018
2191f3b
send bitmap to native
vkocheryzhkin Apr 17, 2018
73f2f2f
add dlib as submodule
vkocheryzhkin Apr 17, 2018
30b8869
link with dlib
vkocheryzhkin Apr 17, 2018
6bc6767
start conversion of bmp to rgb dlib image
vkocheryzhkin Apr 18, 2018
58cd257
basic dirty recognition
Apr 18, 2018
5205730
try to build release
Apr 18, 2018
e007fb9
play with rgb in native
Apr 19, 2018
2294feb
add basic python to get face vector
Apr 19, 2018
c1cca81
fix thresh for face rec
vkocheryzhkin Apr 19, 2018
5bc87c3
decrease threshold
vkocheryzhkin Apr 19, 2018
89d9677
restyle for demo
vkocheryzhkin Apr 19, 2018
95bf942
demo prepare #2
Apr 20, 2018
349fe5a
add identify button to use dlib from photo
vkocheryzhkin Apr 28, 2018
923e0b4
use separate sp and net
vkocheryzhkin Apr 28, 2018
86365a8
add basic instrumentation test
vkocheryzhkin May 4, 2018
29672e5
add travis
vkocheryzhkin May 8, 2018
3c295df
modify travis
vkocheryzhkin May 8, 2018
5d60921
update travis
vkocheryzhkin May 8, 2018
845d8ac
update travis
vkocheryzhkin May 8, 2018
6561285
update travis 4
vkocheryzhkin May 8, 2018
3f9e256
update travis 5
vkocheryzhkin May 8, 2018
c4684a3
remove abs path to signing
vkocheryzhkin May 8, 2018
831f491
update travis
vkocheryzhkin May 8, 2018
93ccda9
dup key for release
vkocheryzhkin May 8, 2018
d840398
disable lint temprarily
vkocheryzhkin May 8, 2018
2d43257
add simple native test
May 15, 2018
fb12260
start refactoring
vkocheryzhkin May 15, 2018
081986f
try to move all logic into FaceRecognizer
May 16, 2018
d37b412
try to read image from assets
vkocheryzhkin May 16, 2018
5530658
fix test
vkocheryzhkin May 16, 2018
1d2d322
refactor recognize methods
vkocheryzhkin May 18, 2018
a962582
try orientation with exif
vkocheryzhkin May 21, 2018
74faab8
rotate cropped bitmap
May 22, 2018
bb1078e
implement all orientations
vkocheryzhkin May 22, 2018
c4ca415
clean up
vkocheryzhkin May 22, 2018
e778aaf
delete exif
vkocheryzhkin May 22, 2018
f58b237
Merge pull request #1 from hoopoe/bitmap_orientation
vkocheryzhkin May 23, 2018
63f075a
This AS app is OpenCV and Dlib based configured as two different modu…
aloruss1 May 24, 2018
1153e58
build with opencv and dlib
vkocheryzhkin May 25, 2018
e49a4d8
remove opencv for java
vkocheryzhkin May 27, 2018
cf3db1e
remove jni libs
vkocheryzhkin May 27, 2018
8442d59
fix load dir
vkocheryzhkin May 27, 2018
b696585
merge master
vkocheryzhkin May 27, 2018
0da1b19
fix build
vkocheryzhkin May 27, 2018
8d299cb
fix local build for opencv
vkocheryzhkin May 27, 2018
df71024
Merge pull request #2 from hoopoe/AS-OpenCV-DLib-2modules
vkocheryzhkin May 27, 2018
5306431
restructure project
vkocheryzhkin May 27, 2018
a88b3c7
Update README.md
vkocheryzhkin May 27, 2018
ad52dfe
clean up
vkocheryzhkin May 28, 2018
1a593d6
disable identify button temporarly. try to fix build
vkocheryzhkin May 28, 2018
cd61e96
fix build
vkocheryzhkin May 28, 2018
2906463
enable auto focus
vkocheryzhkin May 30, 2018
d044290
update readme for rt statement
vkocheryzhkin May 30, 2018
3dcab26
update readme
vkocheryzhkin May 31, 2018
9701c10
request permissions for camera and sdcard together
vkocheryzhkin Jun 1, 2018
6bb8264
get cam resolution
vkocheryzhkin Jun 2, 2018
0142554
try to fix permission issue
vkocheryzhkin Jun 2, 2018
b0a9375
add Spc detector
sweetdream779 Jun 3, 2018
36ca54d
add Spc detector
sweetdream779 Jun 3, 2018
dc5a8f1
fix initial portrait orientation
vkocheryzhkin Jun 3, 2018
c7e7768
Merge pull request #3 from hoopoe/permissions
vkocheryzhkin Jun 4, 2018
cb6d7af
refactor to use 2 activities
vkocheryzhkin Jun 4, 2018
d609727
change readme; change build gradle
sweetdream779 Jun 4, 2018
9e4d3ad
change readme
sweetdream779 Jun 4, 2018
43e6d4b
Merge pull request #4 from hoopoe/activity
vkocheryzhkin Jun 5, 2018
6277ec9
merge projects
sweetdream779 Jun 6, 2018
b68dba6
merge projects
sweetdream779 Jun 6, 2018
7c1ba3d
remove .externalNativeBuild
sweetdream779 Jun 7, 2018
6b8416d
get tensorflow before build
vkocheryzhkin Jun 7, 2018
7624cfc
update travis
vkocheryzhkin Jun 7, 2018
e606a10
Merge pull request #6 from hoopoe/activity
vkocheryzhkin Jun 7, 2018
867dbf5
Merge branch 'master' into dlib-opencv-tf
vkocheryzhkin Jun 8, 2018
4e73909
change readme; add OPENCV_ANDROID_SDK var in CMakeLists
sweetdream779 Jun 8, 2018
3245ee7
Merge branch 'dlib-opencv-tf' of https://github.com/sweetdream779/FR-…
sweetdream779 Jun 8, 2018
f9a876c
Update README.md
sweetdream779 Jun 8, 2018
d211a05
Update README.md
sweetdream779 Jun 8, 2018
18edb6f
change paths; updates readme
sweetdream779 Jun 8, 2018
555ec04
change paths; updates readme
sweetdream779 Jun 8, 2018
a259d1e
return $ENV{OPENCV_ANDROID_SDK}
sweetdream779 Jun 8, 2018
f6d82fb
dlib as submodule
sweetdream779 Jun 8, 2018
60e3c66
change abiFilters
sweetdream779 Jun 8, 2018
359b090
del dlib
sweetdream779 Jun 9, 2018
01348d4
add submodule
sweetdream779 Jun 9, 2018
f3be575
Merge pull request #5 from sweetdream779/dlib-opencv-tf
vkocheryzhkin Jun 13, 2018
a5fcd0e
clean up. reorder activities
vkocheryzhkin Jun 14, 2018
6ba4f0c
merge of tensorflow and OpenCV with google-vision Dlib
aloruss1 Jun 19, 2018
0544881
added lint options for OpenCV and excluded .externalNativeBuild folde…
aloruss1 Jun 19, 2018
a48ff43
.externalNativeBuild deletion
aloruss1 Jun 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
try to fix permission issue
  • Loading branch information
vkocheryzhkin committed Jun 2, 2018
commit 01425546dcc1d9c7572d7a5c022faca4ecc63b9c
6 changes: 3 additions & 3 deletions FaceTracker/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-sdk
android:minSdkVersion="15"
Expand All @@ -20,7 +20,7 @@
android:hardwareAccelerated="true"
android:icon="@drawable/icon"
android:theme="@style/Theme.AppCompat"
android:label="FaceTracker"
android:label="Face Recognition"
android:largeHeap="true">

<meta-data android:name="com.google.android.gms.version"
Expand All @@ -33,7 +33,7 @@
<activity
android:name="com.google.android.gms.samples.vision.face.facetracker.FaceTrackerActivity"
android:icon="@drawable/icon"
android:label="SG"
android:label="Face Recognition"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:screenOrientation="fullSensor">
<intent-filter>
Expand Down
2 changes: 1 addition & 1 deletion FaceTracker/app/src/main/cpp/OCV-native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern "C"

void detect(Mat &gray) {

String face_cascade_name = "/storage/emulated/0/Movies/haarcascade_frontalface_default.xml";
String face_cascade_name = "/sdcard/Download/haarcascade_frontalface_default.xml";

CascadeClassifier face_cascade;
std::vector<Rect> faces;
Expand Down
102 changes: 20 additions & 82 deletions FaceTracker/app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,85 +72,23 @@ float FACE_RECOGNIZE_THRESH = 0.55;

extern "C"
JNIEXPORT jint JNICALL
Java_com_google_android_gms_samples_vision_face_facetracker_FaceTrackerActivity_loadResources(
JNIEnv *env, jobject instance)
{

LOGI("load resource");
FILE *file1 = fopen("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat", "r+");
FILE *file2 = fopen("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat",
"r+");
//FILE *file3 = fopen("/storage/emulated/0/Download/faces_linear.svm", "r+");

if (file1 != NULL && file2 != NULL ) {
fclose(file1);
fclose(file2);
//fclose(file3);
dlib::deserialize("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat")
>> sp;
dlib::deserialize("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat")
>> sp1;
dlib::deserialize("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat")
>> net;
dlib::deserialize("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat")
>> net1;
//dlib::deserialize("/storage/emulated/0/Download/faces_linear.svm") >> df;

DIR *d;
char *p1,*p2;
int ret;
struct dirent *dir;
d = opendir("/storage/emulated/0/Download");
if (d)
{
LOGI("Loading feature vectors using *.vec", p1);
while ((dir = readdir(d)) != NULL)
{
p1=strtok(dir->d_name,".");
p2=strtok(NULL,".");
if(p2!=NULL)
{
ret=strcmp(p2,"vec");
if(ret==0)
{
std::string name = std::string(p1);
std::string file = name + ".vec";
matrix<float, 0, 1> face_vector;
dlib::deserialize("/storage/emulated/0/Download/" + file) >> face_vector;
known_faces.insert({name, face_vector});
}
}

}
closedir(d);
}
} else {
return 1; //failed
}

return 0;
}extern "C"
JNIEXPORT jint JNICALL
Java_dlib_android_FaceRecognizer_loadResourcesPart1(JNIEnv *env, jobject instance) {

LOGI("load resource part1");
FILE *file1 = fopen("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat", "r+");
FILE *file2 = fopen("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat",
"r+");
FILE *file1 = fopen("/sdcard/Download/shape_predictor_5_face_landmarks.dat", "r+");
FILE *file2 = fopen("/sdcard/Download/dlib_face_recognition_resnet_model_v1.dat", "r+");

if (file1 != NULL && file2 != NULL ) {
fclose(file1);
fclose(file2);
dlib::deserialize("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat")
>> sp;
dlib::deserialize("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat")
>> net;
dlib::deserialize("/sdcard/Download/shape_predictor_5_face_landmarks.dat") >> sp;
dlib::deserialize("/sdcard/Download/dlib_face_recognition_resnet_model_v1.dat") >> net;

DIR *d;
char *p1,*p2;
int ret;
struct dirent *dir;
d = opendir("/storage/emulated/0/Download");
d = opendir("/sdcard/Download");
if (d)
{
LOGI("Loading feature vectors using *.vec", p1);
Expand All @@ -166,7 +104,7 @@ Java_dlib_android_FaceRecognizer_loadResourcesPart1(JNIEnv *env, jobject instanc
std::string name = std::string(p1);
std::string file = name + ".vec";
matrix<float, 0, 1> face_vector;
dlib::deserialize("/storage/emulated/0/Download/" + file) >> face_vector;
dlib::deserialize("/sdcard/Download/" + file) >> face_vector;
known_faces.insert({name, face_vector});
}
}
Expand All @@ -175,6 +113,7 @@ Java_dlib_android_FaceRecognizer_loadResourcesPart1(JNIEnv *env, jobject instanc
closedir(d);
}
} else {
LOGI("Failed to load resources part1");
return -1; //failed
}

Expand All @@ -185,18 +124,16 @@ JNIEXPORT jint JNICALL
Java_dlib_android_FaceRecognizer_loadResourcesPart2(JNIEnv *env, jobject instance) {

LOGI("load resource part2");
FILE *file1 = fopen("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat", "r+");
FILE *file2 = fopen("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat",
"r+");
FILE *file1 = fopen("/sdcard/Download/shape_predictor_5_face_landmarks.dat", "r+");
FILE *file2 = fopen("/sdcard/Download/dlib_face_recognition_resnet_model_v1.dat", "r+");

if (file1 != NULL && file2 != NULL ) {
fclose(file1);
fclose(file2);
dlib::deserialize("/storage/emulated/0/Download/shape_predictor_5_face_landmarks.dat")
>> sp1;
dlib::deserialize("/storage/emulated/0/Download/dlib_face_recognition_resnet_model_v1.dat")
>> net1;
dlib::deserialize("/sdcard/Download/shape_predictor_5_face_landmarks.dat") >> sp1;
dlib::deserialize("/sdcard/Download/dlib_face_recognition_resnet_model_v1.dat") >> net1;
} else{
LOGI("Failed to load resources part2");
return -1;
}
return 0;
Expand Down Expand Up @@ -234,7 +171,7 @@ Java_dlib_android_FaceRecognizer_recognizeFaces(JNIEnv *env,

img.set_size(infocolor.height, infocolor.width);

LOGI("size w=%d h=%d", infocolor.width, infocolor.height);
// LOGI("size w=%d h=%d", infocolor.width, infocolor.height);
for (y = 0; y < infocolor.height; y++) { //todo: performance
argb *line = (argb *) pixelscolor;
for (x = 0; x < infocolor.width; ++x) {
Expand Down Expand Up @@ -302,6 +239,7 @@ Java_dlib_android_FaceRecognizer_recognizeFace(JNIEnv *env, jobject instance, jo
LOGI("color image :: width is %d; height is %d; stride is %d; format is %d;flags is %d",
infocolor.width, infocolor.height, infocolor.stride, infocolor.format, infocolor.flags);

LOGI("known_faces size %d", known_faces.size());
if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGE("Bitmap format is not RGBA_8888 !");
return env->NewStringUTF("Image broken 2");
Expand All @@ -328,7 +266,7 @@ Java_dlib_android_FaceRecognizer_recognizeFace(JNIEnv *env, jobject instance, jo
LOGI("detected size %d", dets.size());

float min_dist = 0.0;
if(dets.size() > 0 ){
if(dets.size() > 0 ){
auto face = dets.front();
std::vector<matrix<rgb_pixel>> faces;
int x = face.left();
Expand All @@ -337,18 +275,18 @@ Java_dlib_android_FaceRecognizer_recognizeFace(JNIEnv *env, jobject instance, jo
int height = face.height();

auto shape = sp(img, face);
LOGI("shape predictor");
// LOGI("shape predictor");
matrix<rgb_pixel> face_chip;
extract_image_chip(img, get_face_chip_details(shape, 150, 0.25), face_chip);
faces.push_back(move(face_chip));

LOGI("before recognized size %d", 0);
// LOGI("before recognized size %d", -1);
std::vector<matrix<float, 0, 1>> face_descriptors = net(faces);
LOGI("after recognized size %d", face_descriptors.size());
// LOGI("after recognized size %d", face_descriptors.size());
if (face_descriptors.size() > 0)
{
matrix<float, 0, 1> face_desc = face_descriptors[0];
for (auto& i : known_faces) {
for (auto i : known_faces) {
float dist = length(face_desc - i.second );
if (dist < min_dist){
min_dist = dist;
Expand All @@ -363,7 +301,7 @@ Java_dlib_android_FaceRecognizer_recognizeFace(JNIEnv *env, jobject instance, jo
LOGI("not recognized, max dist %0.2f", min_dist);
}

// LOGI("unlocking pixels");
LOGI("unlocking pixels");
AndroidBitmap_unlockPixels(env, bmp);

std::string returnValue = "Unknown";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public void onCreate(Bundle icicle) {
// permission is not granted yet, request permission.
int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
int rs = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

if (rc == PackageManager.PERMISSION_GRANTED && rs == PackageManager.PERMISSION_GRANTED) {

createCameraSource();
} else {
requestCameraAndSdCardPermission();
Expand All @@ -114,7 +116,8 @@ private void requestCameraAndSdCardPermission() {
Log.w(TAG, "Camera and sdcard permissions are not granted. Requesting permission");

final String[] permissions = new String[]{Manifest.permission.CAMERA,
Manifest.permission.READ_EXTERNAL_STORAGE};
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};

if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
Expand All @@ -140,12 +143,9 @@ public void onClick(View view) {

private void calcCameraFrameSize()
{
// Log.i(TAG, String.format("width %d height %d", mFrontCamWidth, mFrontCamHeight));
Log.i(TAG, "test");
int numCameras = Camera.getNumberOfCameras();
Log.i(TAG, String.format("%d",numCameras));

for (int i=0;i<numCameras;i++)
for (int i = 0; i < numCameras; i++)
{
Camera.CameraInfo cameraInfo=new Camera.CameraInfo();
Camera.getCameraInfo(i, cameraInfo);
Expand All @@ -154,21 +154,15 @@ private void calcCameraFrameSize()
Camera camera= Camera.open(i);
Camera.Parameters cameraParams=camera.getParameters();
List<Camera.Size> sizes= cameraParams.getSupportedPreviewSizes();
Log.i(TAG, String.format("front sizes %d", sizes.size()));
mFrontCamWidth = sizes.get(0).width;
mFrontCamHeight = sizes.get(0).height;
Log.i(TAG, String.format("mFrontCamWidth %d", mFrontCamWidth));
Log.i(TAG, String.format("mFrontCamHeight %d", mFrontCamHeight));
camera.release();
} else if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
Camera camera = Camera.open(i);
Camera.Parameters cameraParams = camera.getParameters();
List<Camera.Size> sizes = cameraParams.getSupportedPreviewSizes();
Log.i(TAG, String.format("back sizes %d", sizes.size()));
mBackCamWidth = sizes.get(0).width;
mBackCamHeight = sizes.get(0).height;
Log.i(TAG, String.format("mBackCamWidth %d", mBackCamWidth));
Log.i(TAG, String.format("mBackCamHeight %d", mBackCamHeight));
camera.release();
}
}
Expand Down Expand Up @@ -223,15 +217,12 @@ private void createCameraSource() {
// Log.w(TAG, "mPictureDetector dependencies are not yet available.");
// }

//.setRequestedPreviewSize(640, 480)
//.setRequestedFps(30.0f)
//.setFacing(CameraSource.CAMERA_FACING_BACK)
calcCameraFrameSize();
mCameraSource = new CameraSource.Builder(context, customDetector)
.setRequestedPreviewSize(mBackCamWidth, mBackCamHeight)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setAutoFocusEnabled(true)
.setRequestedFps(20)
.setRequestedFps(10)
.build();

// mBtnDetect.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -324,6 +315,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
if (grantResults.length != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Camera permission granted - initialize the camera source");
// we have permission, so create the camerasource
mFaceRecognizer.loadNative();
createCameraSource();

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
int childHeight = (int)(((float) layoutWidth / (float) width) * height);

// If height is too tall using fit width, does fit height instead.
if (childHeight > layoutHeight) {
childHeight = layoutHeight;
childWidth = (int)(((float) layoutHeight / (float) height) * width);
}
// if (childHeight > layoutHeight) {
// childHeight = layoutHeight;
// childWidth = (int)(((float) layoutHeight / (float) height) * width);
// }

for (int i = 0; i < getChildCount(); ++i) {
getChildAt(i).layout(0, 0, childWidth, childHeight);
Expand Down