How to install wget in macOS?

install by brew
>ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
>brew install wget --with-libressl

install by port
> sudo port install wget


Enjoy!





tar (child): xz: Cannot exec: No such file or directory on Amazon Linux

yum install -y xz

docker command summarise


stop all containers:
docker kill $(docker ps -q)

remove all containers
docker rm $(docker ps -a -q)

remove all docker images
docker rmi $(docker images -q)

access(enter) docker container
docker exec -it docker_container_name sh

Exit from docker container
>exit

Commit container and build image
docker commit container_name image_name
ex) docker commit lambdapack lambda_image
ex) docker commit lambdapack marearts/lambda_image:v1.0.0

Docker build
>docker build --tag hello:0.1 .
>docker build --tag marearts/hello:0.1 .

Docker run shell and tunnelling 
>docker run -v $(pwd):/outputs --name nickname -d docker_image tail -f /dev/null
ex)
>docker run -v $(pwd):/outputs --name lambdapackgen2 -d marearts/awspy:0.1 tail -f /dev/null

Docker run shell

> docker run -it marearts/amazon_linux_py36:v1.0.0 /bin/bash

Docker run sh file in container
>docker exec -i -t container_name /bin/bash /outputs/shfile.sh
ex)
>docker exec -i -t lambdapackgen2 /bin/bash /outputs/buildPack_py3.sh

Restart Docker container
>docker restart <container id or name>
ex)
docker restart 59



it will be updated..


summarise Azure function docker image deploy



*make python virtualenv
*activate python virtualenv !!
*install azure util cli
https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image#run-the-build-command

make project (app)
func init DocApp100 --docker

change directory
cd DocApp100

new function
func new --name MyHttpTrigger --template "HttpTrigger"

build docker image
docker build --tag marearts/image:v1 .

make new resource group
az group create --name DocGroup --location westeurope

storage account
az storage account create --name docstorage100 --location westeurope --resource-group DocGroup --sku Standard_LRS

create linux service plan
az appservice plan create --name docserviceplan --resource-group DocGroup --sku B1 --is-linux

create app and deploy
az functionapp create --name docapp100 --storage-account docstorage100 --resource-group DocGroup --plan docserviceplan --deployment-container-image-name marearts/image:v1

Configuration the function app
storageConnectionString=$(az storage account show-connection-string --resource-group DocGroup --name docstorage100 --query connectionString --output tsv)
az functionapp config appsettings set --name docapp100 --resource-group DocGroup --settings AzureWebJobsDashboard=$storageConnectionString AzureWebJobsStorage=$storageConnectionString

Test
curl https://docapp100.azurewebsites.net/api/MyHttpTrigger?name=myname


Azure function create command summarise.

reference:
https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local#v2
https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-python


create project
func init PythonAzure

create azure function
func new

Test
func host test

create resource group
az group create --name mareartstest --location westeurope

create strage account
az storage account create --name mareartsteststorage --location westeurope --resource-group mareartstest --sku Standard_LRS

create app
az functionapp create --resource-group mareartstest --os-type Linux --consumption-plan-location westeurope --runtime python --name mareartstest --storage-account mareartsteststorage

deploy
func azure functionapp publish mareartstest

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();
    
}

Elastic image effect, python opencv example source code.

Elastic effect source code.
It can be useful when you want to augment image dataset.
and it is also good to make image effect.

Please refer code:

import numpy as np
import cv2
from scipy.ndimage.interpolation import map_coordinates
from scipy.ndimage.filters import gaussian_filter

def elastic(image, alpha, sigma, random_state=None):
"""Elastic deformation of images as described in [Simard2003]_.
.. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
Convolutional Neural Networks applied to Visual Document Analysis", in
Proc. of the International Conference on Document Analysis and
Recognition, 2003.
"""
if random_state is None:
random_state = np.random.RandomState(None)

#print(random_state)
shape = image.shape
dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma, mode="constant", cval=0) * alpha
dz = np.zeros_like(dx)

x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))

indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1)), np.reshape(z, (-1, 1))
distored_image = map_coordinates(image, indices, order=1, mode='nearest') #wrap,reflect, nearest

return distored_image.reshape(image.shape)



#main
#file read
o_img = cv2.imread('izone_oy.png')
elMat = elastic(o_img, alpha=5000, sigma=8, random_state=None)

cv2.namedWindow('origin',0)
cv2.imshow('origin', o_img)
cv2.namedWindow('elastic',0)
cv2.imshow('elastic', elMat)

cv2.waitKey(0)


GitHub url :



python OpenCV, draw grid example source code

make well divided linear coordinate
And make pair coordinate

Please see code for detail explanation.


import numpy as np
import cv2
import sys

rows = 500
cols = 500
newMat_3ch = np.zeros((rows, cols, 3), dtype = "uint8") #3channel

step = 20
x = np.linspace(start=0, stop=rows, num=step)
y = np.linspace(start=0, stop=cols, num=step)

v_xy = []
h_xy = []
for i in range(step):
v_xy.append( [int(x[i]), 0, int(x[i]), rows-1] )
h_xy.append( [0, int(y[i]), cols-1, int(y[i])] )

for i in range(step):
[x1, y1, x2, y2] = v_xy[i]
[x1_, y1_, x2_, y2_] = h_xy[i]

cv2.line(newMat_3ch, (x1,y1), (x2, y2), (0,0,255),1 )
cv2.line(newMat_3ch, (x1_,y1_), (x2_, y2_), (255,0,0),1 )
cv2.namedWindow('newMat_3ch',0)
cv2.imshow('newMat_3ch', newMat_3ch)
cv2.waitKey(0)




data dump by pickle in python

Data dump by pickle

see below example:

data dump to file 
import pickle
f = open("any_file.dmp', "wb")
f.write(pickle.dumps(data))
f.close()

data load by dump
data = pickle.loads(open("any_file.dmp", "rb").read())