Skip to content

Commit

Permalink
Merge pull request #106 from texttechnologylab/uima-3
Browse files Browse the repository at this point in the history
Added latest Updates
  • Loading branch information
abrami authored Oct 8, 2024
2 parents 7e6df73 + 62f6610 commit ab97a18
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
import com.github.dockerjava.api.model.*;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.transport.DockerHttpClient;
import com.google.common.collect.ImmutableList;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidParameterException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -162,22 +166,21 @@ public class DUUIDockerInterface {
*/
public DUUIDockerInterface() throws IOException {

// GREAT TODO!!!
//URI dockerClientURI;
//if (System.getProperty("os.name").toLowerCase().contains("windows")) {
// dockerClientURI = URI.create("npipe:////./pipe/docker_engine");
//} else {
// dockerClientURI = URI.create("tcp://localhost:2375");
//}

//_docker = DockerClientBuilder.getInstance()
// .withDockerHttpClient(new ApacheDockerHttpClient.Builder()
// .dockerHost(dockerClientURI).build()).build();
_docker = DockerClientBuilder.getInstance().build();

if (!System.getProperty("os.name").contains("Windows")) {
_docker = DockerClientBuilder.getInstance().build();
} else {
// Windows
final DockerHttpClient http = new ApacheDockerHttpClient.Builder()
.connectionTimeout(Duration.ofSeconds(5))
.responseTimeout(Duration.ofMinutes(10))
.dockerHost(URI.create("npipe:////./pipe/docker_engine"))
// .dockerHost(URI.create("tcp://127.0.0.1:2375")) // if npipe doesn't work.
.build();
_docker = DockerClientBuilder.getInstance()
.withDockerHttpClient(http)
.build();

// DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost("tcp://localhost:2375").build();
// _docker = DockerClientBuilder.getInstance(config).build();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,43 @@ public interface IDUUICommunicationLayer {

public void deserialize(JCas jc, ByteArrayInputStream input, String targetView) throws IOException, SAXException, CASException;

/**
* Serializes a JCas to a byte array output stream by using the LUA script provided by the component.
* @param jc Input JCas.
* @param out Output stream, i.e. the input to the component.
* @param parameters Parameters for use in the LUA script.
* @throws CompressorException
* @throws IOException
* @throws SAXException
*/
public void serialize(JCas jc, ByteArrayOutputStream out, Map<String, String> parameters) throws CompressorException, IOException, SAXException, CASException;

/**
* Deserializes a byte array input stream to a JCas by using the LUA script provided by the component.
* @param jc Output JCas, note that the CAS is not reset before deserialization.
* @param input Input stream, i.e. the output of the component.
* @throws IOException
* @throws SAXException
*/
public void deserialize(JCas jc, ByteArrayInputStream input) throws IOException, SAXException, CASException;

/**
*
* @return
*/
public IDUUICommunicationLayer copy();

/**
*
* @param results
* @return
*/
public ByteArrayInputStream merge(List<ByteArrayInputStream> results);

String myLuaTestMerging();
/**
* Serializes a JCas to a byte array output stream by using the LUA script provided by the component.
* @param jc Input JCas.
* @param out Output stream, i.e. the input to the component.
* @param parameters Parameters for use in the LUA script.
* @throws CompressorException
* @throws IOException
* @throws SAXException
*/
default void serialize(JCas jc, ByteArrayOutputStream out, Map<String,String> parameters) throws CompressorException, IOException, SAXException, CASException {
serialize(jc, out, parameters, "_InitialView");
}

/**
* Deserializes a byte array input stream to a JCas by using the LUA script provided by the component.
* @param jc Output JCas, note that the CAS is not reset before deserialization.
* @param input Input stream, i.e. the output of the component.
* @throws IOException
* @throws SAXException
*/
default void deserialize(JCas jc, ByteArrayInputStream input) throws IOException, SAXException, CASException {
deserialize(jc, input, "_InitialView");
}

/**
*
* @return
*/
public IDUUICommunicationLayer copy();

/**
*
* @param results
* @return
*/
public ByteArrayInputStream merge(List<ByteArrayInputStream> results);

String myLuaTestMerging();

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Collectors;


Expand Down Expand Up @@ -281,47 +280,31 @@ public List<DUUIDocument> listDocuments(String path, String fileExtension, boole

@Override
public DUUIFolder getFolderStructure() {
return getFolderStructure("", "Files");
}

DUUIFolder root = new DUUIFolder("", "Files");
public DUUIFolder getFolderStructure(String path, String name) {

listFolderContents(root);
DUUIFolder root = new DUUIFolder(path, name);

return root;
}
ListFolderResult result = null;

/**
* Recursively traverses the dropbox directory and saves the directory
* tree in the root folder.
*
* @param root Root folder containing entire directory structure
*/
private void listFolderContents(DUUIFolder root) {
try {
ListFolderResult result = client.files().listFolderBuilder(root.id)
.withRecursive(false)
.withIncludeMediaInfo(false)
.withIncludeDeleted(false)
.withIncludeHasExplicitSharedMembers(false)
.withIncludeMountedFolders(true)
.withLimit(2000L)
result = client
.files()
.listFolderBuilder(path)
.start();

do {
result.getEntries().parallelStream()
.filter(entry -> entry instanceof FolderMetadata)
.map(entry -> (FolderMetadata) entry)
.map(entry -> new DUUIFolder(entry.getPathDisplay(), entry.getName()))
.peek(root::addChild)
.forEach(this::listFolderContents);

if (result.getHasMore()) {
result = client.files().listFolderContinue(result.getCursor());
} else break;

} while (true);
} catch (Exception e) {
new RuntimeException(e);
} catch (DbxException e) {
return null;
}

result.getEntries().stream()
.filter(f -> f instanceof FolderMetadata)
.map(f -> getFolderStructure(((FolderMetadata) f).getId(), f.getName()))
.filter(Objects::nonNull)
.forEach(root::addChild);

return root;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,6 @@ public DUUISegmentationStrategy getSegmentationStrategy() {
}

public long getTimeout() {
return Long.valueOf(_options.getOrDefault(timeout, "60l"));
return Long.valueOf(_options.getOrDefault(timeout, "60"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,11 @@ private void setVideoMetadata(YouTubeVideo video, JCas jCas) throws IOException,

FSList<Playlist> list = FSList.create(jCas, playlists);
list.addToIndexes();

youTube.setPlaylist(list);
}

youTube.setName(video._title);
youTube.setUrl(video.getVideoUrl());
youTube.setChannelName(video._channelName);
youTube.setChannelURL(video._channelUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,39 @@ private static void executeFFMpeg(String absoluteInputPath, List<String> command
}
}

/**
* Gets subimages as independent image files in .png
* @param jCas The JCas containing SubImage Annotations
* @return A List of files, each containing a SubImage. Files get deleted on exit
*/
public static List<File> getSubImages(JCas jCas){
return getSubImages(jCas, null);
return getSubImages(jCas, "png");
}

public static List<File> getSubImages(JCas jCas, String overrideExtension) {
/**
* Gets subimages as independent image files
* @param jCas The JCas containing SubImage Annotations
* @param extension Changes the subimages images file extension
* @return A List of files, each containing a SubImage. Files get deleted on exit
*/
public static List<File> getSubImages(JCas jCas, String extension) {

if(extension.startsWith("."))
extension = extension.substring(1);
String finalExtension = extension;


List<File> subImages = new ArrayList<>();

JCasUtil.select(jCas, SubImage.class).forEach(subImage -> {
byte[] base64Image = Base64.decodeBase64(subImage.getParent().getSrc());
String mainImageB64 = "";
if(subImage.getParent().getSrc() == null){
mainImageB64 = jCas.getSofaDataString();
}else{
mainImageB64 = subImage.getParent().getSrc();
}

byte[] base64Image = Base64.decodeBase64(mainImageB64);
try {
BufferedImage bImage = ImageIO.read(new ByteArrayInputStream(base64Image));

Expand All @@ -468,45 +491,28 @@ public static List<File> getSubImages(JCas jCas, String overrideExtension) {

Rectangle bounds = polygon.getBounds();

BufferedImage bSubImage = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_RGB);

Graphics2D graphics = bSubImage.createGraphics();

polygon.translate(-bounds.x, -bounds.y);

graphics.setClip(polygon);
graphics.drawImage(bImage, -bounds.x, -bounds.y, null);

// Create file

File tempInputFile = new File("tempInputImage");
tempInputFile.deleteOnExit();
try (OutputStream stream = new FileOutputStream(tempInputFile)) {
stream.write(base64Image);
}

String mimeType = Files.probeContentType(tempInputFile.toPath());
String subType = "";

if(overrideExtension == null){
if(mimeType != null && !mimeType.isEmpty()){
subType = mimeType.split("/")[1];
}else{
subType = "jpg";
}
}else{
subType = overrideExtension;
if(subType.startsWith(".")){
if(subType.length() > 1)
subType = subType.substring(1);
BufferedImage bSubImage;
if(finalExtension.equals("png") || finalExtension.equals("tiff") || finalExtension.equals("gif") || finalExtension.equals("apng"))
bSubImage = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB); // Alpha channel support
else
bSubImage = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_RGB); // No alpha

// Copy pixels form main image to new subimage
for (int x = bounds.x; x < bounds.x + bounds.width; x++) {
for (int y = bounds.y; y < bounds.y + bounds.height; y++) {
if(polygon.contains(x, y)){
int iColor = bImage.getRGB(x, y);
bSubImage.setRGB(x - bounds.x, y - bounds.y, iColor);
}
}
}

File outputFile = new File(getOutputName(jCas, subImage, subType));
// Create output file
File outputFile = new File(getOutputName(jCas, subImage, finalExtension));
outputFile.deleteOnExit();

RenderedImage rendImage = bSubImage;
ImageIO.write(rendImage, subType, outputFile);
ImageIO.write(rendImage, finalExtension, outputFile);

subImages.add(outputFile);
} catch (IOException e) {
Expand Down
1 change: 0 additions & 1 deletion src/test/java/TestDUUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,6 @@ public void multimodalImageCutterTest() throws Exception{

composer.add(new DUUIDockerDriver.Component("duui-yolo:latest") // Image detection
.withScale(iWorkers)
.withRunningAfterDestroy(true)
.build());

composer.add(new DUUIUIMADriver.Component(createEngineDescription(XmiWriter.class,
Expand Down

0 comments on commit ab97a18

Please sign in to comment.