B4A Library Onyx

this is a wrap for this Github Project.

An android library that uses technologies like artificial Intelligence, machine learning, and deep learning to make developers understand the content that they are displaying in their app.

Please note that this lib used service from Clarifai which is not totally free (https://clarifai.com/pricing)


Onyx
Author:
DonManfred (wrapper)
Version: 1
  • OnyxTags
    Events:
    • onComplete (tags As List)
    Methods:
    • Initialize (EventName As String)
    • IsInitialized As Boolean
    • TagsandProbability
    • TagsfromApi
    • fromURL (urls As String) As Tags
    • fromVideoArray (videoArray() As Byte) As Tags
    Permissions:
    • android.permission.ACCESS_NETWORK_STATE
    • android.permission.INTERNET
    Properties:
    • Instance As Tags [write only]

In this example the following image is scanned... eiffelturm.jpg


B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim onyx As OnyxTags
End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    onyx.Initialize("Onyx")
    onyx.fromURL("http://www.europa-entdecken.net/fileadmin/eu-entdecken/images/frankreich/eiffelturm.jpg")
    onyx.TagsandProbability
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Onyx_onComplete(tags As List)
    For i = 0 To tags.Size -1
        Log("Tag: "&tags.Get(i))
    Next
End Sub

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
lib:Raising.. onyx_oncomplete()
Tag: architecture-0.9931149482727051
Tag: travel-0.9882233142852783
Tag: no person-0.9877809882164001
Tag: tower-0.9698358774185181
Tag: outdoors-0.9656822681427002
Tag: sky-0.9615469574928284
Tag: landmark-0.9527928829193115
Tag: monument-0.9508309364318848
Tag: city-0.9461989402770996
Tag: tourism-0.9448901414871216
Tag: building-0.9446797370910645
Tag: old-0.9224209785461426
Tag: tallest-0.903542160987854
Tag: famous-0.8974774479866028
Tag: capital-0.8950203657150269
Tag: ancient-0.8797848224639893
Tag: sightseeing-0.8765351176261902
Tag: tourist-0.8734467625617981
Tag: traditional-0.8710891008377075
Tag: historic-0.8630752563476563
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
 

Attachments

  • OnyxEx.zip
    6.7 KB · Views: 408
  • OnyxV1.0.zip
    215.3 KB · Views: 398
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
It's really nice but can I scan a local image?
 

susu

Well-Known Member
Licensed User
Longtime User
If there's no internet connection, this error occurred:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: com.hanuor.onyx.exceptionhandler.ClarifaiException: IOException
at com.hanuor.onyx.helper.CredentialManager.getCredential(CredentialManager.java:70)
at com.hanuor.onyx.helper.ClarifaiRequester.executeOnce(ClarifaiRequester.java:61)
at com.hanuor.onyx.helper.ClarifaiRequester.execute(ClarifaiRequester.java:40)
at com.hanuor.onyx.helper.ClarifaiClient.recognize(ClarifaiClient.java:87)
at com.hanuor.onyx.hub.Tags$3.doInBackground(Tags.java:122)
at com.hanuor.onyx.hub.Tags$3.doInBackground(Tags.java:117)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more
Caused by: java.net.UnknownHostException: Unable to resolve host "api.clarifai.com": Unknown error
at java.net.InetAddress.lookupHostByName(InetAddress.java:435)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:255)
at java.net.InetAddress.getAllByName(InetAddress.java:218)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:310)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:249)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:397)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:229)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
at com.hanuor.onyx.helper.CredentialManager.getCredential(CredentialManager.java:48)
... 11 more
 

freedom2000

Well-Known Member
Licensed User
Longtime User
HI,

I have tried this library --> fantastic job :)

Here are 2 tests with my cat, with non easy pictures :
cat.jpg


and even less easy :
cat2.jpg


I must say that I am impressed by the accuracy of the returned tags :

B4X:
lib:Raising.. onyx_oncomplete() >>>>> first image
Tag: cat-0.9843883514404297
Tag: mammal-0.9824749231338501
Tag: pet-0.9763637781143188
Tag: cute-0.9732187986373901
Tag: animal-0.9653147459030151
Tag: domestic-0.959976851940155
Tag: no person-0.9498270153999329
Tag: one-0.9343335628509521
Tag: family-0.9201111793518066
Tag: portrait-0.9160491824150085
Tag: kitten-0.909244179725647
Tag: sit-0.9080994129180908
Tag: fur-0.9052782654762268
Tag: sleep-0.900326669216156
Tag: little-0.90019690990448
Tag: looking-0.8975399732589722
Tag: eye-0.8563491106033325
Tag: young-0.8389911651611328
Tag: downy-0.8171677589416504
Tag: funny-0.8079372048377991

lib:Raising.. onyx_oncomplete() >>>>> second image
Tag: sleep-0.9827830791473389
Tag: mammal-0.9814931154251099
Tag: fur-0.9796252250671387
Tag: downy-0.9748929738998413
Tag: no person-0.9641279578208923
Tag: animal-0.9632241725921631
Tag: cover-0.9560747146606445
Tag: cat-0.9497789740562439
Tag: pet-0.948310375213623
Tag: bed-0.9481406211853027
Tag: one-0.9392414093017578
Tag: domestic-0.9365823268890381
Tag: pillow-0.9315234422683716
Tag: wear-0.9257190227508545
Tag: family-0.9140724539756775
Tag: cute-0.9100362062454224
Tag: furniture-0.9068081974983215
Tag: bedroom-0.8855085968971252
Tag: dog-0.8806296586990356
Tag: portrait-0.8717036247253418

Thank you for this lib !
 

freedom2000

Well-Known Member
Licensed User
Longtime User
For the above example I have uploaded my cat on my web site.
But I really don't know how to pass a Url to access an image on my android phone...
Has anyone succeeded in doing this ?

Basicaly my idea would be to try to set up a simple cat feeder like this one
(But only for fun of course, no commercial wish).
I imagine opening the cover plate if the cat is recognized + some other "efeatures"
 

freedom2000

Well-Known Member
Licensed User
Longtime User
I have tried to call onyx.fromVideoArray after having taken an image with CameraEx class
I wanted to pass the BytesArray but it failed : code runs but Onyx never answered :(

B4X:
Sub Camera1_PictureTaken (Data() As Byte)
 
    onyx.Initialize("Onyx")
    onyx.fromVideoArray(Data)
    onyx.TagsandProbability
End Sub
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Finally I have found a workaround to upload my cat from my phone...

I use httpServer library.
I have been obliged to configure my router to access my Android server, but it works, you can take a picture of your cat and give it to Onyx :)

images
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi,

Just for fun I have coded a small App which is a merge of HttpServer, CameraEx and Onyx lib.

Here is the result and the source code :)

Thanks again @DonManfred for this impressive lib

onyx.jpg


It should compile... Just change the IP Address of your router

B4X:
ub Camera1_PictureTaken (Data() As Byte)
    Dim filename As String = "1.jpg"
    Dim dir As String = File.DirInternalCache
   
    camEx.SavePictureToFile(Data, dir, filename)
    camEx.StartPreview 'restart preview
   
    'send a broadcast intent to the media scanner to force it to scan the saved file.
    Dim Phone As Phone
    Dim i As Intent
    i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
        "file://" & File.Combine(dir, filename))
    Phone.SendBroadcastIntent(i)
    'ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename), True)
    EdtOnyx.Text = ""
    onyx.Initialize("Onyx")
    onyx.fromURL("http://YOUR_Address:5555/1.jpg")
    onyx.TagsandProbability
End Sub
 

Attachments

  • Onyx.zip
    14 KB · Views: 326

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends,

it not functioned. As is in this threat wrote - because of pricing and licencing. We must have API key, its true? But where I must put this key?

Best regards
p4ppc
 
Top