eto123
eto123

Reputation: 83

How to run python neural network keras script in c# .net core app

I need to run python neural network script in witch using imports:

from keras.models import Sequential    
from keras.layers import Dense    
from keras.callbacks import History    
from keras.models import load_model    
import numpy as np    
import matplotlib.pyplot as plt    
np.random.seed(7)    
import pandas as pd    
from sklearn.model_selection import train_test_split    
from keras import optimizers

I try to run it using IronPython but this dont worked. IronPython dont recognise keras.models etc... I also try to run it with System.Diagnostic.Process but this solution also dont worked. It run only simple python scripts with some prints.

I need just to execute .py script from .cs class.

I try examples with cmd.exe with run .py script with but they dont worked.

Is there any way to run python neural network script in c# app? It must be very simple.

EDIT: One of my solutions: string fileName =

string fileName="C:/Users/FUJITSU/source/repos/CardiologicClinic_WebApp/CardiologicClinic_WebApp/AI/xd.py";
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo(@"C:\Users\FUJITSU\PycharmProjects\NIDUC\venv\Scripts\python.exe", fileName);
                p.StartInfo.WorkingDirectory = "C:/Users/FUJITSU/source/repos/CardiologicClinic_WebApp/CardiologicClinic_WebApp/AI";
                p.Start();
                p.WaitForExit();
                p.Close();

And error produced:

line 1, in <module>
    from keras.models import load_model
ModuleNotFoundError: No module named 'keras'

Upvotes: 2

Views: 1698

Answers (2)

WG97
WG97

Reputation: 52

Here is the solution:

 string fileName = "path to script.py";
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo(@"path to python.exe", fileName);
            p.StartInfo.WorkingDirectory = "path to working dir";
            p.Start();
            p.WaitForExit();
            p.Close();

This worked fine for me.

Optionaly you can hide process with this command:

p.startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

Upvotes: 1

shanecandoit
shanecandoit

Reputation: 621

I don't think that every python library will work in IronPython. Posting any error messages you do have would be helpful in finding a solution.

Instead, try calling python using the C# version of system('python myscript.py'). It is sure to be tough going though getting two languages to work together.

(If I could comment, I would have made this a comment instead. Your forgiveness, please.)

Upvotes: 0

Related Questions