9/24/2018

python opencv webcam canny edge, laplacian and adaptive threshold test

source code is here


import numpy as np
import cv2

from matplotlib import pyplot as plt
cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    if frame is None:
        continue

    frame = cv2.resize(frame, (320,240))
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray.copy(),threshold1=50, threshold2=150,apertureSize = 3)


    #sobel
    laplacian = cv2.Laplacian(gray.copy(),cv2.CV_8U,13)
    #kernel = np.ones((3,3),np.uint8)
    #laplacian = cv2.dilate(laplacian,kernel,iterations = 1)

    im_th1 = cv2.adaptiveThreshold(gray.copy(), 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \
                                   cv2.THRESH_BINARY, 5, 2)
    
    # Display the resulting frame
    #edges = cv2.Canny(gray.copy(),threshold1=50, threshold2=150,apertureSize = 3)
    tempImg = cv2.medianBlur(gray, 5)
    im_th1 = cv2.adaptiveThreshold(tempImg, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \
                                cv2.THRESH_BINARY, 5, 2)

    blur = cv2.GaussianBlur(im_th1, (5, 5), 0)
    _, im_th2 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    
    
    numpy_horizontal = np.hstack((gray, edges, laplacian, im_th2))

    cv2.imshow('Numpy Horizontal', numpy_horizontal)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


result



9/22/2018

QRcode & Barcode generation example source code by Python



#########################
#qr code creator example
#pip install pillow
#pip install qrcode
#https://pypi.org/project/qrcode/# 
import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)
qr.add_data('http://webapp.marearts.com')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("qr.png")


#########################
#barcode creator example
#pip install python-barcode
#https://pypi.org/project/python-barcode/

import barcode
from barcode.writer import ImageWriter
EAN = barcode.get_barcode_class('ean13')
ean = EAN('5901234123457', writer=ImageWriter()) #13 digits number only
fullname = ean.save('barcode')

you can test this source code on here: http://www.marearts.com/webapp/qrcode/






9/18/2018

How to make Mat in opencv python

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

Mat is just numpy array.

9/17/2018

python get file list in directory

from os import listdir
from os.path import isfile, join
mypath = "./customer_sample"
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
print(onlyfiles)

result
['._s1.pdf', 's2.pdf', '._s2.pdf', '._s7.pdf', 's8.pdf', '._s8.pdf', '._s9.pdf', 's10.pdf', '._s10.pdf', 's1_p0.jpg', 's9.pdf', '._s1_p0.jpg', '._s4.pdf', 's5.pdf', '._s5.pdf', 's6.pdf', '._s6.pdf', 's7.pdf', '._s3.pdf', 's4.pdf', 's2_p0.jpg', 's2_p1.jpg', 's3_p0.jpg', 's3.pdf', 's3_p1.jpg', 's3_p2.jpg', 's1.pdf', 's3_p3.jpg']

9/15/2018

python 1 line code for N random number (tip)

Make 1~100 random integer as 10 loops


import random
myr = [random.randrange(1, 100) for _ in range(10)]
print(myr)

result
[9, 67, 32, 55, 79, 47, 86, 62, 95, 30]

Thank you. ^^