Advertisement
jarekmor

VisionAI_local_tesseract

Mar 9th, 2023 (edited)
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. import cv2
  2. import pytesseract
  3. from matplotlib import pyplot as plt
  4.  
  5. # show image function
  6. def show_img(img):
  7.   fig = plt.gcf()
  8.   fig.set_size_inches(16, 8)
  9.   plt.axis("off")
  10.   plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
  11.   plt.show()
  12.  
  13. def detect_text(image_file, text_area):
  14.     """Detects text in an image and returns it as a float."""
  15.  
  16.     try:
  17.         # Read the image
  18.         img = cv2.imread(image_file)
  19.        
  20.         # Rotate the image if needed
  21.         height , width = img.shape[: 2]
  22.         rotation_matrix=cv2.getRotationMatrix2D((width / 2 , height / 2), 0, 1)
  23.         img_rotated=cv2.warpAffine(img , rotation_matrix , (width , height))
  24.         # show_img(img_rotated)
  25.        
  26.         # Crop the image with given text_area=(left, top, width, height)
  27.         (x, y, w, h) = text_area
  28.         img_crop=img_rotated[y:y+h,x:x+w]
  29.         # show_img(img_crop)
  30.        
  31.         # Convert the image to grayscale mode and apply thresholding
  32.         img_gray = cv2.cvtColor(img_crop, cv2.COLOR_BGR2GRAY)
  33.         _, img_bw = cv2.threshold(img_gray, 128, 255, cv2.THRESH_BINARY)
  34.         # show_img(img_bw)
  35.        
  36.         # Reduce the brightness and enhance the contrast of the image if needed
  37.         img_low_brigh = cv2.addWeighted(img_bw, 1.5, img_bw, 0.0 , -50)
  38.         alpha = 3.0                                                                         # Contrast control (1.0-3.0)
  39.         beta = 0                                                                            # Brightness control (0-100)
  40.         img_contrast = cv2.convertScaleAbs(img_low_brigh , alpha=alpha , beta=beta )
  41.         # show_img(img_contrast)
  42.        
  43.         # Reverse the colors by subtracting the grayscale image from 255 if original digits are in white color
  44.         img_reversed = 255 - img_contrast
  45.         # show_img(img_reversed)
  46.        
  47.        # Save the modified image for debugging purposes
  48.         cv2.imwrite("reversed_image.png",img_reversed)
  49.  
  50.     except FileNotFoundError:
  51.         print(f"Error: File not found: {image_file}")
  52.         return None
  53.  
  54.     # Use pytesseract to perform optical character recognition on the image
  55.     custom_config = r'--oem 3 --psm 7 outbase digits'
  56.  
  57.     text=pytesseract.image_to_string(img_reversed, config=custom_config)   #use this line if original digits are white
  58.     #text=pytesseract.image_to_string(img_contrast, config=custom_config)   #use this line if original digits are not white
  59.  
  60.     # Return the detected text as a float or None if no text is found
  61.     try:
  62.         return float(text)
  63.  
  64.     except ValueError:
  65.         return None
  66.  
  67. detect_text("test.png", text_area=(200, 190, 410, 65)) # text_area=(left, top, width, height)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement