'need help wrong text recognized during embossed ocr

I am to read the text on this embossed plate. I don't have much experience in this field this is what I am doing.

img5 = cv2.cvtColor(img5, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(img5,40,90)
kernel = np.ones((5,5), np.uint8)
#erosion = cv2.erode(canny, kernel, iterations=1)
dilate = cv2.dilate(canny, kernel, iterations=2)
erosion = cv2.erode(dilate, kernel, iterations=1)
#closing = cv2.morphologyEx(erosion, cv2.MORPH_CLOSE, kernel)
thresh = cv2.adaptiveThreshold(thresh,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY,11,5)
#text = pytesseract.image_to_string(thresh,config="--psm 8")
#print(text)
plt.figure(figsize = (6,20))
plt.imshow(thresh,cmap = plt.cm.gray)

and then I did this

cnts = cv2.findContours(erosion, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cents[1]
cnts  = sorted(cnts, key = lambda x: cv2.boundingRect(x)[0])
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    z =cv2.rectangle(img5, (x,y), (x+w,y+h), (0, 255,0),2)
#    ROI = img4[y:y+h, x:x+w]
text = pytesseract.image_to_string(z,config="--psm 8")
print(text)
#    print(cv2.contourArea(c))
plt.figure(figsize = (6,20))
plt.imshow(z,vmin = 0,vmax = 255,cmap = plt.cm.gray)

this is the original image and the text on it is PFXTWF00019



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source