knettercalculator

Automatic calculator for the Math (and Physics) OneNote Script made by my teacher Mr. Knetter
git clone git://archive.git.mtrnord.blog/MTRNord/knettercalculator.git
Log | Files | Refs | README

commit c331325541ffc1fa6b56efc91c69cae6ef1574a4
parent 08c0a99e81744a130725213759ea815f15bb07e6
Author: Marcel <MTRNord@users.noreply.github.com>
Date:   Fri,  7 Oct 2016 22:38:25 +0200

Change path to learning image and rename to actual name
Diffstat:
Dcode/main.py | 45---------------------------------------------
Acode/training.py | 45+++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/code/main.py b/code/main.py @@ -1,45 +0,0 @@ -############## Code from: http://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python ################# -import sys - -import numpy as np -import cv2 - -im = cv2.imread('pitrain.png') -im3 = im.copy() - -gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) -blur = cv2.GaussianBlur(gray,(5,5),0) -thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2) - -################# Now finding Contours ################### - -contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) - -samples = np.empty((0,100)) -responses = [] -keys = [i for i in range(48,58)] - -for cnt in contours: - if cv2.contourArea(cnt)>50: - [x,y,w,h] = cv2.boundingRect(cnt) - - if h>28: - cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2) - roi = thresh[y:y+h,x:x+w] - roismall = cv2.resize(roi,(10,10)) - cv2.imshow('norm',im) - key = cv2.waitKey(0) - - if key == 27: # (escape to quit) - sys.exit() - elif key in keys: - responses.append(int(chr(key))) - sample = roismall.reshape((1,100)) - samples = np.append(samples,sample,0) - -responses = np.array(responses,np.float32) -responses = responses.reshape((responses.size,1)) -print "training complete" - -np.savetxt('generalsamples.data',samples) -np.savetxt('generalresponses.data',responses) diff --git a/code/training.py b/code/training.py @@ -0,0 +1,45 @@ +############## Code from: http://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python ################# +import sys + +import numpy as np +import cv2 + +im = cv2.imread('images/NumberLearning.png') +im3 = im.copy() + +gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) +blur = cv2.GaussianBlur(gray,(5,5),0) +thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2) + +################# Now finding Contours ################### + +contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) + +samples = np.empty((0,100)) +responses = [] +keys = [i for i in range(48,58)] + +for cnt in contours: + if cv2.contourArea(cnt)>50: + [x,y,w,h] = cv2.boundingRect(cnt) + + if h>28: + cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2) + roi = thresh[y:y+h,x:x+w] + roismall = cv2.resize(roi,(10,10)) + cv2.imshow('norm',im) + key = cv2.waitKey(0) + + if key == 27: # (escape to quit) + sys.exit() + elif key in keys: + responses.append(int(chr(key))) + sample = roismall.reshape((1,100)) + samples = np.append(samples,sample,0) + +responses = np.array(responses,np.float32) +responses = responses.reshape((responses.size,1)) +print "training complete" + +np.savetxt('generalsamples.data',samples) +np.savetxt('generalresponses.data',responses)