Skip to content

Commit

Permalink
working commit for rewrite of external-api-extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
faberf committed Jun 8, 2023
1 parent 546b23c commit 7d27038
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 123 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.vitrivr.cineast.core.features;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -15,16 +16,17 @@ public class ExternalTextFeature extends AbstractTextRetriever {

public static final String DEFAULT_TABLE_NAME = "features_externalText";

final AbstractFeatureClientWrapper client;
final FeatureClient client;

public ExternalTextFeature() {
super(DEFAULT_TABLE_NAME);
throw new IllegalArgumentException("no properties specified");
}

public ExternalTextFeature(Map<String, String> properties) throws Exception {
public ExternalTextFeature(Map<String, String> properties) throws IOException {
super(properties.getOrDefault(CineastConstants.ENTITY_NAME_KEY, DEFAULT_TABLE_NAME), properties);
this.client = AbstractFeatureClientWrapper.build(properties);

this.client = FeatureClient.build(properties);
}

private static final Logger LOGGER = LogManager.getLogger();
Expand All @@ -35,8 +37,8 @@ public void processSegment(SegmentContainer sc) {
return;
}
try {
BufferedImage img = sc.getMostRepresentativeFrame().getImage().getBufferedImage();
String feature = client.extractTextFromImage(img);
Map<String,Object> extractions = client.extract(sc);
String feature = (String) extractions.getOrDefault("raw_text", "");
var descriptor = new SimpleFulltextFeatureDescriptor(sc.getId(), feature);
this.writer.write(descriptor);
} catch (Exception e) {
Expand Down
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;
}



}
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 "";
}
}
}
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);
}
}
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;
}

This file was deleted.

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;
}



}
Loading

0 comments on commit 7d27038

Please sign in to comment.