pavansuresh commited on
Commit
168adbc
·
verified ·
1 Parent(s): 32d99a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -5
app.py CHANGED
@@ -6,14 +6,22 @@ import tempfile
6
  import os
7
  from PIL import Image
8
  from paddleocr import PaddleOCR
 
9
 
10
- # Initialize PaddleOCR once
11
- ocr_model = PaddleOCR(use_angle_cls=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
  total_pixels = binary_mask.size
18
  sterilized_pixels = cv2.countNonZero(binary_mask)
19
  coverage_percent = (sterilized_pixels / total_pixels) * 100
@@ -25,18 +33,23 @@ def analyze_uv_coverage(img, brightness_threshold=150):
25
  annotated_img = cv2.addWeighted(img, 0.6, overlay, 0.4, 0)
26
  return annotated_img, coverage_percent
27
 
 
28
  def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, output_path):
29
  pdf = FPDF()
30
  pdf.add_page()
31
- pdf.set_font("Arial", size=14)
 
 
32
  pdf.cell(200, 10, txt="UV Sterilization Report", ln=True, align='C')
33
-
34
  pdf.ln(10)
 
 
35
  pdf.set_font("Arial", size=12)
36
  pdf.cell(0, 10, f"Sterilization Coverage: {coverage_percent:.2f}%", ln=True)
37
  pdf.ln(5)
38
 
39
- pdf.cell(0, 10, "Extracted Texts from Image (OCR):", ln=True)
 
40
  pdf.set_font("Arial", size=10)
41
  if extracted_texts:
42
  for text in extracted_texts:
@@ -45,8 +58,12 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
45
  pdf.cell(0, 8, "No text detected.", ln=True)
46
 
47
  pdf.ln(10)
 
 
48
  pdf.cell(0, 10, "Annotated Image:", ln=True)
49
  pdf.image(annotated_image_path, x=10, y=pdf.get_y(), w=pdf.w - 20)
 
 
50
  pdf.output(output_path)
51
 
52
  def process_image(input_img, brightness_threshold=150):
@@ -54,11 +71,13 @@ def process_image(input_img, brightness_threshold=150):
54
  img = cv2.cvtColor(np.array(input_img), cv2.COLOR_RGB2BGR)
55
 
56
  # Run OCR using PaddleOCR
 
57
  ocr_result = ocr_model.ocr(np.array(input_img))
58
  extracted_texts = []
59
  for line in ocr_result:
60
  for word_info in line:
61
  extracted_texts.append(word_info[1][0])
 
62
 
63
  # Analyze UV coverage
64
  annotated_img, coverage_percent = analyze_uv_coverage(img, brightness_threshold)
@@ -77,6 +96,7 @@ def process_image(input_img, brightness_threshold=150):
77
 
78
  report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%\n\nExtracted Texts:\n"
79
  report_text += "\n".join(extracted_texts) if extracted_texts else "No text detected."
 
80
 
81
  # Delete annotated image file to avoid temp buildup
82
  os.unlink(temp_img_file.name)
 
6
  import os
7
  from PIL import Image
8
  from paddleocr import PaddleOCR
9
+ import time
10
 
11
+ # Initialize PaddleOCR once with improved settings
12
+ ocr_model = PaddleOCR(use_angle_cls=True, lang='en', rec=True, det=True)
13
 
14
+ # Function to analyze UV sterilization coverage (with morphological operations)
15
  def analyze_uv_coverage(img, brightness_threshold=150):
16
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
17
+
18
+ # Apply threshold to identify sterilized vs unsterilized zones
19
  _, binary_mask = cv2.threshold(gray, brightness_threshold, 255, cv2.THRESH_BINARY)
20
 
21
+ # Apply morphological operations for better segmentation
22
+ kernel = np.ones((5, 5), np.uint8)
23
+ binary_mask = cv2.dilate(binary_mask, kernel, iterations=1) # Dilation to fill gaps
24
+
25
  total_pixels = binary_mask.size
26
  sterilized_pixels = cv2.countNonZero(binary_mask)
27
  coverage_percent = (sterilized_pixels / total_pixels) * 100
 
33
  annotated_img = cv2.addWeighted(img, 0.6, overlay, 0.4, 0)
34
  return annotated_img, coverage_percent
35
 
36
+ # Enhanced function to create a better PDF report
37
  def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, output_path):
38
  pdf = FPDF()
39
  pdf.add_page()
40
+
41
+ # Title and general info
42
+ pdf.set_font("Arial", 'B', size=16)
43
  pdf.cell(200, 10, txt="UV Sterilization Report", ln=True, align='C')
 
44
  pdf.ln(10)
45
+
46
+ # Sterilization Coverage
47
  pdf.set_font("Arial", size=12)
48
  pdf.cell(0, 10, f"Sterilization Coverage: {coverage_percent:.2f}%", ln=True)
49
  pdf.ln(5)
50
 
51
+ # OCR Text extraction in a formatted way
52
+ pdf.cell(0, 10, "Extracted Text from Image (OCR):", ln=True)
53
  pdf.set_font("Arial", size=10)
54
  if extracted_texts:
55
  for text in extracted_texts:
 
58
  pdf.cell(0, 8, "No text detected.", ln=True)
59
 
60
  pdf.ln(10)
61
+
62
+ # Annotated Image Section
63
  pdf.cell(0, 10, "Annotated Image:", ln=True)
64
  pdf.image(annotated_image_path, x=10, y=pdf.get_y(), w=pdf.w - 20)
65
+
66
+ # Save the PDF to the specified path
67
  pdf.output(output_path)
68
 
69
  def process_image(input_img, brightness_threshold=150):
 
71
  img = cv2.cvtColor(np.array(input_img), cv2.COLOR_RGB2BGR)
72
 
73
  # Run OCR using PaddleOCR
74
+ start_time = time.time()
75
  ocr_result = ocr_model.ocr(np.array(input_img))
76
  extracted_texts = []
77
  for line in ocr_result:
78
  for word_info in line:
79
  extracted_texts.append(word_info[1][0])
80
+ ocr_time = time.time() - start_time
81
 
82
  # Analyze UV coverage
83
  annotated_img, coverage_percent = analyze_uv_coverage(img, brightness_threshold)
 
96
 
97
  report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%\n\nExtracted Texts:\n"
98
  report_text += "\n".join(extracted_texts) if extracted_texts else "No text detected."
99
+ report_text += f"\n\nOCR Processing Time: {ocr_time:.2f} seconds"
100
 
101
  # Delete annotated image file to avoid temp buildup
102
  os.unlink(temp_img_file.name)