In VS2017, C# calls. py packaged. exe file (pytorch network)

Posted by quick5pnt0 on Wed, 13 Oct 2021 22:39:36 +0200

Two parts: 1. Use pyinstaller to package the project file of xxx.py and generate. exe file;

               2. Simple C# of the interactive interface to achieve. exe file call and transfer parameters (file name).

c# implementation functions: 1) click browse folder, select file (file path for. exe),

                       2) Click Browse Folder and select. exe file;

                      3) Click to execute the. exe file; Display output results -- string type (contents of print("") in xx.py)

This paper implements python program packaging based on python 1.6 (without a large number of third-party libraries, the steps are the same)

The two reference links below are important. First look at the second link, then look at the previous packaging steps and error reports. See the first link.

For the neural network program based on pytorch, pay attention to the following items 6 and 7 when packaging (otherwise. exe calls lib to make an error)

1, Packaging:

        1) Streamline the file and focus the functions on an if in main.py__ name__== "_main_": in the function, the files to be used (. txt,.csv,.pt...) all use relative paths (placed in the current directory of main.py);

        2) Pass parameters in sys.args[x] mode, which can be executed on the cmd command line;

        3) cmd, enter the project working environment and run the code;

            (parameter, separated by spaces - > args [1] ='aa ', args [2] ='text. TXT')

 

      4) After the. Py file code is verified to be correct, start the packaging process. Install pyinstaller library;

pip install pyinstaller

   5) In the current environment, enter the path of the project to be packaged (main.py), and execute the following steps:;

       If the icon is not needed, you can not use - i and subsequent commands ico file website has a special generation website.

6) In this project, buil d (in the process of generation), dist folder (result) and main.spec (which may be used later) will appear. There are exe files required by. In dist.

7) Run in cmd and report a third-party library error. For modification, see:

    (Anaconda \ envs \ XXX \ lib \ site packgs \ ~ ~), copy the entire folder of the third library In conda virtual environment, package python program with pyinstaller to generate exe and call the third-party library_ pingfan2014 blog - CSDN bloghttps://blog.csdn.net/pingfan2014/article/details/113436472     If you cannot find the file (. txt, etc.) that needs to be read during the execution of the. py program, copy the file to the resource folder and modify the. spec file by using the above method

2, C # call implementation code

Four methods of c# calling python (four methods have been tried, only the last two of my success are explained in detail, and the other methods are only listed. Please use Google Baidu for details)_ Wulin prawn blog - CSDN blog_ C# call Python

  The simplest is to use TextBlock, Label and button controls. Read the folder and use the openfiler in the code segment, which is not reflected here.

As follows, the code of the path is displayed to browse the folder. Call. exe, see the previous reference link (for special reasons, you can't share all)

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;


namespace ASR_V0._1
{

    public partial class MainWindow : Window
    {
       
        OpenFileDialog Fp_Wav = new OpenFileDialog();      //Make it a global variable
        OpenFileDialog Fp_Exe = new OpenFileDialog();


        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
     
            Fp_Wav.Filter = "Audio file|*.wav";

            if (Fp_Wav.ShowDialog() == true)
            {
                textBlock.Text = Fp_Wav.FileName;
            }
        }

        private void Button1_Click(object sender, RoutedEventArgs e)   /*"Click on "Browse"
        {

            Fp_Exe.Filter = "application program|*.exe";

            if (Fp_Exe.ShowDialog() == true)
            {
                textBlock1.Text = Fp_Exe.FileName;   //Displays the results of the selection
            }

        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
           //Calling program
            textBlock2.Text = output;             //Display results
        }


    }
}

Topics: Python C# Pytorch asr pyinstaller