首页 文章

使用java(spring)中嵌入的tensorflow库执行python程序不起作用

提问于
浏览
0
  • 首先,我在spring框架中有这个java程序试图调用一个简单的hello world python脚本 .

  • 它工作并返回"hello world"

  • 然后我试着运行一个嵌入了tensorflow库的python脚本,然后它什么也没有返回 . (控制台中没有打印)

  • python tensorflow

import tensorflow as tf import sys import os

os.environ ['TF_CPP_MIN_LOG_LEVEL'] ='2'导入张量流为tf

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

label_lines = [line.rstrip()for tf.gfile.GFile中的行(“logs / trained_labels.txt”)]

用tf.gfile.FastGFile(“logs / trained_graph.pb”,'rb')作为f:graph_def = tf.GraphDef()graph_def.ParseFromString(f.read())_ = tf.import_graph_def(graph_def,name =' “)

使用tf.Session()作为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 )))
  • java脚本

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

公共类HelloWorld扩展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 \测试\ crack1.jpg“); p.waitFor();

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

// Thread.sleep(1000);字符串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); }}}

  • 问题 - 我可以在java程序中运行一个带有tensorflow的python吗?

1 回答

  • 0

    您可以加载保存的tensorflow模型并从java运行预测 . 检查here

    加载train_graph.pb文件并使用feed和fetch函数发送和接收数据 . 你可以关注android tutorial

相关问题