pavansuresh commited on
Commit
12893c6
·
verified ·
1 Parent(s): 098c355

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -4,23 +4,19 @@ import numpy as np
4
  from fpdf import FPDF
5
  import tempfile
6
  import os
7
- from PIL import Image
8
  from paddleocr import PaddleOCR
9
  import time
10
 
11
- # Initialize PaddleOCR with updated parameters
12
  ocr_model = PaddleOCR(use_textline_orientation=True, lang='en')
13
 
14
  def analyze_uv_coverage(img, brightness_threshold=150):
15
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
16
-
17
- # Apply threshold to identify sterilized vs unsterilized zones
18
  _, binary_mask = cv2.threshold(gray, brightness_threshold, 255, cv2.THRESH_BINARY)
19
 
20
- # Apply morphological operations for better segmentation
21
  kernel = np.ones((5, 5), np.uint8)
22
- binary_mask = cv2.dilate(binary_mask, kernel, iterations=1) # Dilation to fill gaps
23
-
24
  total_pixels = binary_mask.size
25
  sterilized_pixels = cv2.countNonZero(binary_mask)
26
  coverage_percent = (sterilized_pixels / total_pixels) * 100
@@ -35,8 +31,8 @@ def analyze_uv_coverage(img, brightness_threshold=150):
35
  def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, output_path):
36
  pdf = FPDF()
37
  pdf.add_page()
38
-
39
- pdf.set_font("Arial", 'B', size=16)
40
  pdf.cell(200, 10, txt="UV Sterilization Report", ln=True, align='C')
41
  pdf.ln(10)
42
 
@@ -53,7 +49,6 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
53
  pdf.cell(0, 8, "No text detected.", ln=True)
54
 
55
  pdf.ln(10)
56
-
57
  pdf.cell(0, 10, "Annotated Image:", ln=True)
58
  pdf.image(annotated_image_path, x=10, y=pdf.get_y(), w=pdf.w - 20)
59
 
@@ -62,13 +57,20 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
62
  def process_image(input_img, brightness_threshold=150):
63
  img = cv2.cvtColor(np.array(input_img), cv2.COLOR_RGB2BGR)
64
 
 
 
 
 
 
 
 
65
  start_time = time.time()
66
- ocr_result = ocr_model.ocr(np.array(input_img))
67
  ocr_time = time.time() - start_time
68
 
69
  extracted_texts = []
70
  for line in ocr_result:
71
- if line: # Skip None or empty entries
72
  for word_info in line:
73
  extracted_texts.append(word_info[1][0])
74
 
@@ -83,9 +85,8 @@ def process_image(input_img, brightness_threshold=150):
83
 
84
  annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
85
 
86
- # Hide extracted texts in output; show only coverage percentage
87
  report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%"
88
- # Optionally include OCR processing time
89
  # report_text += f"\n\nOCR Processing Time: {ocr_time:.2f} seconds"
90
 
91
  os.unlink(temp_img_file.name)
@@ -107,5 +108,7 @@ iface = gr.Interface(
107
  description="Upload a post-UV sterilization image to analyze surface coverage and generate a compliance report."
108
  )
109
 
 
 
110
  if __name__ == "__main__":
111
  iface.launch()
 
4
  from fpdf import FPDF
5
  import tempfile
6
  import os
 
7
  from paddleocr import PaddleOCR
8
  import time
9
 
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
  kernel = np.ones((5, 5), np.uint8)
18
+ binary_mask = cv2.dilate(binary_mask, kernel, iterations=1)
19
+
20
  total_pixels = binary_mask.size
21
  sterilized_pixels = cv2.countNonZero(binary_mask)
22
  coverage_percent = (sterilized_pixels / total_pixels) * 100
 
31
  def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, output_path):
32
  pdf = FPDF()
33
  pdf.add_page()
34
+
35
+ pdf.set_font("Arial", 'B', 16)
36
  pdf.cell(200, 10, txt="UV Sterilization Report", ln=True, align='C')
37
  pdf.ln(10)
38
 
 
49
  pdf.cell(0, 8, "No text detected.", ln=True)
50
 
51
  pdf.ln(10)
 
52
  pdf.cell(0, 10, "Annotated Image:", ln=True)
53
  pdf.image(annotated_image_path, x=10, y=pdf.get_y(), w=pdf.w - 20)
54
 
 
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:
64
+ scale = max_dim / max(h, w)
65
+ img = cv2.resize(img, (int(w * scale), int(h * scale)))
66
+
67
  start_time = time.time()
68
+ ocr_result = ocr_model.ocr(img)
69
  ocr_time = time.time() - start_time
70
 
71
  extracted_texts = []
72
  for line in ocr_result:
73
+ if line:
74
  for word_info in line:
75
  extracted_texts.append(word_info[1][0])
76
 
 
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"\n\nOCR Processing Time: {ocr_time:.2f} seconds"
91
 
92
  os.unlink(temp_img_file.name)
 
108
  description="Upload a post-UV sterilization image to analyze surface coverage and generate a compliance report."
109
  )
110
 
111
+ iface.queue() # Enable request queuing to improve UX on heavy processing
112
+
113
  if __name__ == "__main__":
114
  iface.launch()