title: OCR: check if letter is in (string) of image (Opencv, Python, Tesseract)
body: <p>This is a really tricky question.</p>
  
  <p>I am using the following code to detect text in hand-written images. I don't want it to recognize the char, in this case it only create a bounding box around each character/word he find.</p>
  
  <p><a href="https://i.stack.imgur.com/WSOFI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WSOFI.png" alt="o1"></a></p>
  
  <p>This is the code:</p>
  
  <pre><code>import cv2
  import tesserocr as tr
  from PIL import Image
  import numpy as np
  
  img = cv2.imread('1.png')
  
  idx = 0
  
  # since tesserocr accepts PIL images, converting opencv image to pil
  pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
  
  # initialize api
  api = tr.PyTessBaseAPI()
  
  alphabet_min = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  
  alphabet_max = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
                  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  
  try:
      api.SetImage(pil_img)
      boxes = api.GetComponentImages(tr.RIL.SYMBOL, True)
      text = api.GetUTF8Text()
      print(text)
  
      for (im, box, _, _) in boxes:
          x, y, w, h = box['x'], box['y'], box['w'], box['h']
          #print(box)
  
          #if w &lt; 200:
              #cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=1)
  
      for letter in text:
          if letter in alphabet_min:
              cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=-1)
  
      idx += 1
  
  finally:
      api.End()
  
  cv2.imshow('2', img)
  cv2.waitKey(0)
  </code></pre>
  
  <p>If you take a closer look, you can see a <code>print(text)</code>. This one print the text he find in the image. But, being this a hand-made text, it recovers near nothing:</p>
  
  <pre><code>Ca) a1 1. s 5305 Fm“. 4 54 0235 166 ﬁrm 4 §24630455
  </code></pre>
  
  <p>But even this output can help me in some way.</p>
  
  <p>A little above in the code, I made a function: </p>
  
  <pre><code>for letter in text:
          if letter in alphabet_min:
              cv2.rectangle(img, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=-1) 
  </code></pre>
  
  <p>This <em>should</em> take every char in the <code>print(text)</code> output and compare it to the <code>alphabet_min</code> list in the code.
  But it don't wanna work. I don't know why ?</p>
  
  <p>The purpose of this is: if you find a letter in <code>print(text)</code> and this is equal to one of <code>alphabet_min</code> list, then cover it in the image (using <code>cv2.rectangle</code>) using its correspondent in the image.</p>
  
  <p>Any suggestion ?</p>
  
  <p>Source image is this:</p>
  
  <p><a href="https://i.stack.imgur.com/yJmAJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yJmAJ.png" alt="src"></a></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Doing a print(True) under the condition, it show <code>6 True</code>. This means it finds the letter. The only problem is it don't create the bounding box for them.. </p>