Friday, January 20, 2017

Measuring the dimension of an object using OpenCV and Python: MAKE AN EXECUTABLE (.exe) FILE FROM THE PYTHON SCRIPTS (.py) USING PyInstaller

The next day, I came to the lab with a very proud feeling of myself, because I have finished the code to measure the mechanoreceptors' dimensions automatically. I greeted everyone in the lab and did a short demo of how the program works. Finally, I enclosed the demo with some notable limitations of the program.

Everything went well until that point. However, it was not always sunshine and rainbows on the garden when you are facing an end-user.

After the demo was finished, one of my labmates said, "Wow! Great! Thanks! Now please install this in my computer." Then I said, "Okay! Fine! You just have to download the Python3 or Python2 from the python website. And then install it. After that  you need to install the required libraries or packages, such as numpy and opencv, in your computer as well."

Then I realized that as an end-user what I just said was so confusing. At the end of the day, she said to me, "Wow sounds so complex. Please do help me install them in my computer."

Long story short, I installed everything in her computer. Then I showed her how to run the program. "You just have to put the images together in a folder. Copy and paste these python scripts into the same folder as before. Then go back one folder. Now click on the folder. Press shift and then right click on the folder. Lastly, click 'Open command window here'. Afterwards, you need to type: 'python measure_object_dimension.py'. Bam! The magic's happened!"

If you were in her shoes, what do you think and feel? I bet you will be as confused as her and also you will not be able to remember every step that you need to take to run the program. That was exactly what she said to me after my step-by-step explanation, "Could you please make it simpler? Like I just have to double-click on an icon then the program is running automatically."

I was baffled. Never heard of converting a .py file to .exe file. Nevertheless, thanks to Google and Python community! Just by a simple googling I found so many alternatives to convert a .py file to an executable file in windows (.exe).

Let us find out how to convert a .py file to an executable windows (.exe) file. You will be amazed by how simple it is actually.

There are several packages to convert python script (.py) to windows executable file (.exe). You may end up with Py2Exe, or cx_Freeze, or PyInstaller, or any other packages out there. For me, I chose PyInstaller. Two reasons: simple and support python3.


I: GET THE PyInstaller (https://github.com/pyinstaller/pyinstaller)

Just yesterday I had trouble with the stable version of the PyInstaller that I downloaded and installed using pip. Therefore, it is not advisable. Instead, I will suggest you to get the development branch of PyInstaller from the GitHub. Please look at the picture below.
Download the development branch of PyInstaller from https://github.com/pyinstaller/pyinstaller

II: INSTALL PyInstaller

Now go to the folder that consists of the downloaded PyInstaller development. Open the command window, then type python setup.py install. Optionally you might activate your virtual environment (env) first before installing the PyInstaller.
Extract the downloaded zip file, open command window, and type the command.
P.S. If you encounter an error in installing the dependencies of PyInstaller, you might reinstall the PyInstaller. However, before reinstalling make sure that you have already installed the dependency that caused error in advance using pip or other way that you prefer. In my case, I encountered an error in installing pypiwin32, thus, PyInstaller did not install successfully during my first attempt. FYI, I am using Python 3.5. Hence, I installed the pypiwin32 first using pip, then I reinstalled the PyInstaller. It went smooth after manually installing the pypiwin32.


III: CONVERT to executable file

In order to convert your python script (.py) to executable file (.exe), you need to keep all the python scripts within a folder and the run the command window there with the following command: pyinstaller --onefile your_python_script.py. Voila! That is all! Optionally you may change the icon image of your executable file using --icon command. So for example, please refer to the figure below.
--onefile means PyInstaller will build only a single executable file. All the other supporting files have been "cramped" into this executable file. --icon is optional. It is to change the icon image. --clean is also optional. It is to clean the cache. I like to use --clean command because for me it is better to start everything clean from the cache.
Now you may go to the dist folder. Inside the folder you will find the executable file (.exe). You may want to double-click the .exe file and see whether it works perfectly as if you are executing the file from the command window with Python. Here is mine.
Get your .exe file inside the dist folder.
Looking at the clock on the wall, it was 5-O'clock in the afternoon. Right on time baby. Saving all my works. Saving the resulted .exe file to a flash drive. Shutting down the computer. Then I passed the flash drive to my labmate.

This time the instruction is short and simple. "You just keep all the images into one folder. Copy and paste this .exe file into the same folder. And then just double-click it." She was extremely happy. I was happy as well because I did not have to participate in a tedious and laborious work.


That is all folks for this time. Now you can distribute your program without having to worry that your end-user will complain to you due to the complexity in executing the program. Happy weekend! As always, thanks for reading. Any comment please leave them down below in the comment section. Cheers!


P.S. If you are searching for the complete code of this program, you may go to my previous post.

2 comments:

  1. Great with detailed information. It is really very helpful for us.
    Village Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
    As a best video production company in Bangalore, we produce quality and creative videos to our clients.

    ReplyDelete
  2. Hey! This blog is more informative and user friendly

    Thank you so much for giving this information to me..
    best acne spot removal cream
    herbal products online shopping

    ReplyDelete