Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,103 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
temp_file_path = f.name
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from paddleocr import PaddleOCR
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from fpdf import FPDF
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Initialize PaddleOCR
|
| 9 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 10 |
|
| 11 |
+
def run_ocr(image_path):
|
| 12 |
+
result = ocr.ocr(image_path, cls=True)
|
| 13 |
+
texts = []
|
| 14 |
+
for line in result:
|
| 15 |
+
for word_info in line:
|
| 16 |
+
texts.append(word_info[1][0])
|
| 17 |
+
return texts
|
| 18 |
|
| 19 |
+
def analyze_uv_coverage(image_path, brightness_threshold=150):
|
| 20 |
+
# Load image
|
| 21 |
+
img = cv2.imread(image_path)
|
| 22 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 23 |
|
| 24 |
+
# Threshold image to segment sterilized zones
|
| 25 |
+
_, binary_mask = cv2.threshold(gray, brightness_threshold, 255, cv2.THRESH_BINARY)
|
| 26 |
|
| 27 |
+
# Calculate coverage
|
| 28 |
+
total_pixels = binary_mask.size
|
| 29 |
+
sterilized_pixels = cv2.countNonZero(binary_mask)
|
| 30 |
+
coverage_percent = (sterilized_pixels / total_pixels) * 100
|
| 31 |
+
|
| 32 |
+
# Create color overlay: sterilized in green, unsterilized in red
|
| 33 |
+
overlay = img.copy()
|
| 34 |
+
overlay[binary_mask == 255] = [0, 255, 0] # Green zones: sterilized
|
| 35 |
+
overlay[binary_mask == 0] = [0, 0, 255] # Red zones: unsterilized
|
| 36 |
+
|
| 37 |
+
# Blend overlay with original image
|
| 38 |
+
annotated_img = cv2.addWeighted(img, 0.6, overlay, 0.4, 0)
|
| 39 |
+
|
| 40 |
+
return annotated_img, coverage_percent
|
| 41 |
+
|
| 42 |
+
def save_report(image_path, coverage_percent, extracted_texts, output_folder='output'):
|
| 43 |
+
if not os.path.exists(output_folder):
|
| 44 |
+
os.makedirs(output_folder)
|
| 45 |
+
|
| 46 |
+
report_file = os.path.join(output_folder, 'UV_Sterilization_Report.pdf')
|
| 47 |
+
annotated_image_file = os.path.join(output_folder, 'annotated_image.jpg')
|
| 48 |
+
|
| 49 |
+
# Generate annotated image and save
|
| 50 |
+
annotated_img, coverage = analyze_uv_coverage(image_path)
|
| 51 |
+
cv2.imwrite(annotated_image_file, annotated_img)
|
| 52 |
+
|
| 53 |
+
# Create PDF report
|
| 54 |
+
pdf = FPDF()
|
| 55 |
+
pdf.add_page()
|
| 56 |
+
pdf.set_font("Arial", size=14)
|
| 57 |
+
pdf.cell(200, 10, txt="UV Sterilization Report", ln=True, align='C')
|
| 58 |
+
|
| 59 |
+
pdf.ln(10)
|
| 60 |
+
pdf.set_font("Arial", size=12)
|
| 61 |
+
pdf.cell(0, 10, f"Sterilization Coverage: {coverage_percent:.2f}%", ln=True)
|
| 62 |
+
pdf.ln(5)
|
| 63 |
+
|
| 64 |
+
pdf.cell(0, 10, "Extracted Texts from Image (OCR):", ln=True)
|
| 65 |
+
pdf.set_font("Arial", size=10)
|
| 66 |
+
for text in extracted_texts:
|
| 67 |
+
pdf.multi_cell(0, 8, f"- {text}")
|
| 68 |
+
|
| 69 |
+
pdf.ln(10)
|
| 70 |
+
pdf.cell(0, 10, "Annotated Image is saved alongside this report.", ln=True)
|
| 71 |
+
|
| 72 |
+
# Insert annotated image (resize for PDF)
|
| 73 |
+
pdf.image(annotated_image_file, x=10, y=pdf.get_y(), w=pdf.w - 20)
|
| 74 |
+
|
| 75 |
+
pdf.output(report_file)
|
| 76 |
+
print(f"Report saved at: {report_file}")
|
| 77 |
+
print(f"Annotated image saved at: {annotated_image_file}")
|
| 78 |
+
|
| 79 |
+
def main(image_path):
|
| 80 |
+
print("Running OCR...")
|
| 81 |
+
extracted_texts = run_ocr(image_path)
|
| 82 |
+
print("Extracted Texts:", extracted_texts)
|
| 83 |
+
|
| 84 |
+
print("Analyzing UV sterilization coverage...")
|
| 85 |
+
annotated_img, coverage_percent = analyze_uv_coverage(image_path)
|
| 86 |
+
|
| 87 |
+
# Show annotated image using matplotlib
|
| 88 |
+
plt.imshow(cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB))
|
| 89 |
+
plt.title(f"UV Sterilization Coverage: {coverage_percent:.2f}%")
|
| 90 |
+
plt.axis('off')
|
| 91 |
+
plt.show()
|
| 92 |
+
|
| 93 |
+
# Save report and annotated image
|
| 94 |
+
save_report(image_path, coverage_percent, extracted_texts)
|
| 95 |
+
|
| 96 |
+
if __name__ == '__main__':
|
| 97 |
+
import argparse
|
| 98 |
+
|
| 99 |
+
parser = argparse.ArgumentParser(description="UV Sterilization Analysis App")
|
| 100 |
+
parser.add_argument('image_path', type=str, help='Path to post-UV sterilization image')
|
| 101 |
+
args = parser.parse_args()
|
| 102 |
+
|
| 103 |
+
main(args.image_path)
|