python "argparse" simple usage


If you use "argparse" lib, you can get the argument parameters very easily in python.
Let's look below code.

test1.py
import argparse

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
#param add
ap.add_argument("-z", "--zip", type=int, required=True, help="input zip code")
#param add
ap.add_argument("-n", "--name", type=str, required=True, help="input your name")

#get param
args = ap.parse_args()
print(args.zip)
print(args.name)
print('---------')

#get param
args = vars(ap.parse_args())
print(args["zip"])
print(args["name"])
..

The code accepts 2 arguments those are 'zip' and 'name'
So how to input argument?

See the several example

>python test1.py -z 1234 -n kjh
1234
kjh
---------
1234
kjh

>python test1.py
usage: test1.py [-h] -z ZIP -n NAME
test1.py: error: the following arguments are required: -z/--zip, -n/--name


>python test1.py -h
usage: test1.py [-h] -z ZIP -n NAME
optional arguments:
  -h, --help            show this help message and exit
  -z ZIP, --zip ZIP     input zip code
  -n NAME, --name NAME  input your name

you can send argument param
-z or --zip for zip code
-n or --name for name string


Thank you.

reference : https://docs.python.org/3/library/argparse.html


pipenv command summarising.

referenced from :

install
$ pip install pipenv
 brew install pipenv

make virtualenv
$ mkdir myenv
$ cd myenv
$ pipenv --python 3.6


install requirements pipenv
$ cd my_project
$ pipenv install

pipenv package install & uninstall
$ pipenv install beautifulsoup4
 pipenv uninstall beautifulsoup4

freeze → pip freeze > "requirements.txt"
$ pipenv lock

install package to development version
$ pipenv install --dev pytest


install development version requirements 
$ pipenv install --dev

activate pipenv virtualenv
$ pipenv shell

deactivate pipenv virtualenv
$ exit


run pipenv without activate
$ pipenv run which python
$ pipenv run python my_project.py


Python interpreter setting in Visual studio code 

Table(grid) detector


Table(grid) detector source code.
The code find table row & col lines in document image.

Output




Table(Grid) Detector
buy : https://www.cvlecture.marearts.com/product-page/table-grid-detector
test : http://www.marearts.com/webapp/dtable/
github : https://github.com/MareArts/DTable

Usage source code
#include "DTable.h"

//for local computer
void main()
{

    CDTable cDT;

    //test image
    char str[255];
    for (int i = 0; i < 10; ++i)
    {
        
        //make file string
        sprintf_s(str, "%d.jpg", i + 1);
        printf("%s open \n", str);

        //read
        Mat oimg = imread(str);

        //check
        if (oimg.empty())
            continue;

        //get rect vector
        vector< GridV> vRect;
        //dtable
        if (cDT.FindGrid(oimg, vRect, 0, 0) == false)
        {
            printf("Couldn't Find \n");
            break;
        }


        //show table
        namedWindow("oimg", 0);
        int idx = 0;
        for (auto it : vRect)
        {

            printf("%d grid :  (x:%d, y:%d, width:%d, height:%d \n", idx, it.rect.x, it.rect.y, it.rect.width, it.rect.height);

            if (it.pure)
                cv::rectangle(oimg, it.rect, CV_RGB(0, 255, 0), 2);
            else
                cv::rectangle(oimg, it.rect, CV_RGB(255, 0, 0), 2);

            idx++;
        }

        vRect.clear();

        //exit
        imshow("oimg", oimg);
        if (waitKey(0) == 'q')
        {
            break;
        }
    }

    destroyAllWindows();
    
}