import math
from sympy import symbols, Limit # pip install sympy
x, a, h = symbols('x, a, h')
fx = 3 * (x**2) - 4 * x + 1 # 함수 f(x) 정의
fxa = fx.subs({x: a}) # f(x)에 x = a 대입
fxh = fx.subs({x: a + h}) # f(x)에 x = a + h 대입
result = Limit( (fxh - fxa)/h, h, 0 ).doit() # 극한값(미분계수) 계산
print(fx)
print(fxa)
print(fxh)
print("미분 Result:", result)
'''
reference link : https://junyoung-jamong.github.io/computer/vision,/ocr/2019/01/30/Python%EC%97%90%EC%84%9C-Tesseract%EB%A5%BC-%EC%9D%B4%EC%9A%A9%ED%95%B4-OCR-%EC%88%98%ED%96%89%ED%95%98%EA%B8%B0.html
Have to download OCR program firstly.
Link : https://github.com/UB-Mannheim/tesseract/wiki
environment : Window 10, python 3.6
tesseract ver : v5.0.0.20190526.exe
- 이미지 글자 추출 테스트
cmd > tesseract path\name.png stdout
- 패키지 설치
pip install pillow
pip install pytesseract
pip install opencv-python
'''
import cv2
import os
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
# 설치한 tesseract 프로그램 경로 (64비트)
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
# 32비트인 경우 => r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
# 이미지 불러오기, Gray 프로세싱
image = cv2.imread("digit.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# write the grayscale image to disk as a temporary file so we can
# 글자 프로세싱을 위해 Gray 이미지 임시파일 형태로 저장.
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
# Simple image to string
text = pytesseract.image_to_string(Image.open(filename), lang=None)
os.remove(filename)
print(text)
cv2.imshow("Image", image)
cv2.waitKey(0)
동일 디렉터리에 name.png 라는 파일이 있다고 가정하면, 이 코드가 시행되고나서 그 파일의 색상들이 강조된다.
import cv2
'''
함수) 이미지를 더 선명하게 Contrast(대조) 기법을 적용시킴.
param : 컬러 이미지
return : 대조된 이미지
'''
def img_Contrast(img):
# -----Converting image to LAB Color model-----------------------------------
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
# -----Splitting the LAB image to different channels-------------------------
l, a, b = cv2.split(lab)
# -----Applying CLAHE to L-channel-------------------------------------------
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
cl = clahe.apply(l)
# -----Merge the CLAHE enhanced L-channel with the a and b channel-----------
limg = cv2.merge((cl, a, b))
# -----Converting image from LAB Color model to RGB model--------------------
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
return final
img = cv2.imread('name.png')
img = img_Contrast(img)
cv2.imshow('a', img)
cv2.waitKey(0)
먼저 바코드를 인식하는 open source 라이브러리를 제공해주는데, 그 라이브러리를 pip을 이용해 다운로드 한다.
윈도우의 cmd 창을 이용해서 다운받아본다.
총 3개의 라이브러리가 필요하다.
1. openCV (영상인식 라이브러리)
pip install opencv-python
2. numpy (연산 처리 라이브러리)
pip install numpy
3. pyzbar (바코드 인식 관련 라이브러리)
pip install pyzbar
반응형
자 이제 필요한 모든 라이브러리를 받았으니 아래와 같이 코드를 동작시켜본다.
'''
made by 안산드레이아스
https://ansan-survivor.tistory.com/
'''
# Python Version 3.7.1
import pyzbar.pyzbar as pyzbar # pip install pyzbar
import numpy as np # pip install numpy
import cv2 # pip install opencv-python
# 바코드 탐지하는 엔진 (바코드 및 QR코드 탐지)
def decode(im):
# Find barcodes and QR codes
decodedObjects = pyzbar.decode(im)
# Print results
for obj in decodedObjects:
print('Type : ', obj.type)
print('Data : ', obj.data, '\n')
return decodedObjects
# Display barcode and QR code location
def display(im, decodedObjects):
# Loop over all decoded objects
for decodedObject in decodedObjects:
points = decodedObject.polygon
# If the points do not form a quad, find convex hull
if len(points) > 4:
hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
hull = list(map(tuple, np.squeeze(hull)))
else:
hull = points;
# Number of points in the convex hull
n = len(hull)
# Draw the convext hull
for j in range(0, n):
cv2.line(im, hull[j], hull[(j + 1) % n], (255, 0, 0), 3)
# Display results
cv2.imshow("Results", im);
cv2.waitKey(0);
# 파일명 zbar.jpg의 이미지에서 바코드를 탐지하면 해당 코드를 리턴
# Main
if __name__ == '__main__':
# Read image
im = cv2.imread('zbar.jpg')
decodedObjects = decode(im)
display(im, decodedObjects)
이미지 파일 zbar.jpg는 현재 파이썬 파일과 동일 경로에 있어야 한다. (이부분은 절대경로로 수정가능)