notAI-tech / NudeNet

Lightweight nudity detection
https://nudenet.notai.tech/
GNU Affero General Public License v3.0
1.76k stars 342 forks source link

Detector docker server should return full JSON #80

Closed watzon closed 3 years ago

watzon commented 3 years ago

Couldn't find the repo for the docker containers, if there is one. The problem is pretty simple though. Currently the response returned from the detector API is formatted as:

{
    "prediction": String,
    "success": Bool
}

Ideally the prediction should be returned as a JSON object for easier parsing. It would also be nice if there was a public repo for the docker containers.

bedapudi6788 commented 3 years ago

Couldn't find the repo for the docker containers,

readme clearly shows the docker hub repo name in the following command docker run -it -p8080:8080 notaitech/nudenet:classifier. The code to build the images locally (if needed) is present at https://github.com/notAI-tech/NudeNet/tree/v2/fastDeploy_recipes

Currently the response returned from the detector API is formatted as:

JSON IS A STRING!! you have to do json.loads() in python to get a python object from the json string.

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax.

watzon commented 3 years ago

JSON IS A STRING!!

The hostility is not appreciated, and this comment is just ignorant. Even in the case of Python this takes 2 steps to decode where it could easily take 1 if the object were being returned correctly. In other languages, like the one I'm using which is statically typed, it's even more involved to deserialize an object like this.

I've been a web developer for 11 years. I know very well what JSON is.

Edit: Also the prediction string isn't valid JSON. JSON uses double quotes, so attempting to parse that in most JSON compliant parsers would fail.

bedapudi6788 commented 3 years ago

@watzon

The hostility is not appreciated, and this comment is just ignorant.

You are correct. I acted like an idiot not understanding the issue you were trying to point out. Sorry for being an ass.

Even in the case of Python this takes 2 steps to decode where it could easily take 1 if the object were being returned correctly. I

Yupp, I realised the issue from this statement.

I've been a web developer for 11 years. I know very well what JSON is.

Again, sorry! I misunderstood your original post.

The issue is fixed in the latest docker images. Please pull them and test once. It was happening because the score returned from detector is not a json dumpable object and fastDeploy falls back to converting the object into str and dumping in this case.

watzon commented 3 years ago

Appreciate the apology. All is forgiven. Came to give a solution actually, since I tried working on my own server to overcome the issue and stumbled upon what I think the problem was. Still could be useful so I'll drop it here.

Apparently types such as np.float32 aren't serializable by default, so that was causing the serialization to fail and the data to get dumped as a string. I managed to solve this by using singledispatch.

from functools import singledispatch

@singledispatch
def to_serializable(val):
    return str(val)

Very simple and I don't know why Python doesn't just do this by default, but it works. I'll check the latest images and close this in a bit. Thanks!

watzon commented 3 years ago

Much better. Thanks for the fix!