-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an Image-to-Text action kamelet (#2171)
* Switch to Camel 4.8.0-SNAPSHOT * Create an Image-to-Text action kamelet
- Loading branch information
Showing
7 changed files
with
230 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# --------------------------------------------------------------------------- | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# --------------------------------------------------------------------------- | ||
apiVersion: camel.apache.org/v1 | ||
kind: Kamelet | ||
metadata: | ||
name: djl-image-to-text-action | ||
annotations: | ||
camel.apache.org/kamelet.support.level: "Preview" | ||
camel.apache.org/catalog.version: "4.8.0-SNAPSHOT" | ||
camel.apache.org/kamelet.icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAArCAYAAACO7C3tAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAXEgAAFxIBZ5/SUgAAD7ZJREFUeAHtW1tsXMUZnplz21178SWxg01uTRNKbJVLmjSlhcaBVGoLkVpVG7WhaqW2UiSqICoR+o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X0CgAphf0ICBYY2NjsT79n1JCVqDQZQgIeqJzCx2DoDUD1SW2Cbg39yf/S1yTf089obyG0hJBC9ieUi4mNAQw/iOFaMMu4luZ3t6L343FHFqbUPExbY37I8PYgxSeDkpLxSX3AADEhtoPUtAfQu0AAngnRykzbDHf9Xp923/askaOdr467Bx4bN119Tc1UviCCotWeMeIfYFDAHWQ87DwDyq4sDNdmH+u85LfwiQKd3pFz1gJQ2FoHLvYOsUNlFQpPk4JyHBVlfJpTeB5A5hlJVrRAAyxRpHwxdbogfIBCn49bIegbz4C2vQhsr0H9d8DZ7ybsu1uGLcBBFW79MVSHowxzRT/EF3znKd58mI2xS/g6KpOt/QIBF2H+jQwW3SHrukHSxmA4WMYDgx42s3Yb5qm8WfhmW+OsHDmwGPLJgWDqtN44BViA1obxm7BELJs9A87lJtXgdmo7HQTaOfAsyRnph8R5kU1VdpOMIfSjPHPtI5Yu3ZtrW7bjRnbDoGzubAsMMq11S/VCQLd6I580pXQtXCIua0ymZFoOJzJ4tOFgYEB+o7ExV4WucMUHT/WXlBDkyRSAQc+ZGbkk6FbzcbwnX6GbwK/NkmmGtF3FKjBpAkPliaNnfyr4OkPuM5eGxmU/+1bGu7dcoXhU+jy1RQMbSOYZoWBz9oo2eDsSYZWfjbaghMj8DNy+vTpc8eOHSN6ULtFaSm3E3Lxinb9RiOgzYjCh2IgeMAFVJc2FPHFLYOdoLyygSjtKw4bFMfgW48xUwv3GdVLTAOGO2KPmBWw2VAtQMMaTfm2mR3t8bw1rCYzuIapmm4my/Wo8v0FdKFxFzZXCnMqHc+MnmmxdANmLAsQkpJHsbY4nEiUVZ4GWjBWtEDEVbYBnWqSR8gYduU+Ao41MbegSQr1yKAGZaBaBzuY2rWRPPTpJ5ozBfNRGNL0a09eo5FiBJqbC9/pTyj4f7KX/lUJ8yGiAAAAAElFTkSuQmCC" | ||
camel.apache.org/provider: "Apache Software Foundation" | ||
camel.apache.org/kamelet.group: "Actions" | ||
camel.apache.org/kamelet.namespace: "AI" | ||
labels: | ||
camel.apache.org/kamelet.type: "action" | ||
spec: | ||
definition: | ||
title: "Image-to-Text Action" | ||
description: Detect and classify objects in an image into texts using the SSD and ResNet models and the ImageNet dataset. | ||
type: object | ||
types: | ||
out: | ||
mediaType: application/json | ||
dependencies: | ||
- "mvn:ai.djl.pytorch:pytorch-engine:0.29.0" | ||
- "mvn:ai.djl.pytorch:pytorch-model-zoo:0.29.0" | ||
- "mvn:net.sf.extjwnl:extjwnl:2.0.5" | ||
- "mvn:net.sf.extjwnl:extjwnl-data-wn31:1.2" | ||
- "mvn:org.apache.camel.kamelets:camel-kamelets-utils:4.8.0-SNAPSHOT" | ||
- "camel:core" | ||
- "camel:kamelet" | ||
- "camel:jackson" | ||
- "camel:djl" | ||
template: | ||
beans: | ||
- name: imageNetUtil | ||
type: "#class:org.apache.camel.kamelets.utils.djl.ImageNetUtil" | ||
from: | ||
uri: "kamelet:source" | ||
steps: | ||
- to: "djl:cv/object_detection?artifactId=ssd" | ||
- convertBodyTo: "ai.djl.modality.cv.Image[]" | ||
- split: | ||
expression: | ||
simple: "${body}" | ||
aggregationStrategy: "#class:org.apache.camel.processor.aggregate.GroupedBodyAggregationStrategy" | ||
steps: | ||
- to: "djl:cv/image_classification?artifactId=resnet" | ||
# The output from the image classification model is classified | ||
# as one of 1000 labels from WordNet. | ||
# Since it's too fine-grained, we want to find the higher-level | ||
# group (= hypernym) for the classification using the WordNet | ||
# dictionary. | ||
- bean: | ||
ref: "{{imageNetUtil}}" | ||
method: extractClassName | ||
- bean: | ||
ref: "{{imageNetUtil}}" | ||
method: addHypernym | ||
- marshal: | ||
json: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../camel-kamelets-utils/src/main/java/org/apache/camel/kamelets/utils/djl/ImageNetUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.camel.kamelets.utils.djl; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.camel.Exchange; | ||
import org.apache.camel.RuntimeCamelException; | ||
|
||
import ai.djl.modality.Classifications; | ||
import net.sf.extjwnl.data.IndexWord; | ||
import net.sf.extjwnl.data.POS; | ||
import net.sf.extjwnl.data.PointerUtils; | ||
import net.sf.extjwnl.data.list.PointerTargetNodeList; | ||
import net.sf.extjwnl.dictionary.Dictionary; | ||
|
||
/** | ||
* A utility bean class for handling ImageNet (https://image-net.org/) classifications. | ||
*/ | ||
public class ImageNetUtil { | ||
|
||
public ImageNetUtil() { | ||
} | ||
|
||
public void extractClassName(Exchange exchange) { | ||
Classifications body = exchange.getMessage().getBody(Classifications.class); | ||
String className = body.best().getClassName().split(",")[0].split(" ", 2)[1]; | ||
exchange.getMessage().setBody(className); | ||
} | ||
|
||
public void addHypernym(Exchange exchange) throws Exception { | ||
String className = exchange.getMessage().getBody(String.class); | ||
Dictionary dic = Dictionary.getDefaultResourceInstance(); | ||
IndexWord word = dic.getIndexWord(POS.NOUN, className); | ||
if (word == null) { | ||
throw new RuntimeCamelException("Word not found: " + className); | ||
} | ||
PointerTargetNodeList hypernyms = PointerUtils.getDirectHypernyms(word.getSenses().get(0)); | ||
String hypernym = hypernyms.stream() | ||
.map(h -> h.getSynset().getWords().get(0).getLemma()) | ||
.findFirst().orElse(className); | ||
exchange.getMessage().setBody(List.of(className, hypernym)); | ||
} | ||
} |
Oops, something went wrong.