Ejemplo n.º 1
0
def _entity_from_response_type(feature_type, results):
    """Convert a JSON result to an entity type based on the feature.

    :rtype: list
    :returns: List containing any of
              :class:`~google.cloud.vision.entity.EntityAnnotation`,
              :class:`~google.cloud.vision.face.Face`

              or one of

              :class:`~google.cloud.vision.safe_search.SafeSearchAnnotation`,
              :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`.
    """
    detected_objects = []
    if feature_type == _FACE_ANNOTATIONS:
        detected_objects.extend(
            Face.from_api_repr(face) for face in results)
    elif feature_type == _IMAGE_PROPERTIES_ANNOTATION:
        return ImagePropertiesAnnotation.from_api_repr(results)
    elif feature_type == _SAFE_SEARCH_ANNOTATION:
        return SafeSearchAnnotation.from_api_repr(results)
    else:
        for result in results:
            detected_objects.append(EntityAnnotation.from_api_repr(result))
    return detected_objects
Ejemplo n.º 2
0
def _make_faces_from_pb(faces):
    """Create face objects from a protobuf response.

    :type faces:
    :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.FaceAnnotation`
    :param faces: Protobuf instance of ``FaceAnnotation``.

    :rtype: list
    :returns: List of ``Face``.
    """
    return [Face.from_pb(face) for face in faces]
Ejemplo n.º 3
0
    def detect_faces(self, limit=10):
        """Detect faces in image.

        :type limit: int
        :param limit: The number of faces to try and detect.

        :rtype: list
        :returns: List of :class:`~google.cloud.vision.face.Face`.
        """
        faces = []
        face_detection_feature = Feature(FeatureTypes.FACE_DETECTION, limit)
        result = self.client.annotate(self, [face_detection_feature])
        for face_response in result['faceAnnotations']:
            face = Face.from_api_repr(face_response)
            faces.append(face)

        return faces
Ejemplo n.º 4
0
    def detect_faces(self, limit=10):
        """Detect faces in image.

        :type limit: int
        :param limit: The number of faces to try and detect.

        :rtype: list
        :returns: List of :class:`~google.cloud.vision.face.Face`.
        """
        faces = []
        face_detection_feature = Feature(FeatureTypes.FACE_DETECTION, limit)
        result = self.client.annotate(self, [face_detection_feature])
        for face_response in result['faceAnnotations']:
            face = Face.from_api_repr(face_response)
            faces.append(face)

        return faces
Ejemplo n.º 5
0
def _entity_from_response_type(feature_type, results):
    """Convert a JSON result to an entity type based on the feature."""

    detected_objects = []
    feature_key = _REVERSE_TYPES[feature_type]

    if feature_type == _FACE_DETECTION:
        detected_objects.extend(
            Face.from_api_repr(face) for face in results[feature_key])
    elif feature_type == _IMAGE_PROPERTIES:
        detected_objects.append(
            ImagePropertiesAnnotation.from_api_repr(results[feature_key]))
    elif feature_type == _SAFE_SEARCH_DETECTION:
        result = results[feature_key]
        detected_objects.append(SafeSearchAnnotation.from_api_repr(result))
    else:
        for result in results[feature_key]:
            detected_objects.append(EntityAnnotation.from_api_repr(result))
    return detected_objects
Ejemplo n.º 6
0
def _entity_from_response_type(feature_type, results):
    """Convert a JSON result to an entity type based on the feature.

    :rtype: list
    :returns: List containing any of
              :class:`~google.cloud.vision.color.ImagePropertiesAnnotation`,
              :class:`~google.cloud.vision.entity.EntityAnnotation`,
              :class:`~google.cloud.vision.face.Face`,
              :class:`~google.cloud.vision.safe.SafeSearchAnnotation`.
    """
    detected_objects = []
    if feature_type == _FACE_ANNOTATIONS:
        detected_objects.extend(Face.from_api_repr(face) for face in results)
    elif feature_type == _IMAGE_PROPERTIES_ANNOTATION:
        detected_objects.append(ImagePropertiesAnnotation.from_api_repr(results))
    elif feature_type == _SAFE_SEARCH_ANNOTATION:
        detected_objects.append(SafeSearchAnnotation.from_api_repr(results))
    else:
        for result in results:
            detected_objects.append(EntityAnnotation.from_api_repr(result))
    return detected_objects