Skip to content
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

My despair at trying to simply import the opencv face class (python-for-android) #2206

Closed
SonofPoseidon1982 opened this issue May 18, 2020 · 4 comments · Fixed by #2209
Closed

Comments

@SonofPoseidon1982
Copy link

SonofPoseidon1982 commented May 18, 2020

I've been trying this for weeks, I was told it would be straightforward and that if I wanted to import the face class I could simply "add that option to the recipe". But this is somehow starting to seem more difficult than I first thought. Has anyone ever managed to compile any of the extra modules (contrib) through p4a by changing the opencv recipe? If this is indeed too difficult, could I use pyjnius, as only 10 lines of code in my entire app is dedicated to detecting facial landmarks.

@opacam
Copy link
Member

opacam commented May 20, 2020

I just created a PR that it may help you, honestly, I don't know if it will work at runtime. Btw, afaik, the opencv_contrib repository is intended to be used for development purposes, so the compiled libraries may work or not...Anyway, could you try #2209?

and please...let us know if it worked for you 😉

@SonofPoseidon1982
Copy link
Author

SonofPoseidon1982 commented May 24, 2020

Thanks for this. I think all the modules are importing from the repository, but I still haven't managed to get 68 facial landmarks. It finds the face class at runtime but I keep getting the error "OpenCV NoneType object has no attribute 'fit'". It finds the faces, but not the face landmark points. I suspect however this is probably just a general opencv issue rather than any specific difficulties in importing this module through Python-for-android. Here is an extract of the code:

      # reducing image size to avoid opencv memory allocation errors
      ii_file = "tmp_r_i.png"
      tmp_tiny_width = 650
      tmp_img = p_img.open(i_file)
      i_perc = tmp_tiny_width/float(tmp_img.size[0])
      i_height = int(float(tmp_img.size[1]) * float(i_perc))
      tmp_img2 = tmp_img.resize((tmp_tiny_width, i_height), p_img.ANTIALIAS)
      tmp_img2.save(ii_file)
             
      img_1 = cv2.imread(ii_file)
      img = cv2.cvtColor(img_1, cv2.COLOR_BGR2GRAY)
      det = cv2.CascadeClassifier(lm_model)
      faces = det.detectMultiScale(img)
      l_det = cv2.face.createFacemarkLBF()
      l_det = l_det.loadModel(lm_model_2)
      ff_locations_01 = l_det.fit(img, faces)

@SonofPoseidon1982
Copy link
Author

opacam,

the build has been successful, but I am encountering an error when trying to find face landmarks. . It loads the LBF model, but then crashes when calling the 'cv2.face.fit' function on an android device. I am using opencv/opencv_contrib 4.0.1 and NDK 19. The relevant code is shown below (it works on my laptop):

img_1 = cv2.imread(ii_file)
img = cv2.cvtColor(img_1, cv2.COLOR_BGR2GRAY)
det = cv2.CascadeClassifier(lm_model)
faces = det.detectMultiScale(img)

l_det = cv2.face.createFacemarkLBF()
l_det.loadModel(lm_model_2)

ff_locations_01 = l_det.fit(img, faces)

It crashes immediately after it calls the fit function with the following stacktraces:

*--------- beginning of crash

06-05 18:23:34.580 8134 8162 F libc : /buildbot/src/android/ndk-release-r19/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:73: abort_message: assertion "terminating with uncaught exception of type std::length_error: vector" failed
06-05 18:23:37.604 8134 8162 F libc : Fatal signal 6 (SIGABRT), code -6 in tid 8162 (SDLThread)
06-05 18:23:37.749 9203 9203 I AEE_AED : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-05 18:23:37.750 9203 9203 I AEE_AED : Build fingerprint: 'OUKITEL/U22/U22:7.0/NRD90M/1508468660:user/release-keys'
06-05 18:23:37.750 9203 9203 I AEE_AED : Revision: '0'
06-05 18:23:37.750 9203 9203 I AEE_AED : ABI: 'arm'
06-05 18:23:37.750 9203 9203 I AEE_AED : pid: 8134, tid: 8162, name: SDLThread >>> com.aniface.aface <<<
06-05 18:23:37.750 9203 9203 I AEE_AED : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
06-05 18:23:37.762 9203 9203 I AEE_AED : Abort message: '/buildbot/src/android/ndk-release-r19/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:73: abort_message: assertion "terminating with uncaught exception of type std::length_error: vector" failed'
06-05 18:23:37.762 9203 9203 I AEE_AED : r0 00000000 r1 00001fe2 r2 00000006 r3 00000008
06-05 18:23:37.763 9203 9203 I AEE_AED : r4 97425978 r5 00000006 r6 97425920 r7 0000010c
06-05 18:23:37.763 9203 9203 I AEE_AED : r8 960bfe49 r9 97422b28 sl fbd00bff fp 00000000
06-05 18:23:37.763 9203 9203 I AEE_AED : ip 00000000 sp 97422528 lr b158b7d7 pc b158e034 cpsr 200f0010
06-05 18:23:37.787 9203 9203 I AEE_AED :
06-05 18:23:37.787 9203 9203 I AEE_AED : backtrace:
06-05 18:23:37.787 9203 9203 I AEE_AED : #00 pc 0004b034 /system/lib/libc.so (tgkill+12)
06-05 18:23:37.787 9203 9203 I AEE_AED : #1 pc 000487d3 /system/lib/libc.so (pthread_kill+34)
06-05 18:23:37.787 9203 9203 I AEE_AED : #2 pc 0001d785 /system/lib/libc.so (raise+10)
06-05 18:23:37.787 9203 9203 I AEE_AED : #3 pc 000192c1 /system/lib/libc.so (__libc_android_abort+34)
06-05 18:23:37.787 9203 9203 I AEE_AED : #4 pc 00017034 /system/lib/libc.so (abort+4)
06-05 18:23:37.787 9203 9203 ...

Part of the reason I am bringing this to your attention is not only because I suspect you're one of the best qualified guys to help me but also because it may be a problem in the python-for-android build in the extra_class recipe. I don't understand the error, I have tried increasing my mobile devices memory and also changing the ndk version, nothing seems to help.
Have you any idea why this error is occurring when detecting face landmarks on android?

cheers.

Nick

@SonofPoseidon1982
Copy link
Author

SonofPoseidon1982 commented Jun 26, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants