-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working commit for rewrite of external-api-extraction
- Loading branch information
Showing
9 changed files
with
245 additions
and
123 deletions.
There are no files selected for viewing
56 changes: 0 additions & 56 deletions
56
...st-core/src/main/java/org/vitrivr/cineast/core/features/AbstractFeatureClientWrapper.java
This file was deleted.
Oops, something went wrong.
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
72 changes: 72 additions & 0 deletions
72
cineast-core/src/main/java/org/vitrivr/cineast/core/features/FeatureClient.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,72 @@ | ||
package org.vitrivr.cineast.core.features; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.vitrivr.cineast.core.data.segments.SegmentContainer; | ||
import org.vitrivr.cineast.core.features.exporter.AudioSegmentExporter; | ||
import org.vitrivr.cineast.core.util.web.ImageParser; | ||
import org.vitrivr.cineast.core.util.web.WebClient; | ||
import org.vitrivr.cineast.core.util.web.MessageTemplate; | ||
|
||
public class FeatureClient { | ||
protected final WebClient client; | ||
protected final FeatureClientRequest request; | ||
protected final MessageTemplate responseTemplate; | ||
|
||
protected static final Logger LOGGER = LogManager.getLogger(); | ||
|
||
public static FeatureClient build(Map<String,String> properties) throws IOException { | ||
if(!properties.containsKey("endpoint")){ | ||
throw new IllegalArgumentException("No endpoint specified for external client"); | ||
} | ||
String endpoint = properties.get("endpoint"); | ||
if(!properties.containsKey("request")){ | ||
throw new IllegalArgumentException("No request specified for external client"); | ||
} | ||
String requestString = properties.get("request"); | ||
if(!properties.containsKey("response")){ | ||
throw new IllegalArgumentException("No response template specified for external client"); | ||
} | ||
String responseString = properties.get("response"); | ||
FeatureClientRequest request; | ||
if (new File(requestString).exists()) { | ||
request = new FeatureClientJsonRequest(requestString); | ||
}else { | ||
request = new FeatureClientBinaryRequest(requestString); | ||
} | ||
return new FeatureClient(endpoint, request, responseString); | ||
} | ||
|
||
protected FeatureClient(String endpoint, FeatureClientRequest req, String responseTemplatePath) throws IOException { | ||
this.client = new WebClient(endpoint); | ||
this.request = req; | ||
this.responseTemplate = new MessageTemplate(responseTemplatePath); | ||
} | ||
|
||
public Map<String,Object> extract(SegmentContainer sc) throws IOException, InterruptedException { | ||
|
||
String response = this.request.execute(sc, this.client); | ||
|
||
Map<String, String> responseKeys = this.responseTemplate.parseString(response); | ||
|
||
HashMap<String, Object> result = new HashMap<>(); | ||
for (var key : responseKeys.keySet()) { | ||
switch (key) { | ||
case "raw_text": | ||
result.put(key, responseKeys.get(key)); | ||
break; | ||
default: | ||
LOGGER.warn("Unknown key: {}", key); | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
|
||
|
||
} |
28 changes: 28 additions & 0 deletions
28
cineast-core/src/main/java/org/vitrivr/cineast/core/features/FeatureClientBinaryRequest.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,28 @@ | ||
package org.vitrivr.cineast.core.features; | ||
|
||
import java.io.IOException; | ||
import org.vitrivr.cineast.core.data.segments.SegmentContainer; | ||
import org.vitrivr.cineast.core.features.abstracts.AbstractSegmentExporter; | ||
import org.vitrivr.cineast.core.features.exporter.AudioSegmentExporter; | ||
import org.vitrivr.cineast.core.util.web.WebClient; | ||
|
||
public class FeatureClientBinaryRequest extends FeatureClientRequest { | ||
|
||
private final String key; | ||
public FeatureClientBinaryRequest(String requestString) { | ||
super(); | ||
this.key = requestString; | ||
} | ||
|
||
@Override | ||
public String execute(SegmentContainer sc, WebClient client) throws IOException, InterruptedException { | ||
switch (this.key) { | ||
case "wav_binary": | ||
var wavExport = new AudioSegmentExporter(); | ||
return client.postRawBinary(wavExport.exportToBinary(sc)); | ||
default: | ||
LOGGER.warn("Unknown key: {}", this.key); | ||
return ""; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
cineast-core/src/main/java/org/vitrivr/cineast/core/features/FeatureClientJsonRequest.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,40 @@ | ||
package org.vitrivr.cineast.core.features; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.vitrivr.cineast.core.data.segments.SegmentContainer; | ||
import org.vitrivr.cineast.core.features.exporter.AudioSegmentExporter; | ||
import org.vitrivr.cineast.core.util.web.ImageParser; | ||
import org.vitrivr.cineast.core.util.web.MessageTemplate; | ||
import org.vitrivr.cineast.core.util.web.WebClient; | ||
|
||
public class FeatureClientJsonRequest extends FeatureClientRequest { | ||
private final MessageTemplate requestTemplate; | ||
public FeatureClientJsonRequest(String RequestTemplatePath) throws IOException { | ||
super(); | ||
this.requestTemplate = new MessageTemplate(RequestTemplatePath); | ||
} | ||
@Override | ||
public String execute(SegmentContainer sc, WebClient client) throws IOException, InterruptedException { | ||
Map<String, String> keys = new HashMap<>(); | ||
//iterate through keys | ||
for (var key : this.requestTemplate.getKeys()) { | ||
switch (key) { | ||
case "png_image": | ||
var bufImg = sc.getMostRepresentativeFrame().getImage().getBufferedImage(); | ||
keys.put(key, ImageParser.bufferedImageToDataURL(bufImg, "png")); | ||
break; | ||
case "wav_dataurl": | ||
var wavExport = new AudioSegmentExporter(); | ||
keys.put(key, wavExport.exportToDataUrl(sc)); | ||
break; | ||
default: | ||
LOGGER.warn("Unknown key: {}", key); | ||
break; | ||
} | ||
} | ||
String request = this.requestTemplate.formatString(keys); | ||
return client.postJsonString(request); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
cineast-core/src/main/java/org/vitrivr/cineast/core/features/FeatureClientRequest.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,14 @@ | ||
package org.vitrivr.cineast.core.features; | ||
|
||
import java.io.IOException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.vitrivr.cineast.core.data.segments.SegmentContainer; | ||
import org.vitrivr.cineast.core.util.web.WebClient; | ||
|
||
abstract class FeatureClientRequest { | ||
|
||
protected static final Logger LOGGER = LogManager.getLogger(); | ||
|
||
abstract public String execute(SegmentContainer sc, WebClient client) throws IOException, InterruptedException; | ||
} |
58 changes: 0 additions & 58 deletions
58
cineast-core/src/main/java/org/vitrivr/cineast/core/features/SimpleFESWrapper.java
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
cineast-core/src/main/java/org/vitrivr/cineast/core/util/web/MessageTemplate.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,66 @@ | ||
package org.vitrivr.cineast.core.util.web; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class MessageTemplate { | ||
|
||
private final String template; | ||
|
||
public MessageTemplate(String path) throws IOException { | ||
this.template = Files.readString(new File(path).toPath()); | ||
} | ||
|
||
public List<String> getKeys(){ | ||
List<String> keys = new ArrayList<>(); | ||
Matcher matcher = Pattern.compile("\\$\\{(.*?)\\}").matcher(template); | ||
while(matcher.find()){ | ||
keys.add(matcher.group(1)); | ||
} | ||
return keys; | ||
} | ||
|
||
|
||
public String formatString(Map<String,String> values){ | ||
// if not all keys are present, throw exception | ||
for(String key : getKeys()){ | ||
if(!values.containsKey(key)){ | ||
throw new IllegalArgumentException("Missing key " + key); | ||
} | ||
} | ||
String result = template; | ||
for(String key : values.keySet()){ | ||
result = result.replace("${" + key + "}", values.get(key)); | ||
} | ||
return result; | ||
} | ||
|
||
public Map<String,String> parseString(String input){ | ||
Map<String,String> values = new HashMap<>(); | ||
|
||
String[] parts = template.split("\\$\\{.*?\\}"); | ||
String[] valueParts = new String[parts.length - 1]; | ||
|
||
for (int i = 0; i < parts.length - 1; i++) { | ||
input = input.substring(parts[i].length()); | ||
valueParts[i] = input.substring(0, input.indexOf(parts[i+1])); | ||
} | ||
|
||
List<String> keys = getKeys(); | ||
for (int i = 0; i < keys.size(); i++) { | ||
values.put(keys.get(i), valueParts[i]); | ||
} | ||
|
||
return values; | ||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.