Upload 2 files
Browse files- Dockerfile +2 -5
- ocr_utils.py +11 -39
Dockerfile
CHANGED
|
@@ -20,11 +20,8 @@ COPY requirements.txt .
|
|
| 20 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 21 |
|
| 22 |
# 🛑 CRITICAL FIX FOR EASYOCR PERMISSION: 🛑
|
| 23 |
-
# Create the necessary writable directories
|
| 24 |
-
|
| 25 |
-
RUN mkdir -p /app/easyocr_models/user_network
|
| 26 |
-
RUN chmod -R 777 /app/easyocr_models
|
| 27 |
-
# NOTE: This ensures both model storage and user network directories are writable.
|
| 28 |
|
| 29 |
# 3b. Copy the rest of your application code and models
|
| 30 |
COPY . .
|
|
|
|
| 20 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 21 |
|
| 22 |
# 🛑 CRITICAL FIX FOR EASYOCR PERMISSION: 🛑
|
| 23 |
+
# Create the necessary writable directories including user_network
|
| 24 |
+
RUN mkdir -p /app/easyocr_models/user_network && chmod -R 777 /app/easyocr_models
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# 3b. Copy the rest of your application code and models
|
| 27 |
COPY . .
|
ocr_utils.py
CHANGED
|
@@ -3,18 +3,20 @@ import os
|
|
| 3 |
|
| 4 |
# Initialize the OCR reader once to save time
|
| 5 |
# 'en' is for English language
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
os.makedirs(
|
|
|
|
| 13 |
|
| 14 |
# DEBUG: Verify the directories
|
| 15 |
print(f"[DEBUG] EasyOCR models directory: {MODELS_DIR}")
|
| 16 |
print(f"[DEBUG] EasyOCR user network directory: {USER_NETWORK_DIR}")
|
| 17 |
|
|
|
|
| 18 |
reader = easyocr.Reader(
|
| 19 |
['en'],
|
| 20 |
model_storage_directory=MODELS_DIR,
|
|
@@ -24,26 +26,11 @@ reader = easyocr.Reader(
|
|
| 24 |
def extract_keywords_from_report(file_path):
|
| 25 |
"""
|
| 26 |
Performs OCR on the uploaded file and extracts relevant text.
|
| 27 |
-
|
| 28 |
-
Args:
|
| 29 |
-
file_path (str): The path to the uploaded image or PDF.
|
| 30 |
-
|
| 31 |
-
Returns:
|
| 32 |
-
str: A single string containing all recognized text.
|
| 33 |
"""
|
| 34 |
try:
|
| 35 |
-
# EasyOCR works well with image files (JPG, PNG)
|
| 36 |
-
# For PDFs, you'll need an external tool to convert PDF to image pages first,
|
| 37 |
-
# but for simplicity, we'll focus on image inputs for now.
|
| 38 |
-
|
| 39 |
-
# Read the text from the image
|
| 40 |
results = reader.readtext(file_path, detail=0)
|
| 41 |
-
|
| 42 |
-
# Join all lines into a single string for keyword searching
|
| 43 |
full_text = " ".join(results).lower()
|
| 44 |
-
|
| 45 |
return full_text
|
| 46 |
-
|
| 47 |
except Exception as e:
|
| 48 |
print(f"OCR Error: {e}")
|
| 49 |
return ""
|
|
@@ -51,12 +38,6 @@ def extract_keywords_from_report(file_path):
|
|
| 51 |
def score_text_for_risk(text):
|
| 52 |
"""
|
| 53 |
Scores the extracted text and lists the keywords found.
|
| 54 |
-
|
| 55 |
-
Args:
|
| 56 |
-
text (str): The full text extracted via OCR.
|
| 57 |
-
|
| 58 |
-
Returns:
|
| 59 |
-
tuple: (float risk_score, list keywords_found)
|
| 60 |
"""
|
| 61 |
high_risk_keywords = [
|
| 62 |
"nodule", "abnormal cell", "squamous", "carcinoma", "malignant",
|
|
@@ -69,25 +50,16 @@ def score_text_for_risk(text):
|
|
| 69 |
for keyword in high_risk_keywords:
|
| 70 |
if keyword in text:
|
| 71 |
score += 0.1
|
| 72 |
-
keywords_found.append(keyword.title())
|
| 73 |
|
| 74 |
-
# Cap the score at 1.0
|
| 75 |
return min(score, 1.0), keywords_found
|
| 76 |
|
| 77 |
-
# Example
|
| 78 |
if __name__ == '__main__':
|
| 79 |
-
# You would test this with a sample image report
|
| 80 |
-
# print(extract_keywords_from_report('path/to/sample_report.jpg'))
|
| 81 |
-
pass
|
| 82 |
-
|
| 83 |
-
if __name__ == '__main__':
|
| 84 |
-
# ⚠️ Replace 'test_report.png' with the actual file name
|
| 85 |
test_file_path = 'test_report.png'
|
| 86 |
-
|
| 87 |
if os.path.exists(test_file_path):
|
| 88 |
extracted_text = extract_keywords_from_report(test_file_path)
|
| 89 |
risk_score = score_text_for_risk(extracted_text)
|
| 90 |
-
|
| 91 |
print("--- OCR Test Results ---")
|
| 92 |
print(f"Extracted Text: {extracted_text}")
|
| 93 |
print(f"Calculated Risk Score: {risk_score}")
|
|
|
|
| 3 |
|
| 4 |
# Initialize the OCR reader once to save time
|
| 5 |
# 'en' is for English language
|
| 6 |
+
# Set both model storage and user network directories inside /app (writable)
|
| 7 |
+
|
| 8 |
+
MODELS_DIR = "/app/easyocr_models"
|
| 9 |
+
USER_NETWORK_DIR = "/app/easyocr_models/user_network"
|
| 10 |
+
|
| 11 |
+
# Ensure directories exist
|
| 12 |
+
os.makedirs(MODELS_DIR, exist_ok=True)
|
| 13 |
+
os.makedirs(USER_NETWORK_DIR, exist_ok=True)
|
| 14 |
|
| 15 |
# DEBUG: Verify the directories
|
| 16 |
print(f"[DEBUG] EasyOCR models directory: {MODELS_DIR}")
|
| 17 |
print(f"[DEBUG] EasyOCR user network directory: {USER_NETWORK_DIR}")
|
| 18 |
|
| 19 |
+
# Initialize EasyOCR reader with both directories specified
|
| 20 |
reader = easyocr.Reader(
|
| 21 |
['en'],
|
| 22 |
model_storage_directory=MODELS_DIR,
|
|
|
|
| 26 |
def extract_keywords_from_report(file_path):
|
| 27 |
"""
|
| 28 |
Performs OCR on the uploaded file and extracts relevant text.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
"""
|
| 30 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
results = reader.readtext(file_path, detail=0)
|
|
|
|
|
|
|
| 32 |
full_text = " ".join(results).lower()
|
|
|
|
| 33 |
return full_text
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
print(f"OCR Error: {e}")
|
| 36 |
return ""
|
|
|
|
| 38 |
def score_text_for_risk(text):
|
| 39 |
"""
|
| 40 |
Scores the extracted text and lists the keywords found.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
"""
|
| 42 |
high_risk_keywords = [
|
| 43 |
"nodule", "abnormal cell", "squamous", "carcinoma", "malignant",
|
|
|
|
| 50 |
for keyword in high_risk_keywords:
|
| 51 |
if keyword in text:
|
| 52 |
score += 0.1
|
| 53 |
+
keywords_found.append(keyword.title())
|
| 54 |
|
|
|
|
| 55 |
return min(score, 1.0), keywords_found
|
| 56 |
|
| 57 |
+
# Example test
|
| 58 |
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
test_file_path = 'test_report.png'
|
|
|
|
| 60 |
if os.path.exists(test_file_path):
|
| 61 |
extracted_text = extract_keywords_from_report(test_file_path)
|
| 62 |
risk_score = score_text_for_risk(extracted_text)
|
|
|
|
| 63 |
print("--- OCR Test Results ---")
|
| 64 |
print(f"Extracted Text: {extracted_text}")
|
| 65 |
print(f"Calculated Risk Score: {risk_score}")
|