in

how to extract the text from located bounding box & place the exact word at same position but on a white background using python & opencv


how to extract the text from located bounding box & place the exact word at same position but on a white background using python & opencv

import csv
import cv2

r1=[]
#import numpy as np

# read image
gt_got="E:codesmotion detectoropencv-grabcutimg_451.txt"
def read_coordinate(gt_got):
    #length1=0
    #i=0
    #l=[[]]
    with open(gt_got, "r") as csvfile:
        ofile = csv.reader(csvfile, delimiter=",")
        rows = (r for r in ofile if r)
        print("rows=",rows)
        #os.chdir(wht_dir)
        verts = [[int(i) for i in r] for r in rows]
        print("verts=",len(verts))
        length1=int(len(verts))
        for v in verts:
            l1=v
           # print ("l ",l1)
            
    return verts

img = cv2.imread('E:codesmotion detectoropencv-grabcutimg_451.jpg')#('E:myPrj20GUI_EASTEAST-argdemo_imagesframe0.jpg')
#img = cv2.imread('E:myPrj20GUI_EASTEAST-argdemo_imagesframe0.jpg')
# threshold on box outline color
#lowerBound = (80,120,100)
#upperBound = (160,200,180)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#lowerBound = (110,50,50)
#upperBound = (130,255,255)
#thresh = cv2.inRange(hsv, lowerBound, upperBound)
thresh = hsv[:, :, 1]

cv2.imshow("in range",thresh)
# apply morphology to ensure regions are filled and remove extraneous noise
#kernel = np.ones((3,3), np.uint8)
#thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

#cv2.imshow("noise removed",thresh)

ret, thresh = cv2.threshold(thresh, 0, 255, cv2.THRESH_OTSU)#+cv2.THRESH_BINARY)
cv2.imshow('otsu',thresh)


# get contours
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

rect1=read_coordinate(gt_got)
for r in rect1:
    
    r1=[r[0],r[1],r[2],r[3]]
    r1=list(map(int,r1)) #for converting r1 to list of int values
    print(r1)
# get bounding boxes
    i = 1
    for cntr in contours:
    # get bounding boxes
        if cv2.contourArea(cntr) >2:
            #print(r1)
           # print("inside contiur area")
            x,y,w,h = cv2.boundingRect(cntr)
            print("x=",x,"  y=", y, "r1=", r1[0], "r1=",r1)
            if (x >=r1[0] or y>=r1[1]):
                print("match")
                crop = img[y:y+h, x:x+w]
                cv2.imwrite("text_boxes_crop_{0}.png".format(i), crop)
                i = i + 1

# save threshold
cv2.imwrite("text_boxes_thresh.png",thresh)

# show thresh and result    
cv2.imshow("thresh", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

in this code .txt file have 4 coordinates of located text, from csv i am converting in to int, and trying to extract only those bouding box values whose value match with the coordinate present in the text. But i am unable to match all the 4 co-ordinates and extract only that portion of image.
please suggest modification



Source: https://stackoverflow.com/questions/70542728/how-to-extract-the-text-from-located-bounding-box-place-the-exact-word-at-same

Telegram Launched These New Cool Features for Users

Minimal code and simple experiments to play with Denoising Diffusion Probabilistic Models (DDPMs)