|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Cesar Coelho |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | + * a copy of this software and associated documentation files (the |
| 6 | + * "Software"), to deal in the Software without restriction, including |
| 7 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | + * permit persons to whom the Software is furnished to do so, subject to |
| 10 | + * the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 19 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + * |
| 23 | + */ |
| 24 | +package esa.mo.platform.impl.provider.adapters; |
| 25 | + |
| 26 | +import esa.mo.helpertools.misc.OSValidator; |
| 27 | +import esa.mo.helpertools.misc.ShellCommander; |
| 28 | +import esa.mo.platform.impl.provider.gen.ArtificialIntelligenceAdapterInterface; |
| 29 | +import java.io.File; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.logging.Level; |
| 32 | +import java.util.logging.Logger; |
| 33 | + |
| 34 | +/** |
| 35 | + * The Artificial Intelligence adapter for the Intel Movidius Neural Compute |
| 36 | + * Stick via a python script. |
| 37 | + * |
| 38 | + * @author Cesar Coelho |
| 39 | + */ |
| 40 | +public class ArtificialIntelligenceIntelMovidiusAdapter implements ArtificialIntelligenceAdapterInterface { |
| 41 | + |
| 42 | + private static final Logger LOGGER = Logger.getLogger(ArtificialIntelligenceIntelMovidiusAdapter.class.getName()); |
| 43 | + private final File setupVarsPath; |
| 44 | + |
| 45 | + public ArtificialIntelligenceIntelMovidiusAdapter() throws IOException { |
| 46 | + // Check if Python3 is installed! |
| 47 | + ShellCommander shellCommander = new ShellCommander(); |
| 48 | + String cmdPython = "python3 --version"; |
| 49 | + String out = shellCommander.runCommandAndGetOutputMessage(cmdPython); |
| 50 | + String[] splits = out.split("Python "); |
| 51 | + |
| 52 | + if (splits.length == 0) { |
| 53 | + throw new IOException("The Python version could not be determined!" |
| 54 | + + " The command returned: " + out); |
| 55 | + } |
| 56 | + |
| 57 | + LOGGER.log(Level.INFO, "The Python3 version is: " + splits[1]); |
| 58 | + String[] subVersions = splits[1].split("\\."); |
| 59 | + |
| 60 | + if (Integer.valueOf(subVersions[1]) < 6) { |
| 61 | + throw new IOException("The installed python3 version is < 3.6.0!" |
| 62 | + + "\n>>>> Please update your Python version!"); |
| 63 | + } |
| 64 | + |
| 65 | + OSValidator os = new OSValidator(); |
| 66 | + |
| 67 | + // Is it Linux or Windows? |
| 68 | + if (os.isUnix()) { |
| 69 | + // Find the folder where the Intel Movidius software is installed |
| 70 | + String[] options = { |
| 71 | + "/opt/intel" |
| 72 | + }; |
| 73 | + |
| 74 | + setupVarsPath = this.crawlOptions(options, "setupvars.sh"); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (os.isWindows()) { |
| 79 | + // Find the folder where the Intel Movidius software is installed |
| 80 | + String[] options = { |
| 81 | + "C:\\Program Files (x86)\\Intel", |
| 82 | + "C:\\Program Files (x86)\\IntelSWTools", |
| 83 | + "C:\\Program Files\\Intel", |
| 84 | + "C:\\Program Files\\IntelSWTools" |
| 85 | + }; |
| 86 | + |
| 87 | + setupVarsPath = this.crawlOptions(options, "setupvars.bat"); |
| 88 | + |
| 89 | + // Please install version: 2020.3 |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + throw new IOException("The current OS is not supported: " + os.getOS()); |
| 94 | + } |
| 95 | + |
| 96 | + private File crawlOptions(String[] options, String filename) throws IOException { |
| 97 | + for (String option : options) { |
| 98 | + File folder = new File(option); |
| 99 | + File path = this.findPathToFile(folder, filename); |
| 100 | + |
| 101 | + if (path != null) { // Found! |
| 102 | + LOGGER.log(Level.INFO, "The file was found on path:\n" |
| 103 | + + " >> " + path.getAbsolutePath()); |
| 104 | + return path; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + throw new IOException("The file " + filename + " was not found!"); |
| 109 | + } |
| 110 | + |
| 111 | + private File findPathToFile(File path, String toBeMatched) { |
| 112 | + if (path.isFile()) { |
| 113 | + return toBeMatched.equals(path.getName()) ? path : null; |
| 114 | + } |
| 115 | + |
| 116 | + // It is a directory... crawl through it |
| 117 | + for (File entry : path.listFiles()) { |
| 118 | + File file = findPathToFile(entry, toBeMatched); |
| 119 | + if (file != null) { |
| 120 | + return file; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void setModel(String modelPath, String weightsPath) throws IOException { |
| 129 | + |
| 130 | + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void executeInference(String inputPath, String outputPath) { |
| 135 | + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
| 136 | + } |
| 137 | + |
| 138 | + public String generateScriptSH(String pathIntelVar, String pythonFile) { |
| 139 | + StringBuilder str = new StringBuilder(); |
| 140 | + str.append("#!/bin/bash \n\n"); |
| 141 | + str.append("source ").append(pathIntelVar); |
| 142 | + str.append("\n\n"); |
| 143 | + str.append("python3 ").append(pythonFile); |
| 144 | + return str.toString(); |
| 145 | + } |
| 146 | + |
| 147 | + public String generateScriptBAT(String pathIntelVar, String pythonFile) { |
| 148 | + StringBuilder str = new StringBuilder(); |
| 149 | + str.append(pathIntelVar); |
| 150 | + str.append("\n\n"); |
| 151 | + str.append(pythonFile); |
| 152 | + return str.toString(); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments