mirror of
https://github.com/wassname/IndicoIo-python.git
synced 2026-06-27 16:10:34 +08:00
Merge pull request #87 from IndicoDataSolutions/contentFiltering
added content filtering
This commit is contained in:
@@ -18,6 +18,7 @@ from indicoio.text.ner import named_entities
|
||||
from indicoio.images.fer import fer
|
||||
from indicoio.images.features import facial_features
|
||||
from indicoio.images.features import image_features
|
||||
from indicoio.images.filtering import content_filtering
|
||||
from indicoio.utils.multi import predict_image, predict_text
|
||||
|
||||
from indicoio.config import API_NAMES
|
||||
|
||||
+2
-1
@@ -58,7 +58,8 @@ TEXT_APIS = [
|
||||
IMAGE_APIS = [
|
||||
'fer',
|
||||
'facial_features',
|
||||
'image_features'
|
||||
'image_features',
|
||||
'content_filtering'
|
||||
]
|
||||
|
||||
API_NAMES = IMAGE_APIS + TEXT_APIS + ["predict_text", "predict_image"]
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import requests
|
||||
|
||||
from indicoio.utils.api import api_handler
|
||||
from indicoio.utils.image import image_preprocess
|
||||
import indicoio.config as config
|
||||
|
||||
def content_filtering(image, cloud=None, batch=False, api_key=None, **kwargs):
|
||||
"""
|
||||
Given a grayscale input image, returns how obcene the image is.
|
||||
Input should be in a list of list format.
|
||||
|
||||
Example usage:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> from indicoio import content_filtering
|
||||
>>> import numpy as np
|
||||
>>> face = np.zeros((48,48)).tolist()
|
||||
>>> res = content_filtering(face)
|
||||
>>> res
|
||||
.056
|
||||
|
||||
:param image: The image to be analyzed.
|
||||
:type image: list of lists
|
||||
:rtype: float of nsfwness
|
||||
"""
|
||||
image = image_preprocess(image, batch=batch, size=None)
|
||||
url_params = {"batch": batch, "api_key": api_key}
|
||||
return api_handler(image, cloud=cloud, api="contentfiltering", url_params=url_params, **kwargs)
|
||||
@@ -48,7 +48,8 @@ def image_preprocess(image, size=(48,48), batch=False):
|
||||
raise IndicoError("Image must be a filepath, base64 encoded string, or a numpy array")
|
||||
|
||||
# image resizing
|
||||
out_image = out_image.resize(size)
|
||||
if size:
|
||||
out_image = out_image.resize(size)
|
||||
|
||||
# convert to base64
|
||||
temp_output = StringIO.StringIO()
|
||||
|
||||
+13
-2
@@ -6,8 +6,8 @@ from requests import ConnectionError
|
||||
from nose.plugins.skip import Skip, SkipTest
|
||||
|
||||
from indicoio import config
|
||||
from indicoio import political, sentiment, fer, facial_features, language, image_features, text_tags
|
||||
from indicoio import batch_political, batch_sentiment, batch_fer, batch_facial_features
|
||||
from indicoio import political, sentiment, fer, facial_features, content_filtering, language, image_features, text_tags
|
||||
from indicoio import batch_political, batch_sentiment, batch_fer, batch_content_filtering, batch_facial_features
|
||||
from indicoio import batch_language, batch_image_features, batch_text_tags
|
||||
from indicoio import keywords, batch_keywords
|
||||
from indicoio import sentiment_hq, batch_sentiment_hq
|
||||
@@ -64,6 +64,12 @@ class BatchAPIRun(unittest.TestCase):
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], dict))
|
||||
|
||||
def test_batch_content_filtering(self):
|
||||
test_data = [generate_array((48,48))]
|
||||
response = batch_content_filtering(test_data, api_key=self.api_key)
|
||||
self.assertTrue(isinstance(response, list))
|
||||
self.assertTrue(isinstance(response[0], float))
|
||||
|
||||
def test_batch_fer_bad_b64(self):
|
||||
test_data = ["$bad#FI jeaf9(#0"]
|
||||
self.assertRaises(IndicoError, batch_fer, test_data, api_key=self.api_key)
|
||||
@@ -324,6 +330,11 @@ class FullAPIRun(unittest.TestCase):
|
||||
self.assertTrue(isinstance(response, dict))
|
||||
self.assertEqual(fer_set, set(response.keys()))
|
||||
|
||||
def test_safe_content_filtering(self):
|
||||
test_face = self.load_image("data/happy.png", as_grey=True)
|
||||
response = content_filtering(test_face)
|
||||
self.assertTrue(response < 0.5)
|
||||
|
||||
def test_good_facial_features(self):
|
||||
test_face = generate_array((48,48))
|
||||
response = facial_features(test_face)
|
||||
|
||||
Reference in New Issue
Block a user