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

Added FaceMatcher.toJSON()/.fromJSON() and LabeledFaceDescriptors.toJSON()/.fromJSON(), with unit tests #397

Merged
merged 8 commits into from
Aug 31, 2019

Conversation

jondewoo
Copy link
Contributor

In response to #231.

@jondewoo
Copy link
Contributor Author

I went back a bit a realized that the toJSON() was not necessary (might as well call JSON.stringify()) as long as fromJSON() did the job right.

@jondewoo jondewoo changed the title Added FaceMatcher.toJSON() & FaceMatcher.fromJSON(), with unit tests Added FaceMatcher.fromJSON(), with unit tests Aug 28, 2019
Added FaceMatcher.fromPOJO()
Updated FaceMatcher tests and added tests for LabeledFaceDescriptors
@jondewoo jondewoo changed the title Added FaceMatcher.fromJSON(), with unit tests Added FaceMatcher.fromJSON()/.fromPOJO() and LabeledFaceDescriptors.fromJSON()/.fromPOJO(), with unit tests Aug 28, 2019
@justadudewhohacks
Copy link
Owner

Hi thanks for your PR! Some minor remarks:

  1. Please don't use JSON.stringify to serialize the the objects, since it will simply serialize all private variables. Instead build the JSON manually and you can also cast the descriptors from Float32Array to plain javascript arrays (Array.from(descriptor)). The output should look like this instead: '{"label":"foo","descriptors":[1, 2, 3, 4, 5, 6]}'.

  2. Could you please exclude the build and dist directories from the PR, by reverting the changes.

  3. Can you merge the latest master into your branch? I fixed the travis build script.

@jondewoo jondewoo changed the title Added FaceMatcher.fromJSON()/.fromPOJO() and LabeledFaceDescriptors.fromJSON()/.fromPOJO(), with unit tests Added FaceMatcher.toJSON()/.fromJSON()/.fromPOJO() and LabeledFaceDescriptors.toJSON()/.fromJSON()/.fromPOJO(), with unit tests Aug 30, 2019
@jondewoo
Copy link
Contributor Author

Hi @justadudewhohacks,

Updated the PR as per your requirements.

@justadudewhohacks
Copy link
Owner

Thanks for your effort and thinking of it I am sorry for my bad example above.

Actually your toJSON methods returns a json string, but I think it is better to return a json object here:

{ 
  label: ld.label, 
  descriptors: [
    ld.descriptors.map(d => Array.from(d))}
  ]
}

Then you can remove the fromJSON methods and rename fromPOJO to fromJSON, JSON is the same as a "POJO".

Right now it also looks like [${Array.from(d)}] is also returning an array inside of an array or am I missing something here?

Again, sorry for the inconvenience.

@jondewoo
Copy link
Contributor Author

Gotcha. Coming from C#, Swift, ... toJSON usually means returning a JSON string, but it all makes sense now, thanks to your explanation.

As for [${Array.from(d)}], console.log(${Array.from(d)}) will output something like 1,2,3, hence the square brackets.

Made .fromJSON() implementations return a POJO
Updated the unit tests
@powerdot
Copy link

Hello!
Cant find how to use .toJSON() .fromJSON()
Can you post any example or reply a sample here? I have actual version, checked it twice;)

Thank you!

Снимок экрана 2019-08-31 в 03 17 36

@justadudewhohacks
Copy link
Owner

Great, now it looks good!

@powerdot the change isn't merged yet. If you checked out the branch, the changes have to be built via npm run build.

@justadudewhohacks justadudewhohacks changed the title Added FaceMatcher.toJSON()/.fromJSON()/.fromPOJO() and LabeledFaceDescriptors.toJSON()/.fromJSON()/.fromPOJO(), with unit tests Added FaceMatcher.toJSON()/.fromJSON() and LabeledFaceDescriptors.toJSON()/.fromJSON(), with unit tests Aug 31, 2019
@justadudewhohacks justadudewhohacks merged commit 0f2fda5 into justadudewhohacks:master Aug 31, 2019
@powerdot
Copy link

Vincent and Jonathan, thank you! Works great. Successful compiled.

For community:
var labeledFaceDescriptorsJson = labeledFaceDescriptors.map(x=>x.toJSON())
and (not tested, but i think it works actually like this one)
var labeledFaceDescriptors = labeledFaceDescriptorsJson.map( x=>faceapi.LabeledFaceDescriptors.fromJSON(x) );

@sandyaveas
Copy link

@powerdot Thanks, this works.

This pull request was closed.
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 this pull request may close these issues.

4 participants