B4A Library Face detection with Google Mobile Vision API

The attached project (partially) wraps this Github project. In order to set it up you must follow this post (https://www.b4x.com/android/forum/threads/android-vision-barcode-reader-scan-qr-codes-and-other-1d-2d-barcodes.66408/) BUT use the attached B4A library files and B4A sample project:


You will initially need an internet connection as some "downloading" needs to take place before the face detection artefacts becomes visible. Note that it will not show immediately when you start the project - give it a minute or so after you have started the project and the detector artefacts will start showing.

Set the paths to the below correctly (inside the B4A project) to reflect your setup:
B4X:
#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\design\res, android.support.design

Also posting the java code as it stands at present. Change it to your liking...

You can also try the Google Mobile Vision API barcode scanner that I have posted HERE

You can download and test it but if you want to use it then you need to

1.png



2.png



NOTE THE DETECTED HAPPINESS INDEX....:)
3.png
 

Attachments

  • TheJavaCode.zip
    42.4 KB · Views: 811
  • AndroidVisionFaceTrackerLibFiles.zip
    17.5 KB · Views: 948
  • b4aAndroidVisionFaceTracker.zip
    34.8 KB · Views: 998
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
Yes added this now and dont find faces.
i m using your example with your lib.

View attachment 44239 View attachment 44240

i think the problem its the focus.

i will try your BarcodeLib now, if this have the same problem
https://www.b4x.com/android/forum/t...s-and-other-1d-2d-barcodes.66408/#post-422923
Do you have the most reason version of google play services in your additional library folder? It is not related to focus as my tablet has no focus whatsoever and it works on my tablet.
 

Douglas Farias

Expert
Licensed User
Longtime User
hi nops sorry.
i m using google play services v29
now i see have a new version v30 i will download and test.
 

Johan Schoeman

Expert
Licensed User
Longtime User

Douglas Farias

Expert
Licensed User
Longtime User
very strange, really dont works.
My google play rev 29, tested with your google play on zip files.

No way to work here.

Very strange, because your barcode libs works fine.
https://www.b4x.com/android/forum/t...codes-and-other-1d-2d-barcodes.66408/#content


I will try another device.

I need make changes on your demo samples to works like your image? or only compile and run the apk?

i m using your samples to test
 

Johan Schoeman

Expert
Licensed User
Longtime User
very strange, really dont works.
My google play rev 29, tested with your google play on zip files.

No way to work here.

Very strange, because your barcode libs works fine.
https://www.b4x.com/android/forum/t...codes-and-other-1d-2d-barcodes.66408/#content


I will try another device.

I need make changes on your demo samples to works like your image? or only compile and run the apk?

i m using your samples to test
My sample should work if you follow the instructions in post #1 of this thread. I have it working on KitKat and on my primitive tablet with an earlier version of Android
 

Douglas Farias

Expert
Licensed User
Longtime User
Tested with another device (Sony E4 kitKat) and works.

The problem its with Samsung Galaxy J2 device Android 5.1 (Focus i think)
if is no focus i dont know what is.
 
Last edited:

Douglas Farias

Expert
Licensed User
Longtime User
Strange, now its working.
tested today and works very good :):):), same example, same device. Samsung J2

Questions
1 - in your example works the works the front camera? can you show how this works pls?
i see you log it, front and back camera id
but dont show how to use the front.

2- its possible change the colors? when found a face? Text Color

3 - change the english words too?

thx
 

Johan Schoeman

Expert
Licensed User
Longtime User
Strange, now its working.
tested today and works very good :):):), same example, same device. Samsung J2

Questions
1 - in your example works the works the front camera? can you show how this works pls?
i see you log it, front and back camera id
but dont show how to use the front.

2- its possible change the colors? when found a face? Text Color

3 - change the english words too?

thx
The attached library adds the front camera - will see if I can also add your requests (2) and (3). Also posting the updated B4A project - sure you will figure it out:

In Main activity:
B4X:
    settings(0) = False                                       'do not switch on the Torch when the scanner starts
    settings(1) = True                                        'use Auto Focus once the scanner started
    settings(2) = False                                       'if true then use the back camera; if false then use the front camera

In Main_Scanner activity:
B4X:
Sub StartScan(settings() As Boolean)
   
'    If settings(0) = True Then myscan.FlashOn = True
'    If settings(0) = False Then myscan.FlashOn = False
'   
'    If settings(1) = True Then myscan.AutoFocus = True
'    If settings(1) = False Then myscan.AutoFocus = False   
    If settings(2) = True Then
        myscan.CameraToUse = 0                              'if front and back camera then use the back camera
                                                            'if back camera only then use the back camera
                                                            'if front camera only then use the front camera
    Else
        myscan.CameraToUse = 1                                'if front and back camera then use the front camera
    End If
   
    myscan.beginScan

End Sub

Tested on my Samsung S4 mini with KitKat = working.;)
 

Attachments

  • FaceTrackerNewb4aLibFiles2605.zip
    17.8 KB · Views: 334
  • b4aFaceTrackerAddFrontCamera.zip
    34.9 KB · Views: 349

Johan Schoeman

Expert
Licensed User
Longtime User
Strange, now its working.
tested today and works very good :):):), same example, same device. Samsung J2

Questions
1 - in your example works the works the front camera? can you show how this works pls?
i see you log it, front and back camera id
but dont show how to use the front.

2- its possible change the colors? when found a face? Text Color

3 - change the english words too?

thx
The attached libs will sort out your request #3 (change the English words). I have used "English to Portuguese" translation on Google for Left Eye, Right Eye, and Happiness so not sure it they have been used in the right context.

Attached new B4A lib files and new B4A project. Note the following in activity Main_Scanner:
B4X:
Sub StartScan(settings() As Boolean)
   
'    If settings(0) = True Then myscan.FlashOn = True
'    If settings(0) = False Then myscan.FlashOn = False
'   
'    If settings(1) = True Then myscan.AutoFocus = True
'    If settings(1) = False Then myscan.AutoFocus = False   
    If settings(2) = True Then
        myscan.CameraToUse = 0                              'if front and back camera then use the back camera
                                                            'if back camera only then use the back camera
                                                            'if front camera only then use the front camera
    Else
        myscan.CameraToUse = 1                                'if front and back camera then use the front camera
    End If
   
    'left eye  = Bothidae
    'right eye = Pleuronectidae
    'Happiness = Felicidade 
   
    myscan.LeftEyeText = "Bothidae: "
    myscan.RightEyeText = "Pleuronectidae: "
    myscan.HappinessText = "Felicidade :"


   
    myscan.beginScan

End Sub

5.png
 

Attachments

  • AndroidVisionFaceTrackerNewLibFiles.zip
    18.1 KB · Views: 293
  • b4aFaceTrackerAddOwnText.zip
    35 KB · Views: 283

Douglas Farias

Expert
Licensed User
Longtime User
works very fast and good.
front and back camera, very fast

now the question is how log or get the result values?

The event on Main_Scanner is never called
Sub scanner_scan_result(data As String, format As Int)

and on main too
Sub GetResult(retval() As String)


thx again
 

Johan Schoeman

Expert
Licensed User
Longtime User
a
works very fast and good.
front and back camera, very fast

now the question is how log or get the result values?

The event on Main_Scanner is never called
Sub scanner_scan_result(data As String, format As Int)

and on main too
Sub GetResult(retval() As String)


thx again

Here is an update. The FaceTrackerActivity will raise an event in the B4A project whenever this method is executed in FaceTrackerActivity.java:
B4X:
public void onNewItem(int faceId, Face item) {

In Main_Scanner of the B4A project:
B4X:
Sub scanner_scan_result(faceid As Int, IsLeftEyeOpenProbability As Float, IsRightEyeOpenProbability As Float, IsSmilingProbability As Float)         
                                                               

    Log("back in b4a with faceid = " & faceid)
    Log("back in b4a with IsLeftEyeOpenProbability = " & IsLeftEyeOpenProbability)
    Log("back in b4a with IsRightEyeOpenProbability = " & IsRightEyeOpenProbability)
    Log("back in b4a with IsSmilingProbability = " & IsSmilingProbability)
    Log(" ")


'    Dim retval(2) As String
'    retval(0) = fmt
'    retval(1) = data
'
'    CallSubDelayed2(Main, "GetResult", retval)                'pass the scanned result back to the Main activity.
'
'    Log("Format : " & fmt)
'    Log("Data : " & data)

'    Activity.Finish                                           'kill this activity so that the Main activity is displayed

End Sub

The B4A code will however only be executed when FaceTrackerActivity is paused (i.e when you press the back button). FaceTrackerActivity is a separate activity in the library and whatever it is asked to pass on to B4A will be added to a waiting queue and only be processed when the B4A activity is resumed.

Have also added the option to change the text size and the box line width/thickness around the face(s)
B4X:
myscan.BoxStrokeWidth = 7
myscan.IdTextSize = 30

Attached new B4A library files and an updated B4A project.
 

Attachments

  • b4aFaceTrackerAddReturnData.zip
    35.1 KB · Views: 310
  • AndroidVisionFaceTrackerLibFiles280516.zip
    20.1 KB · Views: 323

danoptic

Member
Licensed User
Longtime User
I cant get the resources working
I use b4a 6, windows 10, last sdk.
Whate m i doing wrong?

ERROR: resource directory 'c:\andriod_sdk_tools\extras\android\support\design\res' does not exist
 

DonManfred

Expert
Licensed User
Longtime User
ERROR: resource directory 'c:\andriod_sdk_tools\extras\android\support\design\res' does not exist
- use the latest android support design library (3.2) from @corwin42
- remove the #additionalres command which point to this folder
 

danoptic

Member
Licensed User
Longtime User
I add new files to the resource app directory adn this what I get with the following #additionalres:

'#AdditionalRes: ..\resource\

#AdditionalRes: C:\Users\Owner\Dropbox\basic4android\SourceCode\b4aAndroidVisionFaceTracker\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#AdditionalRes: C:\Users\Owner\AppData\Local\Android\android-sdk\extras\google\m2repository\com\google\android\gms, com.google.android.gms
#AdditionalRes: C:\Users\Owner\AppData\Local\Android\android-sdk\extras\google\google_play_services, com.google.android.gms
'#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\design\res, android.support.design

#ExcludeClasses: .games, .drive, .ads, .fitness, .wearable, .measurement, .cast, .auth, .nearby
#ExcludeClasses: .tagmanager, .analytics, .wallet, .plus, .gcm, .maps, .panorama

#Extends: android.support.v7.app.AppCompatActivity



B4A version: 6.00
Parsing code. (0.01s)
Compiling code. (0.09s)
Compiling layouts code. (0.12s)
Organizing libraries. (4.89s)
Generating R file. Error
invalid resource directory name: c:\users\owner\appdata\local\android\android-sdk\extras\google\google_play_services docs
invalid resource directory name: c:\users\owner\appdata\local\android\android-sdk\extras\google\google_play_services samples
invalid resource directory name: c:\users\owner\appdata\local\android\android-sdk\extras\google\google_play_services source.properties
 

DonManfred

Expert
Licensed User
Longtime User
#AdditionalRes: C:\Users\Owner\Dropbox\basic4android\SourceCode\b4aAndroidVisionFaceTracker\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#AdditionalRes: C:\Users\Owner\AppData\Local\Android\android-sdk\extras\google\m2repository\com\google\android\gms, com.google.android.gms
#AdditionalRes: C:\Users\Owner\AppData\Local\Android\android-sdk\extras\google\google_play_services, com.google.android.gms
#ExcludeClasses: .games, .drive, .ads, .fitness, .wearable, .measurement, .cast, .auth, .nearby
#ExcludeClasses: .tagmanager, .analytics, .wallet, .plus, .gcm, .maps, .panorama

I guess all these lines can be marked inactive/removed
 

jchal

Active Member
Licensed User
Longtime User
it is been several days trying to run the project
my question is
when i run it i get

B4A version: 5.80
Parsing code. (0.00s)
Compiling code. (0.07s)
Compiling layouts code. (0.00s)
Generating R file. Error
ERROR: resource directory 'c:\andriod_sdk_tools\extras\android\support\design\res' does not exist
when i comment out the following lines

#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\design\res, android.support.design
i get
B4A version: 5.80
Parsing code. (0.01s)
Compiling code. (0.03s)
Compiling layouts code. (0.01s)
Generating R file. Error
..\resource\values\themes.xml:3: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat'.
..\resource\values\themes.xml:6: error: Error: No resource found that matches the given name: attr 'colorAccent'.
..\resource\values\themes.xml:4: error: Error: No resource found that matches the given name: attr 'colorPrimary'.
..\resource\values\themes.xml:5: error: Error: No resource found that matches the given name: attr 'colorPrimaryDark'.
and finaly when i coment out
'#AdditionalRes: ..\resource
i get
B4A version: 5.80
Parsing code. (0.00s)
Compiling code. (0.04s)
Compiling layouts code. (0.00s)
Generating R file. Error
AndroidManifest.xml:22: error: Error: No resource found that matches the given name (at 'theme' with value '@style/MyAppTheme').
AndroidManifest.xml:27: error: Error: No resource found that matches the given name (at 'value' with value '@@Integer/google_play_services_version').
can you give me some guide lines how to run it?
 
Top