How to protect my Python scripts on Raspberry Pi? - python

I have developed a long and complex computer vision algorithm on Python. I insist on the "long and complex" because my very last solution would be to rewrite it with C++ (it's about 3 000 lines long and depends on pure Python libraries)...
This algorithm is embedded on a Raspberry Pi A+. It is possible to plug an SD card to this nanocomputer, so that it includes a Linux system.
So basically my script is in a folder in that SD. When the Raspberry Pi is switched on, it runs the system and then executes my code.
Let's say someone retrieve this SD card. How can I be sure that he won't be able to get to my script ?
I've read it was difficult to obfuscate Python code, is it still true ? Let's say I would rename all of my variables with absurd names, to "scramble the tracks". Are there programs that can retrace the structure of the whole algorithm ?
Regarding the SD card itself, is it possible to protect it ?
What I am actually doing is I deactivate the HDMI and USB port when the Raspberry Pi is launched, and protect the SSH connexion with a password.
Do you think this is enough, or is there other ways someone can get to the script ?
Finally, rewritting it in C++... Would it ensure me that it would be 100% protected ?!
Thank you in advance. :)

I think your best solution would be to compile your Python file using a bundling tool.
See Compiling Python Code.
For example using PyInstaller
pip install pyinstaller
pyinstaller yourprogram.py
Although you can never (even with C++) guarantee full "protection". There are techniques like reverse engineering that can be used to reveal your algorithm.

Related

How to get libCEC for Python (PyCharm on Windows)?

I am unfortunately an absolute beginner in programming.
First of all, I'm programming with the PyCharm IDE on a Windows computer, but the script will later run on a RaspberryPi.
I want to turn on my TV with a Python script via HDMI-CEC and I came across libcec.
However, I can't figure out the setup instructions for Windows.
https://github.com/trainman419/python-cec
Can anyone give me a step by step guide on what exactly I need to do to use the CEC commands in Python?
Or should I program with PyCharm directly on the RaspberryPi? This one is way too slow for this task in my opinion. Then I could install the CEC library very easily.
Thanks for your help.

Distribute a standalone Python software with Julia dependencies

I have a software that is mostly written in Python and, for now, I'm using PyInstaller to bundle and distribute the software in a user-friendly way (it's part of my CI pipeline, for Linux and Windows).
However, my performance is terrible and I want to rewrite some heavy parts in Julia while keeping the front-end in Python. I can use PyJulia to do this, but it means that the user has to install Julia manually in order to use my program.
Julia does have the equivalent of PyInstaller, which is PackageCompiler.jl, but I don't know how to call something compiled with PackageCompiler.jl from the Python side.
How can I make this work, so I can bundle and distribute an executable that has Python, Julia and everything it needs to run?
A little more details
My end user is someone (chemists and pharmacists) that have no idea what programming is. They don't have Python, Julia or Docker (and they don't even want to install it).
In my current approach, the software bundled with PyInstaller consists of a single executable with everything inside it (Python and everything it needs). What I really want is to keep the same user experience, but also with Julia running on the backstage.
I'll implement several functions in the Julia side, and I want (almost) the same level of integration as I get with PyJulia.
Maybe I'll go to Rust and just use the C interface, but I really would like to use Julia.
Thank y'all for your time.
Per here: https://julialang.github.io/PackageCompiler.jl/stable/apps.html#Creating-an-app-1 you can get what is basically an executable file and then you can just follow this post: Python Script execute commands in Terminal to execute the file you create.
You could try using JuliaWin. This provides a standalone Julia runtime that works fully self-contained.
We deployed a tool using PyInstaller with the JuliaWin included as one of the datas as PyInstaller calls them. Iirc it was not PyJulia but JuliaCall+PythonCall doing the interfacing in our case.
Unfortunately the startup was ridiculous (order of minutes). This is in part due to Julia's startup time, but largely due to the generated .exe first having to unpack the JuliaWin. So we are currently investigating using PackageCompiler, possibly also switching to providing the user with 2 files instead of 1.

Sapi5 on linux?

I am working on creating an AI for myself and I chose python as the programming language to code it in. I am an absolute beginner in programming and have very little knowledge of the subject. After spending months of coding I finally created an AI by myself that is able to run and I did so on windows. I wanted to move this program to a raspberry pi but turns out that I am unable to use Sapi5 TTS module from pyttsx3 on linux as it requires the win32com files to function. I am aware that pywin32 cannot be run on linux as it is a different OS and I want to know if there is a way for me to be able to run sapi5 on linux? I have tries using different tts modules such as espeak but they are too robotic and I really like sapi5 so it would be great if there was a way for me to be able to use it, thank you for your time!!
Sapi5 is a built-in part of Microsoft Windows and you can't run it in Linux. I agree it's a pretty good speech synthesizer. You may consider using Mycroft/mimic, though I found the build process tricky, or use gTTS if your utterances are short and you'll have internet access.

MATLAB to web app

Hi I have a MATLAB function that graphs the trajectory of different divers (the Olympic sport diving) depending on the position of a slider at the bottom of the window. The file takes multiple .mat files (with trajectory information in 3 dimensions) as input. I am trying to put this MATLAB app on to the internet. What would be the easiest/most efficient way of doing this? I have experience programming in Python and little experience programming in Java.
Here are the options that I have considered:
1. MATLAB Builder JA (too expensive)
2. Rewrite entire MATLAB function into Java (not experienced enough in Java)
3. Implement MATLAB file using mlabwrapper and using Django to deploy into web app. (having a lot of trouble installing mlabwrapper onto OSX)
4. Rewrite MATLAB function into Python using SciPy, NumPy, and matlibplot and then using Django.
I do not have any experience with Django but I am willing to learn it. Can someone point me in the right direction?
A cheap and somewhat easy way (with limited functionality) would be:
Install MATLAB on your server, or use the MATLAB Compiler to create a stand alone executable (not sure if that comes with your version of MATLAB or not). If you don't have the compiler and can't install MATLAB on your server, you could always go to a freelancing site such as elance.com, and pay someone $20 to compile your code for you into a windows exe file.
Either way, the end goal is to make your MATLAB function callable from the command line (the server will be doing the calling) You could make your input arguments into the slider value, and the .mat files you want to open, and the compiled version of MATLAB will know how to handle this. Once you do that, have the code create a plot and save an image of it. (using getframe or other figure export tools, check out FEX). Have your server output this image to the client.
Tah-dah, you have a crappy low cost work around!
I hope this helps , if not, I apologize!
You could always just host the MATLAB code and sample .mat on a website for people to download and play with on their own machines if they have a MATLAB license. If you are looking at having some sort of embedded app on your website you are going to need to rewrite your code in another language. The project sounds doable in python using the packages you mentioned however hosting it online will not be as simple as running a program from your command line. Django would help you build a website but I do not think that it will allow you to just run a python script in the browser.

How to run a Python script in something other than cmd?

I have written a program. I don't know if it is important how it is written but you can find it here: http://pastebin.com/Z3ZvVPV8 Basically, it asks you to assign values to variables and will perform calculations depending on what variables you chose, and prints the answer.
I would like to know how I can make the program run in a window other than cmd (I am using Windows Vista 32bit). I don't need much at all in terms of GUI, just a window that is a bit more user friendly/easier to look at when they are using the program.
EDIT: To those suggesting using IDLE, while that would work for me, if others want to use the program they would have to download it, so I was hoping for a way for that not to happen.
Python comes with a sort of default GUI package TkInter you can use it.
Also there is a lot of other GUI packages available.
The Python standard library offers a lot of ways to implemt simple (but also rather complex) GUIs. I'd like to point you at the documentation of TK (tool kit for graphical interfaces) http://docs.python.org/library/tk.html where you will find also some useful example of use.
Py2Exe is a viable option if you really don't need a gui. This will make it run and look like a command prompt, but it will be an .exe file. Here is a quick quote from thier page: "py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation."
Another alternative is to get Portable Python. Here is a quote from thier webpage: "Portable Python is a Python® programming language preconfigured to run directly from any USB storage device, enabling you to have, at any time, a portable programming environment. Just download it, extract to your portable storage device or hard drive and in 10 minutes you are ready to create your next Python® application." After packaging the portable python and your .py or .pyc file then create a .bat file that runs the portable python "Python-Portable.exe" with the correct command line parameters for loading your script. Be sure to use relative paths in the batch file in case they are running it from a flash drive, or something other than the same location as you.
NOTE: This is really not a good way to do this as thier download page states: "Installed size: based on selected packages, between 49MB and 480MB". Also be sure to read the the current Python Software Foundation License, as that is what Portable Python is released under, and it may or may not be legal to package it in a closed source project. I haven't really looked at the license myself to be able to tell you. If you are releasing it as open source, then there would not be an issue though. As a quick side note, if you need that .bat file to be a .exe file then you can use a .bat to .exe converter battoexe.com is one. This is really going the long way about doing the whole thing, but it is an option.
Sources:
Working with Python on and off for 7 years now, a lot that using a portable version on a flash drive, and also dealing with Batch files much longer.

Categories

Resources