File size: 2,756 Bytes
ba29f94
fe6e7b2
 
ba29f94
 
fe6e7b2
ba29f94
 
fe6e7b2
 
 
 
ba29f94
 
fe6e7b2
 
 
 
ba29f94
fe6e7b2
 
ba29f94
fe6e7b2
 
 
 
 
 
 
 
 
 
ba29f94
fe6e7b2
 
 
 
 
 
 
 
 
 
 
 
 
 
ba29f94
fe6e7b2
 
 
 
ba29f94
fe6e7b2
 
ba29f94
fe6e7b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import easyocr
import requests
from transformers import AutoModelForTokenClassification, AutoTokenizer
from transformers import pipeline

# Initialize EasyOCR for text extraction from medical report image
reader = easyocr.Reader(['en'])

# Function to extract text from image
def extract_text_from_image(image_path):
    result = reader.readtext(image_path)
    text = ' '.join([item[1] for item in result])
    return text

# Load Med7 model for medical NER (Named Entity Recognition)
model_name = "jeff1evesque/med7"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForTokenClassification.from_pretrained(model_name)

# Initialize the NER pipeline with Med7 model
nlp = pipeline("ner", model=model, tokenizer=tokenizer)

# Function to extract medical entities (test results) from text
def extract_medical_values(text):
    entities = nlp(text)
    medical_data = {}
    
    for entity in entities:
        if entity['entity_group'] == 'LAB_RESULT':  # Change based on Med7's NER labels
            medical_data[entity['word']] = entity['score']
    
    return medical_data

# Function to check the values with LabTestAPI or similar API
def validate_medical_value(test_name, test_value):
    # Example of API call (replace with actual API)
    api_url = "https://api.labtestapi.com/get_reference_range"
    params = {
        "test_name": test_name,
        "value": test_value
    }
    response = requests.get(api_url, params=params)
    if response.status_code == 200:
        result = response.json()
        return result['normal_range']
    else:
        return None

# Function to analyze medical report
def analyze_report(image_path):
    # Step 1: Extract text from the medical report image
    text = extract_text_from_image(image_path)
    
    # Step 2: Extract medical values using Med7
    medical_values = extract_medical_values(text)
    
    analysis_results = []
    
    for test_name, test_value in medical_values.items():
        # Step 3: Validate test value against normal range
        normal_range = validate_medical_value(test_name, test_value)
        if normal_range:
            if test_value < normal_range[0]:
                analysis_results.append(f"{test_name} is low. Consider consulting a doctor.")
            elif test_value > normal_range[1]:
                analysis_results.append(f"{test_name} is high. Consult a doctor.")
            else:
                analysis_results.append(f"{test_name} is within the normal range.")
        else:
            analysis_results.append(f"Could not validate {test_name}.")
    
    return analysis_results

# Example Usage
image_path = "path_to_medical_report_image.png"
analysis = analyze_report(image_path)
for result in analysis:
    print(result)