File size: 2,995 Bytes
003f582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import base64
from PIL import Image
import pytesseract
import google.generativeai as genai
import os
# Step 1: Configure the Gemini API
GEMINI_API_KEY = os.getenv('GEMINI')
genai.configure(api_key=GEMINI_API_KEY)

# Initialize the Gemini model
gemini_model = genai.GenerativeModel('gemini-1.5-flash')

# Step 2: Function to convert the image to base64
def convert_image_to_base64(file_path):
    try:
        with open(file_path, "rb") as image_file:
            encoded_string = base64.b64encode(image_file.read()).decode()
        return encoded_string
    except FileNotFoundError:
        print(f"File {file_path} not found. Please check the path and try again.")
        return None

# Step 3: Function to extract text from the image using OCR (pytesseract)
def extract_text_from_image(file_path):
    try:
        image = Image.open(file_path)
        extracted_text = pytesseract.image_to_string(image)
        print("\nExtracted Text from the Image:")
        print(extracted_text)
        return extracted_text
    except Exception as e:
        print(f"Error processing the image file: {e}")
        return None

# Step 4: Function to analyze the health report using the Gemini model
def analyze_health_report(encoded_image, extracted_text):
    prompt = f"""
    Analyze the following health report image and provide:
    1. Key findings in the report.
    2. Explanation of findings in report.
    3. Recommendations for lifestyle changes or immediate actions.
    4. Suggested doctor specialty to consult.

    Extracted Text from the Report:
    {extracted_text}
    """

    try:
        response = gemini_model.generate_content(prompt)

        # Display the AI analysis result
        if response:
            response_text = response.text.strip()
            print("\n### AI Analysis and Recommendations ###")
            print(response_text)
        else:
            print("No response received from the AI model.")
    except Exception as e:
        print(f"Error while analyzing the report: {e}")

# Main Execution Flow
def main():
    import argparse

    # Argument parser for input file
    parser = argparse.ArgumentParser(description="Health Report Analyzer")
    parser.add_argument("image_file", type=str, help="Path to the health/laboratory report image")
    args = parser.parse_args()

    # Convert the image to base64
    encoded_image = convert_image_to_base64(args.image_file)
    if not encoded_image:
        return

    # Extract text from the image using OCR
    extracted_text = extract_text_from_image(args.image_file)
    if not extracted_text:
        return

    # Analyze the health report
    analyze_health_report(encoded_image, extracted_text)

if __name__ == "__main__":
    # Ensure pytesseract is available
    try:
        pytesseract.get_tesseract_version()
    except EnvironmentError:
        print("pytesseract is not installed or configured. Install it using `sudo apt install tesseract-ocr` or equivalent.")
        exit(1)

    main()