Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,22 +10,44 @@ import time
|
|
| 10 |
# Initialize PaddleOCR once with updated parameters
|
| 11 |
ocr_model = PaddleOCR(use_textline_orientation=True, lang='en')
|
| 12 |
|
| 13 |
-
def analyze_uv_coverage(img, brightness_threshold=150):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 15 |
-
_, binary_mask = cv2.threshold(gray, brightness_threshold, 255, cv2.THRESH_BINARY)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
total_pixels = binary_mask.size
|
| 21 |
sterilized_pixels = cv2.countNonZero(binary_mask)
|
| 22 |
coverage_percent = (sterilized_pixels / total_pixels) * 100
|
| 23 |
|
|
|
|
| 24 |
overlay = img.copy()
|
| 25 |
-
overlay[binary_mask == 255] = [0, 255, 0] # Green
|
| 26 |
-
overlay[binary_mask == 0] = [0, 0, 255] # Red
|
| 27 |
|
| 28 |
annotated_img = cv2.addWeighted(img, 0.6, overlay, 0.4, 0)
|
|
|
|
| 29 |
return annotated_img, coverage_percent
|
| 30 |
|
| 31 |
def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, output_path):
|
|
@@ -44,7 +66,9 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
|
|
| 44 |
pdf.set_font("Arial", size=10)
|
| 45 |
if extracted_texts:
|
| 46 |
for text in extracted_texts:
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
else:
|
| 49 |
pdf.cell(0, 8, "No text detected.", ln=True)
|
| 50 |
|
|
@@ -57,7 +81,7 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
|
|
| 57 |
def process_image(input_img, brightness_threshold=150):
|
| 58 |
img = cv2.cvtColor(np.array(input_img), cv2.COLOR_RGB2BGR)
|
| 59 |
|
| 60 |
-
# Resize large images for faster processing
|
| 61 |
max_dim = 640
|
| 62 |
h, w = img.shape[:2]
|
| 63 |
if max(h, w) > max_dim:
|
|
@@ -72,24 +96,29 @@ def process_image(input_img, brightness_threshold=150):
|
|
| 72 |
for line in ocr_result:
|
| 73 |
if line:
|
| 74 |
for word_info in line:
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
annotated_img, coverage_percent = analyze_uv_coverage(img, brightness_threshold)
|
| 78 |
|
| 79 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img_file:
|
| 80 |
cv2.imwrite(temp_img_file.name, annotated_img)
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
| 87 |
|
| 88 |
report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%"
|
| 89 |
-
# Optionally include OCR time:
|
| 90 |
-
# report_text += f"\
|
| 91 |
|
| 92 |
-
|
|
|
|
| 93 |
|
| 94 |
return annotated_img_rgb, report_text, temp_pdf_file.name
|
| 95 |
|
|
|
|
| 10 |
# Initialize PaddleOCR once with updated parameters
|
| 11 |
ocr_model = PaddleOCR(use_textline_orientation=True, lang='en')
|
| 12 |
|
| 13 |
+
def analyze_uv_coverage(img, brightness_threshold=150, kernel_size=5, apply_blur=True, adaptive_thresh=False):
|
| 14 |
+
"""
|
| 15 |
+
Analyze UV sterilization coverage by thresholding the grayscale image.
|
| 16 |
+
Optional adaptive thresholding and Gaussian blur for noise reduction.
|
| 17 |
+
Morphological operations clean the mask for better accuracy.
|
| 18 |
+
"""
|
| 19 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 20 |
|
| 21 |
+
if apply_blur:
|
| 22 |
+
gray = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 23 |
+
|
| 24 |
+
if adaptive_thresh:
|
| 25 |
+
binary_mask = cv2.adaptiveThreshold(
|
| 26 |
+
gray, 255,
|
| 27 |
+
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 28 |
+
cv2.THRESH_BINARY,
|
| 29 |
+
11, 2)
|
| 30 |
+
else:
|
| 31 |
+
_, binary_mask = cv2.threshold(gray, brightness_threshold, 255, cv2.THRESH_BINARY)
|
| 32 |
+
|
| 33 |
+
# Morphological opening (erosion followed by dilation) to remove noise
|
| 34 |
+
kernel = np.ones((kernel_size, kernel_size), np.uint8)
|
| 35 |
+
binary_mask = cv2.morphologyEx(binary_mask, cv2.MORPH_OPEN, kernel, iterations=1)
|
| 36 |
+
|
| 37 |
+
# Morphological closing (dilation followed by erosion) to close small holes inside foreground
|
| 38 |
+
binary_mask = cv2.morphologyEx(binary_mask, cv2.MORPH_CLOSE, kernel, iterations=1)
|
| 39 |
|
| 40 |
total_pixels = binary_mask.size
|
| 41 |
sterilized_pixels = cv2.countNonZero(binary_mask)
|
| 42 |
coverage_percent = (sterilized_pixels / total_pixels) * 100
|
| 43 |
|
| 44 |
+
# Create overlay for visualization: Green = sterilized, Red = unsterilized
|
| 45 |
overlay = img.copy()
|
| 46 |
+
overlay[binary_mask == 255] = [0, 255, 0] # Green
|
| 47 |
+
overlay[binary_mask == 0] = [0, 0, 255] # Red
|
| 48 |
|
| 49 |
annotated_img = cv2.addWeighted(img, 0.6, overlay, 0.4, 0)
|
| 50 |
+
|
| 51 |
return annotated_img, coverage_percent
|
| 52 |
|
| 53 |
def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, output_path):
|
|
|
|
| 66 |
pdf.set_font("Arial", size=10)
|
| 67 |
if extracted_texts:
|
| 68 |
for text in extracted_texts:
|
| 69 |
+
# Filter out very short or empty OCR texts to improve clarity
|
| 70 |
+
if len(text.strip()) > 1:
|
| 71 |
+
pdf.multi_cell(0, 8, f"- {text}")
|
| 72 |
else:
|
| 73 |
pdf.cell(0, 8, "No text detected.", ln=True)
|
| 74 |
|
|
|
|
| 81 |
def process_image(input_img, brightness_threshold=150):
|
| 82 |
img = cv2.cvtColor(np.array(input_img), cv2.COLOR_RGB2BGR)
|
| 83 |
|
| 84 |
+
# Resize large images for faster processing, preserving aspect ratio
|
| 85 |
max_dim = 640
|
| 86 |
h, w = img.shape[:2]
|
| 87 |
if max(h, w) > max_dim:
|
|
|
|
| 96 |
for line in ocr_result:
|
| 97 |
if line:
|
| 98 |
for word_info in line:
|
| 99 |
+
# Filter short strings and whitespace only
|
| 100 |
+
text = word_info[1][0].strip()
|
| 101 |
+
if len(text) > 1:
|
| 102 |
+
extracted_texts.append(text)
|
| 103 |
|
| 104 |
annotated_img, coverage_percent = analyze_uv_coverage(img, brightness_threshold)
|
| 105 |
|
| 106 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img_file:
|
| 107 |
cv2.imwrite(temp_img_file.name, annotated_img)
|
| 108 |
+
annotated_img_path = temp_img_file.name
|
| 109 |
|
| 110 |
+
temp_pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
| 111 |
+
temp_pdf_file.close()
|
| 112 |
+
create_pdf_report(coverage_percent, extracted_texts, annotated_img_path, temp_pdf_file.name)
|
| 113 |
|
| 114 |
annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
| 115 |
|
| 116 |
report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%"
|
| 117 |
+
# Optionally include OCR time for debugging:
|
| 118 |
+
# report_text += f"\nOCR Processing Time: {ocr_time:.2f} seconds"
|
| 119 |
|
| 120 |
+
# Clean up temp image file after PDF generation
|
| 121 |
+
os.unlink(annotated_img_path)
|
| 122 |
|
| 123 |
return annotated_img_rgb, report_text, temp_pdf_file.name
|
| 124 |
|