Skip to content

Commit

Permalink
Refactoring & AI service WIP code
Browse files Browse the repository at this point in the history
  • Loading branch information
CesarCoelho committed May 27, 2022
1 parent 12cd2dd commit e1dffa8
Show file tree
Hide file tree
Showing 12 changed files with 616 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
* limitations under the License.
* ----------------------------------------------------------------------------
*/
package esa.mo.sm.impl.util;
package esa.mo.helpertools.misc;

/**
* The OSValidator allows easy determination of the current OS.
*
* @author Cesar Coelho
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* limitations under the License.
* ----------------------------------------------------------------------------
*/
package esa.mo.sm.impl.util;
package esa.mo.helpertools.misc;

import java.io.BufferedReader;
import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2021 Cesar Coelho
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package esa.mo.platform.impl.provider.adapters;

import esa.mo.helpertools.misc.OSValidator;
import esa.mo.helpertools.misc.ShellCommander;
import esa.mo.platform.impl.provider.gen.ArtificialIntelligenceAdapterInterface;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* The Artificial Intelligence adapter for the Intel Movidius Neural Compute
* Stick via a python script.
*
* @author Cesar Coelho
*/
public class ArtificialIntelligenceIntelMovidiusAdapter implements ArtificialIntelligenceAdapterInterface {

private static final Logger LOGGER = Logger.getLogger(ArtificialIntelligenceIntelMovidiusAdapter.class.getName());
private final File setupVarsPath;

public ArtificialIntelligenceIntelMovidiusAdapter() throws IOException {
// Check if Python3 is installed!
ShellCommander shellCommander = new ShellCommander();
String cmdPython = "python3 --version";
String out = shellCommander.runCommandAndGetOutputMessage(cmdPython);
String[] splits = out.split("Python ");

if (splits.length == 0) {
throw new IOException("The Python version could not be determined!"
+ " The command returned: " + out);
}

LOGGER.log(Level.INFO, "The Python3 version is: " + splits[1]);
String[] subVersions = splits[1].split("\\.");

if (Integer.valueOf(subVersions[1]) < 6) {
throw new IOException("The installed python3 version is < 3.6.0!"
+ "\n>>>> Please update your Python version!");
}

OSValidator os = new OSValidator();

// Is it Linux or Windows?
if (os.isUnix()) {
// Find the folder where the Intel Movidius software is installed
String[] options = {
"/opt/intel"
};

setupVarsPath = this.crawlOptions(options, "setupvars.sh");
return;
}

if (os.isWindows()) {
// Find the folder where the Intel Movidius software is installed
String[] options = {
"C:\\Program Files (x86)\\Intel",
"C:\\Program Files (x86)\\IntelSWTools",
"C:\\Program Files\\Intel",
"C:\\Program Files\\IntelSWTools"
};

setupVarsPath = this.crawlOptions(options, "setupvars.bat");

// Please install version: 2020.3
return;
}

throw new IOException("The current OS is not supported: " + os.getOS());
}

private File crawlOptions(String[] options, String filename) throws IOException {
for (String option : options) {
File folder = new File(option);
File path = this.findPathToFile(folder, filename);

if (path != null) { // Found!
LOGGER.log(Level.INFO, "The file was found on path:\n"
+ " >> " + path.getAbsolutePath());
return path;
}
}

throw new IOException("The file " + filename + " was not found!");
}

private File findPathToFile(File path, String toBeMatched) {
if (path.isFile()) {
return toBeMatched.equals(path.getName()) ? path : null;
}

// It is a directory... crawl through it
for (File entry : path.listFiles()) {
File file = findPathToFile(entry, toBeMatched);
if (file != null) {
return file;
}
}

return null;
}

@Override
public void setModel(String modelPath, String weightsPath) throws IOException {

throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void executeInference(String inputPath, String outputPath) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

public String generateScriptSH(String pathIntelVar, String pythonFile) {
StringBuilder str = new StringBuilder();
str.append("#!/bin/bash \n\n");
str.append("source ").append(pathIntelVar);
str.append("\n\n");
str.append("python3 ").append(pythonFile);
return str.toString();
}

public String generateScriptBAT(String pathIntelVar, String pythonFile) {
StringBuilder str = new StringBuilder();
str.append(pathIntelVar);
str.append("\n\n");
str.append(pythonFile);
return str.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public interface ArtificialIntelligenceAdapterInterface {
/**
* Executes the inference on the Artificial Intelligence device.
*
* @param path The file path to a folder with a set of files to be
* @param inputPath The path to a folder with a set of files to be
* processed.
* @param outputPath The path to a folder to store the processed files.
*/
public void executeInference(String path);
public void executeInference(String inputPath, String outputPath);

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.ccsds.moims.mo.platform.artificialintelligence.ArtificialIntelligenceHelper;
import org.ccsds.moims.mo.platform.artificialintelligence.provider.ArtificialIntelligenceInheritanceSkeleton;
import esa.mo.helpertools.connections.ConnectionProvider;
import java.io.IOException;
import java.util.ArrayList;

public class ArtificialIntelligenceProviderServiceImpl extends ArtificialIntelligenceInheritanceSkeleton {

Expand All @@ -41,6 +43,8 @@ public class ArtificialIntelligenceProviderServiceImpl extends ArtificialIntelli
private boolean initialiased = false;
private final ConnectionProvider connection = new ConnectionProvider();
private ArtificialIntelligenceAdapterInterface adapter;
private final static Long TIMESTAMP = System.currentTimeMillis();
private final ArrayList<String> modelPaths = new ArrayList();

/**
* creates the MAL objects, the publisher used to create updates and starts
Expand Down Expand Up @@ -102,11 +106,51 @@ public void close() {

@Override
public Long setModel(String modelPath, MALInteraction interaction) throws MALInteractionException, MALException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
if (modelPath == null) {
throw new MALException("The modelPath is null!");
}

for (String path : modelPaths) {
if (path.equals(modelPath)) {
throw new MALException("The model already exists!");
}
}

modelPaths.add(modelPath);
return TIMESTAMP + modelPaths.indexOf(modelPath);
}

@Override
public String doInference(Long modelId, String inputTilesPath, MALInteraction interaction) throws MALInteractionException, MALException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public String doInference(Long modelId, String inputTilesPath,
MALInteraction interaction) throws MALInteractionException, MALException {
if (modelId == null) {
throw new MALException("The modelId is null!");
}

if (inputTilesPath == null) {
throw new MALException("The inputTilesPath is null!");
}

String modelPath = modelPaths.get((int) (modelId - TIMESTAMP));

if (!modelPath.endsWith(".xml")) {
throw new MALException("The model does not end with the file extension: .xml");
}

String weightsPath = modelPath.substring(0, modelPath.length() - 4) + ".bin";
Logger.getLogger(ArtificialIntelligenceProviderServiceImpl.class.getName()).log(
Level.INFO, "The weights file path is:\n >> " + weightsPath);

try {
adapter.setModel(modelPath, weightsPath);
String outputTilesPath = "";
adapter.executeInference(inputTilesPath, outputTilesPath);
return outputTilesPath;
} catch (IOException ex) {
Logger.getLogger(ArtificialIntelligenceProviderServiceImpl.class.getName()).log(
Level.SEVERE, "The inference could not be performed!", ex);
}

throw new MALException("The inference could not be performed!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package esa.mo.platform.impl.provider.gen;

import esa.mo.helpertools.misc.Const;
import esa.mo.platform.impl.provider.gen.GPSAdapterInterface;
import esa.mo.platform.impl.util.HelperGPS;
import java.io.IOException;
import java.util.logging.Level;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import esa.mo.helpertools.connections.SingleConnectionDetails;
import esa.mo.helpertools.helpers.HelperMisc;
import esa.mo.helpertools.misc.Const;
import esa.mo.sm.impl.util.OSValidator;
import esa.mo.helpertools.misc.OSValidator;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import esa.mo.com.impl.util.HelperArchive;
import esa.mo.helpertools.connections.ConfigurationProviderSingleton;
import esa.mo.helpertools.connections.ConnectionProvider;
import esa.mo.sm.impl.util.OSValidator;
import esa.mo.helpertools.misc.OSValidator;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
Expand Down
Loading

0 comments on commit e1dffa8

Please sign in to comment.