Beispiel #1
0
        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)
    if y < 0:
        y = 0
    elif y > img_h:
        y = img_h

    if x+w > img_w:
        w = img_w - x

    if y+h > img_h:
        h = img_h - y

    return x, y, w, h


ann, test_data = digits_ann.train(
    digits_ann.create_ann(60), 50000, 10)

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.
    contours, hier = cv2.findContours(thresh, cv2.RETR_TREE,
                                      cv2.CHAIN_APPROX_SIMPLE)
    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.
Beispiel #4
0
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.creat_ANN(56), 10000)
font = cv2.FONT_HERSHEY_SIMPLEX
path = 'C:/Users/18139/Desktop/my/test.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)
    a = cv2.contourArea(c)
    b = (img.shape[0] - 3) * (img.shape[1] - 3)
from digits_ann import create_ann, train, test

ann, test_data = train(create_ann())
test(ann, test_data)
Beispiel #6
0
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)
Beispiel #7
0
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)