Category: Python

  • Hobby Project: Ai Artwork Enhancer in Python

    Hobby Project: Ai Artwork Enhancer in Python

    I created a program. exe that process and enhances artwork images in batches using AI: the input will be an excel with 3 columns: artwork name, artist name and artwork file name. Then the program enhances the low resolution image using prebuilt models to an 8k ready pdf that can be printed for different purposes, for example a gallery exposition.

    Figure. GUI I built using the PYQT5 picoFramework I developed for personal use. I will be sharing this picoFramework in the future if anyone finds it useful, youc an still do everything with PYQt5 but it might save some setup time if it’s your first time making a GUI with just pure code.

    To accomplish the enhancements of the images I used the following resources:

    *A model that outputs beautiful results in here, Mostly for faces: https://github.com/TencentARC/GFPGAN

    *Another useful library is: mostly for backgrounds: https://github.com/xinntao/Real-ESRGAN

    Demo Sites to see for yourself how this models might work:

    https://app.baseten.co/apps/QPp4nPE/operator_views/RqgOnqV

    https://arc.tencent.com/en/ai-demos/imgRestore

    Examples of the AI scaling:

    Example 1:

    Before:

    With Face upscaling:

    Synthetic Upscaling:

    Example 2:

    Before:

    With Face Upscaling:

    Synthetic Upscaling:

    Common Upscaling methods Included

    In the program I have included alot of different upscaling methods not only AI ones but others like the ones shown in the following table, this is so there’s more choice when processing the images.

    Implementing the machine learning models in your python code!

    In order to implement the machine learning models we can reference this sources:

    Pyinstaller Problems and solutions found when developing the program:

    When bundling with pyinstaller OpenCV was giving me issues I solved it by using python version 3.9.7 and OpenCV Version: 4.5.5.64.

    I had to uninstall the current opeCV with: pip uninstall opencv-python

    Then I used: pip3 install “opencv-python==4.4.4.64” —force-reinstall all this in a virtual environment.

    If the correct version is not installed pyinstaller will refuse to bundle the program: I had to uninstall everything with the following commands because at first I had the default opencv version:

    pip uninstall opencv-python
    pip uninstall opencv-contrib-python
    

    Then we can install the proper contrib version:

    pip3 install "opencv-contrib-python==4.5.5.64" --force-reinstall
    

    Model Research papers & Program Documentation/Usage

    Important:

    -If needed the font can be changed by replacing the “font.ttf” file(name must be font.ttf) -The Upsampling models included are EDSR, ESPCN, FSRCNN, LapSRN -At the moment only CPU can be used as GPU Support requires custom OpenCV Compilation. -The research papers on how this models work can be found in the following links:

    https://arxiv.org/pdf/1707.02921.pdf

    https://arxiv.org/pdf/1609.05158.pdf

    https://arxiv.org/pdf/1608.00367.pdf

    https://arxiv.org/pdf/1710.01992.pdf

    -It is recommended for bulk processing to use ESPCN or FSRCNN as this are the less taxing models. -EDSR might crash computers with less than 32gb of ram. Processing times per iamge are greater than 30 minutes. -The bigger the source image the more time the models will take to process them. -For Really small or low res pictures LapSRN can be used without to many worries.

    **WARNING, only use LapSRN with low res images, as the greater the image resolution the greater the resource intake. **WARNING, EDSR requires a computer with more than 32gb RAM, otherwise the system might crash or hang. Processing time per image is 30 minutes or longer.

    Python Code Snippet to make AI enhancement work

    Now the fun part this is how to use this models in your python program:

    The models themselves are pretty lightweight we just need to place them inside our working folder or reference them.

    We can define a function that handles the resize procedure:

    import cv2
    
    def process_machine(img,model_type,outpath_ML):
        
        # Read image and upscale it
        img = cv2.imread(img)
        sr = cv2.dnn_superres.DnnSuperResImpl_create()
        
        #If model_type is "EDSR" then
        if model_type == "EDSR":
            model = "EDSR_x4.pb"
            sr.readModel(model)
            sr.setModel("edsr",4)
    
        #If model_type is "ESPCN" then   
        elif model_type == "ESPCN":
            model = "ESPCN_x3.pb"
            sr.readModel(model)
            sr.setModel("espcn",3)
        
        #If model_type is "FSRCNN" then
        elif model_type == "FSRCNN":
            model = "FSRCNN_x3.pb"
            sr.readModel(model)
            sr.setModel("fsrcnn",3)
        
        #If model_type is "LAPSRN" then
        elif model_type == "LAPSRN":
            model = "LapSRN_x8.pb"
            sr.readModel(model)
            sr.setModel("lapsrn",8)
        
        result = sr.upsample(img)
        cv2.imwrite(outpath_ML,result)
    
    process_machine(image1_path,"ESPCN",outpath_risizer)
    

    Is that easy!

  • Using Cloudflared & Python for remote executing stuff on your closed home network!

    Using Cloudflared & Python for remote executing stuff on your closed home network!

    As you might be aware Cloudflare recently started offering a tunnel so that you could access your home network from your own domain, this is great news as before we had to open ports or made more complex configurations to make this work.

    So one of the first uses that comes to mind is remote executing stuff on your computer when your away! this opens so many possibilities. I created a python program that basically is a webhook server. It keeps listening for requests and once it has those it will open a program in your local machine! How cool that sounds!

    Th final program looks like this:

    I will go in more detail about how this webhook program works in another post, this post is mostly about how to setup Cloudflared and quicktunnels if you want to skip the creation of a domain.

    Let’s get into the details

    In order to expose a webhook server so it can be accessed from an external source we need to allow external traffic into our network, this is most of the times very risky so in order to allow traffic and not expose ourselves by opening ports in our router and firewall, we can use a tunneling solution, one that’s easy to set up and free to use is the one that is provided by Cloudflare called Cloudflared.

    To make this work the summarized steps are:

    • Get a free domain name *Create a cloudflare account *Add domain name to cloudflare account, point nameservers to cloudflare. *Setup tunnel inside cloudflare account *Setup cloudflared tunnel inside pc.

    To install it on other machines only the last two steps are needed.

    A guide on how to install CloudFlared can be found in the following links:

    https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/tunnel-guide/remote/

    https://www.makeuseof.com/use-cloudflare-tunnel-expose-local-servers-internet/

    After setting up the tunnel we can just start the webhook server, and every request sent to the domain configured when creating the tunnel in Cloudflared will be received by our webhook server.

    Some common webhook parameters to configure in the solution that sends the webhooks are:

    API Endpoint URL:

    This needs to be the domain you configured when setting up your tunneling solution) (For example: www.mydomain.com/webhook/docx)

    Payload Type:

    Json

    Wrap Request In Array:

    No

    Authentication:

    Basic Authentication (The user and password you configured in the webhook server configuration tab)

    Add Headers,Set parameters, etc :

    Unchecked

    Webhook Server Configuration

    In order to use the webhook server we need to define some parameters.

    Host: the default ip address where the webhook server will run: 127.0.0.1 is the default as this means “run on the local machine”.

    Port: You can choose any port you want that is unused.

    Username: the username used for basic authentication when sending webhooks from the outside world.

    Password: the password used for basic authentication when sending webhooks from the outside world.

    URL_Prefix: if no URL prefix is desired for the webhook receiving url, then just input “

    Adding new webhook endpoints: Then in the part below you can associate any webhook endpoint to any file you want in your computer, and the server will open the file whenever a new webhook in this endpoint, windows will use the default program associated with the file extension of this file.

    Thne just hit run and you should start receiving webhooks on your machine!

    Setting up Cloudflared without a Domain.

    To bypass setting up a domain name: (using Quick tunnels) If used for production setting up a domain name is recommended.

    https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/do-more-with-tunnels/trycloudflare/

    Steps to create a Quick Tunnel.

    -Go to https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation/

    -Download The CloudFlared Tunnel Setup

    -Grab the .MSI version x64 from here: https://github.com/cloudflare/cloudflared/releases and install it.

    -Open the command line and run the following command specifying the port where the webhook server is running:

    cloudflared tunnel --url <http://localhost:7000>
    

    -Copy this address to setup the webhook for example:

    -Then webhooks can be received by sending webhooks to this address and adding basic authentication.:

  • How to pass an argument to an RPA Uipath robot from cmd

    How to pass an argument to an RPA Uipath robot from cmd

    I was working on one Uipath robot and needed to strap it together to other scripts: some python files and other xaml to prototype a solution.

    One quick way to test your Uipath robots without having to open the main GUI is by executing the main xaml file from the cmd. 

    To achieve this take a look at the format with this example:

    cd C:\000OfficialScraps
    mkdir %date:~-4,4%%date:~-10,2%%date:~7,2%
    Xcopy C:\000SCRAPPER C:\000OfficialScraps\%date:~-4,4%%date:~-10,2%%date:~7,2% /E /H /C /I
    set folder= C://000OfficialScraps//%date:~-4,4%%date:~-10,2%%date:~7,2%
     
    "C:\Users\Computer\AppData\Local\Programs\UiPath\Studio\UiRobot.exe" execute --file "C:\Users\Computer\Documents\UiPath\BestsellerScrapper\Main.xaml" --input "{'ArgRoot':'%folder%'}"
    cd  C:\000OfficialScraps\%date:~-4,4%%date:~-10,2%%date:~7,2%
    python FilterBestSellers.py
    python Bestseller2Module1AfterSelection.py

    The script is used to scrap some website data, but you can ignore that. First I’m creating a new folder based on the current time. Then I store this variable inside the “folder” variable. Then I pass this variable as an argument to the Uipath robot.

    I have used this method for quick prototyping while in the “development of the robot” phase.

    Then finally I execute some python scripts from here rather than from the robot itself, but this is just personal preference, I bet there are better ways to do it. But just to get the job done this is what you could use to strap together with a quick solution while in the development stage.