Detect text that overlaps with lines on a drawing using Computer Vision

38 Views Asked by At

I aim to detect text overlapped with lines on architectural drawings. My ideal is to detect all lines and record them. Next, I will attempt to detect text using OCR and record the bounding boxes of the text. Finally, I will identify boxes that intersect with the lines I previously drew

Source Image: enter image description here


  1. Initially, I'll utilize cv2.Canny() to detect all lines of drawning. ***
gray = cv2.imread('data/pre1.jpg')
ret,gray = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)

edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
minLineLength=0
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=10)

a,b,c = lines.shape
for i in range(a):
    cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (255,0 ,0 ), 1, cv2.LINE_AA)
    plt.imshow(gray)

The result is: enter image description here


2.Subsequently, I'll attempt text detection using OCR, although the result is very bad. ***

img = cv2.imread("data/pre1.jpg")
height,width,_ = img.shape
# lst_prohibit = ["~","-","_",".","—",",","|","=","'",'"',"\\"]
lowercase_alphabet = [chr(i) for i in range(97, 123)]
uppercase_alphabet = [chr(i) for i in range(65, 91)]
number = []
for i in range(0,10):
    number.append(str(i))
combined_alphabet = lowercase_alphabet + uppercase_alphabet  + number
combined_alphabet

boxes = pytesseract.image_to_boxes(img)
for box in boxes.splitlines():
    box = box.split(" ")
    if box[0]  in combined_alphabet:
        img = cv2.rectangle(img, (int(box[1]), height - int(box[2])), (int(box[3]), height - int(box[4])), (255, 0, 0), 1)
        plt.imshow(img)

The result is: enter image description here

Therefore, I cannot continue my plan

I will really appreciate if you can help detect text or give me other solutions to detect text overlapped with lines on architectural drawings.

Thank all!!!

0

There are 0 best solutions below