mirror of
https://github.com/wassname/IndicoIo-python.git
synced 2026-09-12 12:02:15 +08:00
ADD: facial localization api
This commit is contained in:
@@ -19,6 +19,7 @@ from indicoio.text.keywords import keywords
|
|||||||
from indicoio.text.ner import named_entities
|
from indicoio.text.ner import named_entities
|
||||||
from indicoio.images.fer import fer
|
from indicoio.images.fer import fer
|
||||||
from indicoio.images.features import facial_features
|
from indicoio.images.features import facial_features
|
||||||
|
from indicoio.images.faciallocalization import facial_localization
|
||||||
from indicoio.images.features import image_features
|
from indicoio.images.features import image_features
|
||||||
from indicoio.images.filtering import content_filtering
|
from indicoio.images.filtering import content_filtering
|
||||||
from indicoio.utils.multi import predict_image, predict_text
|
from indicoio.utils.multi import predict_image, predict_text
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
from indicoio.utils.image import image_preprocess
|
||||||
|
from indicoio.utils.api import api_handler
|
||||||
|
|
||||||
|
|
||||||
|
def facial_localization(image, cloud=None, batch=False, api_key=None, **kwargs):
|
||||||
|
"""
|
||||||
|
Given an image, returns a list of faces found within the image.
|
||||||
|
For each face, we return a dictionary containing the upper left corner and lower right corner.
|
||||||
|
If crop is True, the cropped face is included in the dictionary.
|
||||||
|
Input should be in a numpy ndarray or a filename.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
>>> from indicoio import facial_localization
|
||||||
|
>>> import numpy as np
|
||||||
|
>>> img = np.zeros([image of a face])
|
||||||
|
>>> faces = facial_localization(img)
|
||||||
|
>>> len(faces)
|
||||||
|
1
|
||||||
|
|
||||||
|
:param image: The image to be analyzed.
|
||||||
|
:type image: filepath or ndarray
|
||||||
|
:rtype: List of faces (dict) found.
|
||||||
|
"""
|
||||||
|
image = image_preprocess(image, batch=batch)
|
||||||
|
url_params = {"batch": batch, "api_key": api_key}
|
||||||
|
return api_handler(image, cloud=cloud, api="faciallocalization", url_params=url_params, **kwargs)
|
||||||
@@ -16,7 +16,7 @@ def facial_features(image, cloud=None, batch=False, api_key=None, **kwargs):
|
|||||||
|
|
||||||
>>> from indicoio import facial_features
|
>>> from indicoio import facial_features
|
||||||
>>> import numpy as np
|
>>> import numpy as np
|
||||||
>>> face = np.zeros((48,48)).tolist()
|
>>> face = np.zeros((48,48))
|
||||||
>>> features = facial_features(face)
|
>>> features = facial_features(face)
|
||||||
>>> len(features)
|
>>> len(features)
|
||||||
48
|
48
|
||||||
@@ -25,7 +25,7 @@ def facial_features(image, cloud=None, batch=False, api_key=None, **kwargs):
|
|||||||
:type image: list of lists
|
:type image: list of lists
|
||||||
:rtype: List containing feature responses
|
:rtype: List containing feature responses
|
||||||
"""
|
"""
|
||||||
image = image_preprocess(image, batch=batch)
|
image = image_preprocess(image, batch=batch, size=(48,48))
|
||||||
url_params = {"batch": batch, "api_key": api_key}
|
url_params = {"batch": batch, "api_key": api_key}
|
||||||
return api_handler(image, cloud=cloud, api="facialfeatures", url_params=url_params, **kwargs)
|
return api_handler(image, cloud=cloud, api="facialfeatures", url_params=url_params, **kwargs)
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,6 @@ def content_filtering(image, cloud=None, batch=False, api_key=None, **kwargs):
|
|||||||
:type image: list of lists
|
:type image: list of lists
|
||||||
:rtype: float of nsfwness
|
:rtype: float of nsfwness
|
||||||
"""
|
"""
|
||||||
image = image_preprocess(image, batch=batch, size=None, min_axis=128)
|
image = image_preprocess(image, batch=batch, min_axis=128)
|
||||||
url_params = {"batch": batch, "api_key": api_key}
|
url_params = {"batch": batch, "api_key": api_key}
|
||||||
return api_handler(image, cloud=cloud, api="contentfiltering", url_params=url_params, **kwargs)
|
return api_handler(image, cloud=cloud, api="contentfiltering", url_params=url_params, **kwargs)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from indicoio.utils.errors import IndicoError, DataStructureException
|
|||||||
|
|
||||||
B64_PATTERN = re.compile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)")
|
B64_PATTERN = re.compile("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)")
|
||||||
|
|
||||||
def image_preprocess(image, size=(48,48), min_axis=None, batch=False):
|
def image_preprocess(image, size=None, min_axis=None, batch=False):
|
||||||
"""
|
"""
|
||||||
Takes an image and prepares it for sending to the api including
|
Takes an image and prepares it for sending to the api including
|
||||||
resizing and image data/structure standardizing.
|
resizing and image data/structure standardizing.
|
||||||
|
|||||||
+19
-1
@@ -6,7 +6,7 @@ from requests import ConnectionError
|
|||||||
from nose.plugins.skip import Skip, SkipTest
|
from nose.plugins.skip import Skip, SkipTest
|
||||||
|
|
||||||
from indicoio import config
|
from indicoio import config
|
||||||
from indicoio import political, sentiment, fer, facial_features, content_filtering, language, image_features, text_tags
|
from indicoio import political, sentiment, fer, facial_features, facial_localization, 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_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 batch_language, batch_image_features, batch_text_tags
|
||||||
from indicoio import keywords, batch_keywords
|
from indicoio import keywords, batch_keywords
|
||||||
@@ -355,6 +355,24 @@ class FullAPIRun(unittest.TestCase):
|
|||||||
self.assertTrue(isinstance(response, dict))
|
self.assertTrue(isinstance(response, dict))
|
||||||
self.assertEqual(fer_set, set(response.keys()))
|
self.assertEqual(fer_set, set(response.keys()))
|
||||||
|
|
||||||
|
def test_facial_localization(self):
|
||||||
|
test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
|
||||||
|
res = facial_localization(test_face)[0]
|
||||||
|
self.assertTrue(res["top_left_corner"][0] < res["bottom_right_corner"][0])
|
||||||
|
self.assertTrue(res["top_left_corner"][1] < res["bottom_right_corner"][1])
|
||||||
|
|
||||||
|
def test_facial_localization_sensitivity(self):
|
||||||
|
test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
|
||||||
|
low_sens = facial_localization(test_face, sensitivity=0.1)
|
||||||
|
high_sens = facial_localization(test_face, sensitivity=0.9)
|
||||||
|
self.assertEqual(len(low_sens), 1)
|
||||||
|
self.assertTrue(len(high_sens) > 1)
|
||||||
|
|
||||||
|
def test_facial_localization_crop(self):
|
||||||
|
test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
|
||||||
|
res = facial_localization(test_face, crop=True)[0]
|
||||||
|
self.assertTrue(res.get("image"))
|
||||||
|
|
||||||
def test_safe_content_filtering(self):
|
def test_safe_content_filtering(self):
|
||||||
test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
|
test_face = os.path.normpath(os.path.join(DIR, "data/happy.png"))
|
||||||
response = content_filtering(test_face)
|
response = content_filtering(test_face)
|
||||||
|
|||||||
Reference in New Issue
Block a user