Search in sources :

Example 1 with Face

use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face in project jbpm-work-items by kiegroup.

the class DetectFacesWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    Document detectionImage = (Document) workItem.getParameter("ImageToDetect");
    Map<String, Object> widResults = new HashMap<String, Object>();
    if (detectionImage != null) {
        try {
            VisualRecognition service = auth.getService(apiKey);
            ByteArrayInputStream imageStream = new ByteArrayInputStream(detectionImage.getContent());
            DetectFacesOptions detectFacesOptions = new DetectFacesOptions.Builder().imagesFile(imageStream).build();
            DetectedFaces result = service.detectFaces(detectFacesOptions).execute();
            if (result == null || result.getImages() == null || result.getImages().size() < 1) {
                logger.error("Unable to detect faces on provided image.");
                workItemManager.abortWorkItem(workItem.getId());
            } else {
                List<FaceDetectionResult> resultList = new ArrayList<>();
                for (ImageWithFaces imageWithFaces : result.getImages()) {
                    for (Face face : imageWithFaces.getFaces()) {
                        resultList.add(new FaceDetectionResult(imageWithFaces, face));
                    }
                }
                widResults.put(RESULT_VALUE, resultList);
                workItemManager.completeWorkItem(workItem.getId(), widResults);
            }
        } catch (Exception e) {
            handleException(e);
        }
    } else {
        logger.error("Missing image for face detection.");
        throw new IllegalArgumentException("Missing image for face detection.");
    }
}
Also used : FaceDetectionResult(org.jbpm.process.workitem.ibm.watson.result.FaceDetectionResult) HashMap(java.util.HashMap) VisualRecognition(com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition) ArrayList(java.util.ArrayList) DetectedFaces(com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectedFaces) Document(org.jbpm.document.Document) ImageWithFaces(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ImageWithFaces) DetectFacesOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectFacesOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) Face(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face)

Example 2 with Face

use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face in project java-sdk by watson-developer-cloud.

the class VisualRecognition method detectFaces.

/**
 * Detect faces in images.
 *
 * **Important:** On April 2, 2018, the identity information in the response to calls to the Face model was removed.
 * The identity information refers to the `name` of the person, `score`, and `type_hierarchy` knowledge graph. For
 * details about the enhanced Face model, see the [Release
 * notes](https://console.bluemix.net/docs/services/visual-recognition/release-notes.html#2april2018). Analyze and get
 * data about faces in images. Responses can include estimated age and gender. This feature uses a built-in model, so
 * no training is necessary. The Detect faces method does not support general biometric facial recognition. Supported
 * image formats include .gif, .jpg, .png, and .tif. The maximum image size is 10 MB. The minimum recommended pixel
 * density is 32X32 pixels per inch.
 *
 * @param detectFacesOptions the {@link DetectFacesOptions} containing the options for the call
 * @return a {@link ServiceCall} with a response type of {@link DetectedFaces}
 */
public ServiceCall<DetectedFaces> detectFaces(DetectFacesOptions detectFacesOptions) {
    Validator.notNull(detectFacesOptions, "detectFacesOptions cannot be null");
    Validator.isTrue((detectFacesOptions.imagesFile() != null) || (detectFacesOptions.url() != null) || (detectFacesOptions.parameters() != null), "At least one of imagesFile, url, or parameters must be supplied.");
    String[] pathSegments = { "v3/detect_faces" };
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments));
    builder.query(VERSION, versionDate);
    MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
    multipartBuilder.setType(MultipartBody.FORM);
    if (detectFacesOptions.imagesFile() != null) {
        RequestBody imagesFileBody = RequestUtils.inputStreamBody(detectFacesOptions.imagesFile(), detectFacesOptions.imagesFileContentType());
        multipartBuilder.addFormDataPart("images_file", detectFacesOptions.imagesFilename(), imagesFileBody);
    }
    if (detectFacesOptions.parameters() != null) {
        multipartBuilder.addFormDataPart("parameters", detectFacesOptions.parameters());
    }
    if (detectFacesOptions.url() != null) {
        multipartBuilder.addFormDataPart("url", detectFacesOptions.url());
    }
    builder.body(multipartBuilder.build());
    return createServiceCall(builder.build(), ResponseConverterUtils.getObject(DetectedFaces.class));
}
Also used : RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) MultipartBody(okhttp3.MultipartBody) RequestBuilder(com.ibm.watson.developer_cloud.http.RequestBuilder) DetectedFaces(com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectedFaces) RequestBody(okhttp3.RequestBody)

Example 3 with Face

use of com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face in project jbpm-work-items by kiegroup.

the class WatsonWorkitemHandlerTest method setUp.

// @Mock
// Face recognitionFace;
@Before
public void setUp() {
    // image classification
    ClassResult classResultObj = new ClassResult();
    classResultObj.setClassName("testClassName");
    classResultObj.setScore(new Float(1));
    classResultObj.setTypeHierarchy("testTypeHierarchy");
    List<ClassifiedImage> classifiedImageList = new ArrayList<>();
    classifiedImageList.add(associationClassifiedImage);
    List<ClassifierResult> classifierResults = new ArrayList<>();
    classifierResults.add(associationClassifierResult);
    List<ClassResult> classResults = new ArrayList<>();
    classResults.add(classResultObj);
    when(associationService.classify(any(ClassifyOptions.class))).thenReturn(associationServiceCall);
    when(associationServiceCall.execute()).thenReturn(assoiationClassifiedImages);
    when(assoiationClassifiedImages.getImages()).thenReturn(classifiedImageList);
    when(associationClassifiedImage.getClassifiers()).thenReturn(classifierResults);
    when(associationClassifierResult.getClasses()).thenReturn(classResults);
    when(associationClassifierResult.getClassifierId()).thenReturn("testClassifierId");
    // face detection
    List<ImageWithFaces> recognitionImageList = new ArrayList<>();
    recognitionImageList.add(recognitionImageWithFaces);
    FaceAge recognitionFaceAge = new FaceAge();
    recognitionFaceAge.setMin(20);
    recognitionFaceAge.setMax(35);
    FaceGender recognitionFaceGender = new FaceGender();
    recognitionFaceGender.setGender("male");
    FaceIdentity recognitionFaceIdentity = new FaceIdentity();
    recognitionFaceIdentity.setName("testPerson");
    Face recognitionFace = new Face();
    recognitionFace.setAge(recognitionFaceAge);
    recognitionFace.setGender(recognitionFaceGender);
    recognitionFace.setIdentity(recognitionFaceIdentity);
    List<Face> recognitionFaceList = new ArrayList<>();
    recognitionFaceList.add(recognitionFace);
    when(recognitionService.detectFaces(any(DetectFacesOptions.class))).thenReturn(recognitionServiceCall);
    when(recognitionServiceCall.execute()).thenReturn(recognitionDetectFaces);
    when(recognitionDetectFaces.getImages()).thenReturn(recognitionImageList);
    when(recognitionImageWithFaces.getFaces()).thenReturn(recognitionFaceList);
    when(recognitionImageWithFaces.getImage()).thenReturn("testImage");
}
Also used : ClassifyOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions) ArrayList(java.util.ArrayList) FaceGender(com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceGender) ImageWithFaces(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ImageWithFaces) DetectFacesOptions(com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectFacesOptions) ClassifiedImage(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage) ClassifierResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult) Face(com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face) ClassResult(com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult) FaceAge(com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceAge) FaceIdentity(com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceIdentity) Before(org.junit.Before)

Aggregations

DetectFacesOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectFacesOptions)2 DetectedFaces (com.ibm.watson.developer_cloud.visual_recognition.v3.model.DetectedFaces)2 Face (com.ibm.watson.developer_cloud.visual_recognition.v3.model.Face)2 ImageWithFaces (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ImageWithFaces)2 ArrayList (java.util.ArrayList)2 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)1 VisualRecognition (com.ibm.watson.developer_cloud.visual_recognition.v3.VisualRecognition)1 ClassResult (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassResult)1 ClassifiedImage (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifiedImage)1 ClassifierResult (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifierResult)1 ClassifyOptions (com.ibm.watson.developer_cloud.visual_recognition.v3.model.ClassifyOptions)1 FaceAge (com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceAge)1 FaceGender (com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceGender)1 FaceIdentity (com.ibm.watson.developer_cloud.visual_recognition.v3.model.FaceIdentity)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashMap (java.util.HashMap)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1 Document (org.jbpm.document.Document)1 FaceDetectionResult (org.jbpm.process.workitem.ibm.watson.result.FaceDetectionResult)1