title: Using YOLO or other image recognition techniques to identify all alphanumeric text present in images
body: <p>I have multiple images diagram, all of which contains labels as alphanumeric characters instead of just the text label itself. I want my YOLO model to identify all the numbers &amp; alphanumeric characters present in it.</p>
  
  <p>How can I train my YOLO model to do the same. The dataset can be found here. <a href="https://drive.google.com/open?id=1iEkGcreFaBIJqUdAADDXJbUrSj99bvoi" rel="noreferrer">https://drive.google.com/open?id=1iEkGcreFaBIJqUdAADDXJbUrSj99bvoi</a></p>
  
  <p>For example : see the bounding boxes. I want YOLO to detect wherever the text are present. However currently its not necessary to identify the text inside it.</p>
  
  <p><a href="https://i.stack.imgur.com/kckxb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/kckxb.png" alt="enter image description here"></a></p>
  
  <p>Also the same needs to be done for these type of images
  <a href="https://i.stack.imgur.com/KKsO2.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KKsO2.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/8Q2TO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/8Q2TO.png" alt="enter image description here"></a></p>
  
  <p>The images can be downloaded <a href="https://drive.google.com/open?id=1iEkGcreFaBIJqUdAADDXJbUrSj99bvoi" rel="noreferrer">here</a></p>
  
  <p>This is what I have tried using opencv but it does not work for all the images in the dataset.</p>
  
  <pre><code>import cv2
  import numpy as np
  import pytesseract
  
  pytesseract.pytesseract.tesseract_cmd = r"C:\Users\HPO2KOR\AppData\Local\Tesseract-OCR\tesseract.exe"
  
  image = cv2.imread(r'C:\Users\HPO2KOR\Desktop\Work\venv\Patent\PARTICULATE DETECTOR\PD4.png')
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
  clean = thresh.copy()
  
  horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,1))
  detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
  cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = cnts[0] if len(cnts) == 2 else cnts[1]
  for c in cnts:
      cv2.drawContours(clean, [c], -1, 0, 3)
  
  vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,30))
  detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
  cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = cnts[0] if len(cnts) == 2 else cnts[1]
  for c in cnts:
      cv2.drawContours(clean, [c], -1, 0, 3)
  
  cnts = cv2.findContours(clean, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = cnts[0] if len(cnts) == 2 else cnts[1]
  for c in cnts:
      area = cv2.contourArea(c)
      if area &lt; 100:
          cv2.drawContours(clean, [c], -1, 0, 3)
      elif area &gt; 1000:
          cv2.drawContours(clean, [c], -1, 0, -1)
      peri = cv2.arcLength(c, True)
      approx = cv2.approxPolyDP(c, 0.02 * peri, True)
      x,y,w,h = cv2.boundingRect(c)
      if len(approx) == 4:
          cv2.rectangle(clean, (x, y), (x + w, y + h), 0, -1)
  
  open_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
  opening = cv2.morphologyEx(clean, cv2.MORPH_OPEN, open_kernel, iterations=2)
  close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,2))
  close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, close_kernel, iterations=4)
  cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  cnts = cnts[0] if len(cnts) == 2 else cnts[1]
  for c in cnts:
      x,y,w,h = cv2.boundingRect(c)
      area = cv2.contourArea(c)
      if area &gt; 500:
          ROI = image[y:y+h, x:x+w]
          ROI = cv2.GaussianBlur(ROI, (3,3), 0)
          data = pytesseract.image_to_string(ROI, lang='eng',config='--psm 6')
          if data.isalnum():
              cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
              print(data)
  
  cv2.imwrite('image.png', image)
  cv2.imwrite('clean.png', clean)
  cv2.imwrite('close.png', close)
  cv2.imwrite('opening.png', opening)
  cv2.waitKey()
  </code></pre>
  
  <p>Is there any model or any opencv technique or some pre trained model that can do the same for me?
  I just need the bounding boxes around all the alphanumeric characters present in the images. After that I need to identify whats present in it. However the second part is not important currently.</p>