Ask Your Question
1

extract text from Driving Licence

asked 2020-10-08 00:53:43 -0600

updated 2020-10-08 07:17:33 -0600

supra56 gravatar image

Hi team, I could not able to remove image background(watermark) from Driving Licence.For this reason unable to extract text from image.Could anyone please help me on this.

C:\fakepath\Driving Licence_1.PNG

Below is the sample image and code:

from imutils.object_detection import non_max_suppression
import numpy as np
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'\Tesseract-OCR\tesseract.exe'
import argparse
import cv2


def decode_predictions(scores, geometry):

    (numRows, numCols) = scores.shape[2:4]
    rects = []
    confidences = []

    for y in range(0, numRows):

        scoresData = scores[0, 0, y]
        xData0 = geometry[0, 0, y]
        xData1 = geometry[0, 1, y]
        xData2 = geometry[0, 2, y]
        xData3 = geometry[0, 3, y]
        anglesData = geometry[0, 4, y]

        for x in range(0, numCols):

            if scoresData[x] < args['min_confidence']:
                continue

            (offsetX, offsetY) = (x * 4.0, y * 4.0)

            angle = anglesData[x]
            cos = np.cos(angle)
            sin = np.sin(angle)

            h = xData0[x] + xData2[x]
            w = xData1[x] + xData3[x]

            endX = int(offsetX + cos * xData1[x] + sin * xData2[x])
            endY = int(offsetY - sin * xData1[x] + cos * xData2[x])
            startX = int(endX - w)
            startY = int(endY - h)

            rects.append((startX, startY, endX, endY))
            confidences.append(scoresData[x])

    return (rects, confidences)


ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', type=str, help='path to input image')
ap.add_argument('-east', '--east', type=str,
                help='path to input EAST text detector')
ap.add_argument('-c', '--min-confidence', type=float, default=0.5,
                help='minimum probability required to inspect a region')
ap.add_argument('-w', '--width', type=int, default=320,
                help='nearest multiple of 32 for resized width')
ap.add_argument('-e', '--height', type=int, default=320,
                help='nearest multiple of 32 for resized height')
ap.add_argument('-p', '--padding', type=float, default=0.0,
                help='amount of padding to add to each border of ROI')
args = vars(ap.parse_args())

image = cv2.imread(args['image'])
orig = image.copy()
(origH, origW) = image.shape[:2]

(newW, newH) = (args['width'], args['height'])
rW = origW / float(newW)
rH = origH / float(newH)

image = cv2.resize(image, (newW, newH))
(H, W) = image.shape[:2]

layerNames = ['feature_fusion/Conv_7/Sigmoid', 'feature_fusion/concat_3'
              ]

print '[INFO] loading EAST text detector...'
net = cv2.dnn.readNet(args['east'])

blob = cv2.dnn.blobFromImage(
    image,
    1.0,
    (W, H),
    (400, 180, 20),
    swapRB=True,
    crop=False,
    )
net.setInput(blob)
(scores, geometry) = net.forward(layerNames)

(rects, confidences) = decode_predictions(scores, geometry)
boxes = non_max_suppression(np.array(rects), probs=confidences)

results = []

for (startX, startY, endX, endY) in boxes:

    startX = int(startX * rW)
    startY = int(startY * rH)
    endX = int(endX * rW)
    endY = int(endY * rH)

    dX = int((endX - startX) * args['padding'])
    dY = int((endY - startY) * args['padding'])

    startX = max(0, startX - dX)
    startY = max(0, startY - dY)
    endX = min(origW, endX + dX * 2)
    endY = min(origH, endY + dY * 2)

    roi = orig[startY:endY, startX:endX]
    crop_image = orig[startY:endY, startX:endX]

    config = '-l eng --oem 1 --psm 7'
    text = pytesseract.image_to_string(roi, config=config)

    results.append(((startX, startY, endX, endY), text))

results = sorted(results, key=lambda r: r[0][1])

for ((startX, startY, endX, endY), text) in results:

    print 'OCR TEXT'
    print '========'
    print '{}\n'.format(text)

    text = ''.join([(c if ord ...
(more)
edit retag flag offensive close merge delete

Comments

can you explain, why you think, your code would extract text, and how so ?

berak gravatar imageberak ( 2020-10-08 01:37:04 -0600 )edit
2

@berak, updated code

nagarjuna.b@pennanttech.com gravatar image[email protected] ( 2020-10-08 01:48:39 -0600 )edit

Which watermark. The one with stamped on it?

supra56 gravatar imagesupra56 ( 2020-10-08 07:18:40 -0600 )edit

Btw, Which opencv version r u using?

supra56 gravatar imagesupra56 ( 2020-10-08 07:32:05 -0600 )edit
1

@supra56,Yes with stamped.I'm using opencv 4.3.0 version

nagarjuna.b@pennanttech.com gravatar image[email protected] ( 2020-10-09 03:49:55 -0600 )edit

Upgraded to 4.4.0 or 4.5.0. I can't used older version

supra56 gravatar imagesupra56 ( 2020-10-09 04:40:59 -0600 )edit
1

Upgraded to 4.4.0.42.Still I could not able to read text with image having watermark(stamped).While reading the Licence ID was incorrect.Could you please help me on this.

nagarjuna.b@pennanttech.com gravatar image[email protected] ( 2020-10-09 05:27:53 -0600 )edit

I noticed that u can't ur your code as above to remove background.

supra56 gravatar imagesupra56 ( 2020-10-09 07:16:27 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-10-13 00:45:33 -0600

May be choosing a good threshold value can solve your problem. Watermark is of lighter shade and it should get filtered out, just play around with some threshold values.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-10-08 00:53:43 -0600

Seen: 2,585 times

Last updated: Oct 08 '20