mirror of
https://github.com/wassname/IndicoIo-python.git
synced 2026-06-27 16:10:34 +08:00
ADD: ImageRecognition
This commit is contained in:
@@ -18,9 +18,9 @@ from indicoio.text.tagging import text_tags
|
||||
from indicoio.text.keywords import keywords
|
||||
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 facial_features, image_features
|
||||
from indicoio.images.faciallocalization import facial_localization
|
||||
from indicoio.images.features import image_features
|
||||
from indicoio.images.recognition import image_recognition
|
||||
from indicoio.images.filtering import content_filtering
|
||||
from indicoio.utils.multi import analyze_image, analyze_text, intersections
|
||||
|
||||
|
||||
+2
-1
@@ -60,11 +60,12 @@ IMAGE_APIS = [
|
||||
'fer',
|
||||
'facial_features',
|
||||
'image_features',
|
||||
'image_recognition',
|
||||
'content_filtering'
|
||||
]
|
||||
|
||||
OTHER_APIS = [
|
||||
"analyze_text",
|
||||
"analyze_text",
|
||||
"analyze_image",
|
||||
"intersections"
|
||||
]
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import requests
|
||||
|
||||
from indicoio.utils.image import image_preprocess
|
||||
from indicoio.utils.api import api_handler
|
||||
|
||||
def image_recognition(image, cloud=None, batch=False, api_key=None, version=None, **kwargs):
|
||||
"""
|
||||
Given an input image, returns a dictionary of image classifications with associated scores
|
||||
|
||||
* Input can be either grayscale or rgb color and should either be a numpy array or nested list format.
|
||||
* Input data should be either uint8 0-255 range values or floating point between 0 and 1.
|
||||
* Large images (i.e. 1024x768+) are much bigger than needed, minaxis resizing will be done internally to 144 if needed.
|
||||
* For ideal performance, images should be square aspect ratio but non-square aspect ratios are supported as well.
|
||||
|
||||
Example usage:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> from indicoio import image_recognition
|
||||
>>> features = image_recognition(<filename>)
|
||||
|
||||
:param image: The image to be analyzed.
|
||||
:type image: str
|
||||
:rtype: dict containing classifications
|
||||
"""
|
||||
image = image_preprocess(image, size=144, min_axis=True, batch=batch)
|
||||
url_params = {"batch": batch, "api_key": api_key, "version": version}
|
||||
return api_handler(image, cloud=cloud, api="imagerecognition", url_params=url_params, **kwargs)
|
||||
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import unittest, os
|
||||
|
||||
from indicoio import config
|
||||
from indicoio import image_recognition
|
||||
|
||||
DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
class TestVersioning(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.api_key = config.api_key
|
||||
|
||||
def test_single_image_recognition(self):
|
||||
test_data = os.path.normpath(os.path.join(DIR, "data", "fear.png"))
|
||||
response = image_recognition(test_data, api_key = self.api_key, top_n=3)
|
||||
self.assertIsInstance(response, dict)
|
||||
self.assertEqual(len(response), 3)
|
||||
self.assertIsInstance(response.values()[0], float)
|
||||
|
||||
def test_batch_image_recognition(self):
|
||||
test_data = os.path.normpath(os.path.join(DIR, "data", "fear.png"))
|
||||
response = image_recognition([test_data, test_data], api_key = self.api_key, top_n=3)
|
||||
self.assertIsInstance(response, list)
|
||||
self.assertIsInstance(response[0], dict)
|
||||
self.assertEqual(len(response[0]), 3)
|
||||
self.assertIsInstance(response[0].values()[0], float)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -209,13 +209,6 @@ class FullAPIRun(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.api_key = config.api_key
|
||||
|
||||
def load_image(self, relpath, as_grey=False):
|
||||
im = Image.open(os.path.normpath(os.path.join(DIR, relpath))).convert('L');
|
||||
pixels = list(im.getdata())
|
||||
width, height = im.size
|
||||
pixels = [pixels[i * width:(i + 1) * width] for i in xrange(height)]
|
||||
return pixels
|
||||
|
||||
def check_range(self, _list, minimum=0.9, maximum=0.1, span=0.5):
|
||||
vector = list(flatten(_list))
|
||||
_max = max(vector)
|
||||
@@ -253,7 +246,7 @@ class FullAPIRun(unittest.TestCase):
|
||||
assert v >= .1
|
||||
|
||||
def test_keywords_language_detect(self):
|
||||
text = "La semaine suivante, il remporte sa premiere victoire, dans la descente de Val Gardena en Italie, près de cinq ans après la dernière victoire en Coupe du monde d'un Français dans cette discipline, avec le succès de Nicolas Burtin à Kvitfjell"
|
||||
text = "il a remporté sa première victoire dans la descente de Val Gardena en Italie"
|
||||
words = set(text.lower().split())
|
||||
|
||||
results = keywords(text, language = 'detect')
|
||||
@@ -269,7 +262,7 @@ class FullAPIRun(unittest.TestCase):
|
||||
assert v >= .1
|
||||
|
||||
def test_keywords_language(self):
|
||||
text = "La semaine suivante, il remporte sa premiere victoire, dans la descente de Val Gardena en Italie, près de cinq ans après la dernière victoire en Coupe du monde d'un Français dans cette discipline, avec le succès de Nicolas Burtin à Kvitfjell"
|
||||
text = "il a remporté sa première victoire dans la descente de Val Gardena en Italie"
|
||||
words = set(text.lower().split())
|
||||
|
||||
results = keywords(text, language = 'French')
|
||||
|
||||
Reference in New Issue
Block a user