import io, os
from google.cloud import vision
from PIL import Image, ImageEnhance

def detect_text(image_file, credentials_file, text_area):
    """Detects text in an image."""
        
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_file                 # Authenticates the client
    
    try:        
        with Image.open(image_file, 'r') as img:        
            # Crop the image    
            img_cropped = img.crop(text_area)
            
            img_rgb = img_cropped.convert('RGB')

            # Convert the image to grayscale mode
            img_gray = img_rgb.convert("L")

            # Apply thresholding to convert gray pixels to white
            img_bw = img_gray.convert('L')

            # Reduce the brightness of the image
            enhancer = ImageEnhance.Brightness(img_bw)
            img_low_brightness = enhancer.enhance(1.5)

            # Enhance the contrast of the image
            enhancer = ImageEnhance.Contrast(img_low_brightness)
            img_contrast = enhancer.enhance(3)
            
            # Rotate the image
            img_rotated = img_contrast.rotate(0, expand=True)

            # Save the modified image
            img_rotated.save("contrast_image.png")
        
    except FileNotFoundError:
            print(f"Error: File not found: {image_file}")
            return None
   
    # Send the cropped image to the Google Cloud Vision API for text detection
    client = vision.ImageAnnotatorClient()  
    content = io.BytesIO()
    img_contrast.save(content, format='PNG')
                                            
    image = vision.Image(content=content.getvalue())
    response = client.text_detection(image=image)
    texts = response.text_annotations

    # Handle errors
    if response.error.message:
        raise Exception(response.error.message)

    # Return the first detected text as a float
    try:
        print(float(texts[0].description))                                          # Prints the detected text (optional)                                          
        return float(texts[0].description)
    except IndexError:
        print("No text detected in the image.")
        return None

detect_text(image_file='meter.jpg', credentials_file='GoogleVisionKeys.json', text_area=(340, 135, 580, 200))      # text_area = (left, top, right, bottom)