0
votes
  1. first, I have this java program within spring framework that is trying to call a simple hello world python script.
  2. it worked and returned a "hello world"
  3. so then I tried to run a python script with tensorflow library embedded, then it returns nothing.(nothing printed in the console)

  4. python+tensorflow

    import tensorflow as tf import sys import os

    os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tf

    image_path = sys.argv[1] image_data = tf.gfile.FastGFile(image_path, 'rb').read()

    label_lines = [line.rstrip() for line in tf.gfile.GFile("logs/trained_labels.txt")]

    with tf.gfile.FastGFile("logs/trained_graph.pb", 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='')

    with tf.Session() as sess:

    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
    
    predictions = sess.run(softmax_tensor, \
             {'DecodeJpeg/contents:0': image_data})
    
    top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
    
    for node_id in top_k:
        human_string = label_lines[node_id]
        score = predictions[0][node_id]
        print('%s (Percentage = %.2f )'  %  (human_string, (score * 100 )))
    
  5. java script

    package com.test; import java.io.IOException; import java.io.*;

    public class HelloWorld extends Thread{

    public static void main(String [] args) throws InterruptedException {
    

    // System.out.println("Hello worldss!!!!"); try { Process p = Runtime.getRuntime().exec("D:\Python3.6.3\Python36\python D:\EclipseIDE\TestJava\src\com\test\classify.py D:\EclipseIDE\TestJava\src\com\test\crack1.jpg"); p.waitFor();

            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    

    // Thread.sleep(1000); String s; while((s = in.readLine())!=null) { System.out.println(s); } if (s == null){ System.out.println("lol"); } // System.out.println(in.readLine()); }catch(IOException e){ e.printStackTrace(System.err); } } }

  6. question - can I run a python with tensorflow embedded from java program?

1

1 Answers

0
votes

You can load the saved tensorflow model and run the prediction from java. Check here

Load the train_graph.pb file and use the feed and fetch function to send and receive data. You can follow the android tutorial