return True else: return False def wrap_digit(rect): x,y,w,h=rect padding=5 hcenter=x+w/2 vcenter=y+h/2 if(h>w): w=h x=hcenter-(w/2) else: h=w y=vcenter=(h/2) return x - padding, y - padding, w + padding, h + padding ann ,test_data=ANN.train(ANN.create_ANN(56),20000) font = cv2.FONT_HERSHEY_SIMPLEX path="images/numbers.jpg" img=cv2.imread(path,cv2.IMREAD_UNCHANGED) bw =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) bw=cv2.GaussianBlur(bw,(7,7),0) ret,thbw=cv2.threshold(bw,127,255,cv2.THRESH_BINARY_INV) thbw=cv2.erode(thbw,np.ones((2,2),np.uint8),interations=2) image,cntrs,hier=cv2.findContours(thbw.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) rectangles =[] for c in cntrs: r=x,y,w,h=cv2.boundingRect(c) a=cv2.contourArea(c)
def wrap_digit(rect): x, y, w, h = rect padding = 5 hcenter = x + w/2 vcenter = y + h/2 roi = None if (h > w): w = h x = hcenter - (w/2) else: h = w y = vcenter - (h/2) return (x-padding, y-padding, w+padding, h+padding) # ann, test_data = ANN.train(ANN.create_ANN(56), 50000, 5) ann, test_data = ANN.train(ANN.create_ANN(58), 50000, 5) font = cv2.FONT_HERSHEY_SIMPLEX # path = "./images/MNISTsamples.png" path = "./images/numbers.jpg" img = cv2.imread(path, cv2.IMREAD_UNCHANGED) bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) bw = cv2.GaussianBlur(bw, (7,7), 0) ret, thbw = cv2.threshold(bw, 127, 255, cv2.THRESH_BINARY_INV) thbw = cv2.erode(thbw, np.ones((2,2), np.uint8), iterations = 2) image, cntrs, hier = cv2.findContours(thbw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) rectangles = [] for c in cntrs: r = x,y,w,h = cv2.boundingRect(c)
roi = None if (h > w): w = h x = hcenter - (w // 2) else: h = w y = vcenter - (h // 2) padding = 5 x -= padding y -= padding w += 2 * padding h += 2 * padding return x, y, w, h ann, test_data = ANN.train(ANN.create_ANN(60), 50000, 10) font = cv2.FONT_HERSHEY_SIMPLEX img_path = "./digit_images/digits_0.jpg" img = cv2.imread(img_path, cv2.IMREAD_COLOR) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.GaussianBlur(gray, (7, 7), 0, gray) ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV) erode_kernel = np.ones((2, 2), np.uint8) thresh = cv2.erode(thresh, erode_kernel, thresh, iterations=2) if OPENCV_MAJOR_VERSION >= 4: # OpenCV 4 or a later version is being used.
def wrap_digit(rect): x, y, w, h = rect padding = 5 hcenter = x + w / 2 vcenter = y + h / 2 if (h > w): w = h x = hcenter - (w / 2) else: h = w y = vcenter - (h / 2) return (x - padding, y - padding, w + padding, h + padding) ann, test_data = ANN.train(ANN.create_ANN(56), 20000) font = cv2.FONT_HERSHEY_SIMPLEX path = "numbers.png" img = cv2.imread(path, cv2.IMREAD_UNCHANGED) bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) bw = cv2.GaussianBlur(bw, (7, 7), 0) ret, thbw = cv2.threshold(bw, 200, 255, cv2.THRESH_BINARY_INV) thbw = cv2.erode(thbw, np.ones((1, 1), np.uint8), iterations=2) image, cntrs, hier = cv2.findContours(thbw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) rectangles = [] for c in cntrs: r = x, y, w, h = cv2.boundingRect(c)