Frage

Hi i just create a java application to run my python code externally. But it want giving me the out put. this is my java code:-

    package com.epatient;

import java.io.*;


public class InterpreterExample {

    //static String workingDir = System.getProperty("user.dir");
     //static String appDir =  workingDir + "\\epatient\\epatient_prediction.py";

     public static void main(String a[]){
         try{
             String appDir = "C:\\Users\\Thushara Kasun\\Documents\\juno\\serial.port\\epatient\\epatient_prediction.py";
              System.out.println(appDir);
         ProcessBuilder pb = new ProcessBuilder("python",appDir);
         Process p = pb.start();

         BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
         String ret = in.readLine();
         System.out.println("value is : "+ret);
         }catch(NumberFormatException e){e.printStackTrace();}
         catch (IOException e) {e.printStackTrace();}        
          }
       }

this is my python code :-

    import sys
    from sklearn.externals import joblib
    import pandas as pd
    import numpy as np
    import csv

    from sklearn.decomposition import PCA

    import re
    import psycopg2

    import datetime
    import time

    con = None
    bio_data = None
    heart_rate = None
    so2_data = None
    temp_data = None
    bp_data = None

    try:

        con = psycopg2.connect(database='Epatient_user_1', user='postgres', password='root') 
        cur = con.cursor()

    ...#database access codes omited

        model_name = 'trained_model'
        est = joblib.load(model_name)
        predictions = est.predict(data)
        #predictions

    # <codecell>


    #sys.stdout.write(str(int(predictions[0])))
    #sys.stdout.flush()
    #print int(predictions[0])
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    #print st
    cur.execute('INSERT INTO patient_status (STATUS,TIME,PROCESSED) VALUES (' + str(int(predictions[0])) + ',\''+ st + '\',0);')
    con.commit()


except psycopg2.DatabaseError, e:
    print 'Error %s' % e    
    sys.exit(1)


finally:

    if con:
        con.close()

    sys.stdout.write(str(int(predictions[0])))
    sys.stdout.flush()
    #print int(predictions[0])

Java out put is just value is : null there is no problem with the python code it is working perfectly. i just want to print a Boolean which is in a string format. simultaneously i need to update that value in my local postgresql database(via python code, which is working individually). simply the python code is not executing by my java application. Kindly help me on this or give me a alternative way to resolve this issue.

War es hilfreich?

Lösung

I think that your Java code is Ok, then try to change your Python script.

Please, would you be so kind to change the line print int(predictions[0]) with:

sys.stdout.write(str(predictions[0]))
sys.stdout.flush()

sys.stdout will print data to console outputstream

and just for testing, put the absolute path of your script in the ProcessBuilder constructor:

String appDir = "C:\\my\\full\\path\\to\\script.py";
ProcessBuilder pb = 
   new ProcessBuilder("python", appDir);

Process p = pb.start();
....
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top