def solveEqsFromWebcam(withRespectTo): symbols = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '=', 'x', '+', '-', 'y' ] json_file = open('models/model_balanced.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights("models/model_balanced.h5") classifier = ClassifierKeras(symbols, loaded_model) cv2.namedWindow("preview", cv2.WINDOW_NORMAL) vc = cv2.VideoCapture(0) if vc.isOpened(): rval, frame = vc.read() else: rval = False while rval: cv2.resizeWindow("preview", 640, 480) rval, frame = vc.read() key = cv2.waitKey(20) if key == 27: # ESC break # treat frame cv2.imwrite("screencap.jpg", frame) img = ip.getImgMat("screencap.jpg") chars = ip.extractCharacters(img, (45, 45)) for char in chars: classifier.classify(char) lines = parseEquation(chars) drawSolvedEquations(lines, frame, withRespectTo) cv2.imshow("preview", frame) cv2.destroyWindow("preview")
def solveEqFromImage(withRespectTo): img = ip.getImgMat('images/twoEq.jpg') chars = ip.extractCharacters(img, (45, 45)) lines = parseEquation(chars) symbols = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '=', 'x', '+', '-', 'y' ] json_file = open('models/model_balanced.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights("models/model_balanced.h5") classifier = ClassifierKeras(symbols, loaded_model) print("Equations:\n") for line in lines: for group in line: str = "" for c in group: classifier.classify(c) str += c.symbol print(str, end="\t") print("") print("\nSolutions:\n") eqs = toSymPyFormat(lines) for eq in eqs: solveSingleEq(eq, withRespectTo) img = cv2.pyrDown(img) cv2.imshow("test", img) if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows()
def classifyCharsFromWebcam(): symbols = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '=', 'x', '+', '-', 'y' ] json_file = open('models/model_balanced.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights("models/model_balanced.h5") classifier = ClassifierKeras(symbols, loaded_model) cv2.namedWindow("preview", cv2.WINDOW_NORMAL) vc = cv2.VideoCapture(0) if vc.isOpened(): rval, frame = vc.read() else: rval = False while rval: cv2.resizeWindow("preview", 640, 480) rval, frame = vc.read() key = cv2.waitKey(20) if key == 27: # ESC break #Process frame cv2.imwrite("screencap.jpg", frame) threshed_img = ip.getImgMat("screencap.jpg") characters = ip.extractCharacters(threshed_img, (45, 45)) imgWithBounds = ip.drawCharacterBounds(frame, characters) classifiedImg = ip.drawClassifiedCharacters(imgWithBounds, classifier, characters) cv2.imshow("preview", classifiedImg) cv2.destroyWindow("preview")
def classifyCharsFromImage(): symbols = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '=', 'x', '+', '-', 'y' ] json_file = open('models/model_balanced.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights("models/model_balanced.h5") classifier = ClassifierKeras(symbols, loaded_model) imgPath = 'images/twoEq.jpg' img = ip.getImgMat(imgPath) chars = ip.extractCharacters(img) imgRect = img.copy() ip.drawCharacterBounds(imgRect, chars) ip.drawClassifiedCharacters(imgRect, classifier, chars) cv2.imshow("Classifications", imgRect) if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows()
import Network as nw import imageProcessor as ip import Classifier as cl import numpy as np import cv2 import mnist_loader imgMat = ip.getImgMat('ex.png') chars = ip.extractCharacters(imgMat, (28, 28)) #net = nw.Network([784,100,10]) trainingData, validationData, testData = mnist_loader.load_data_wrapper() #net.train(trainingData, 3 ,10, 3.0, testData = testData) net = nw.load('mnist.json') a = testData[0] print(a) #print(imgMat) clas = cl.Classifier(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], net) for c in chars: clas.classify(c) print(c.symbol) cv2.putText(imgMat, c.symbol, (c.xPos, c.yPos), cv2.FONT_HERSHEY_SIMPLEX, 2, 0) cv2.imshow("test", imgMat) if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows()