pavansuresh commited on
Commit
9c6cf28
·
verified ·
1 Parent(s): 168adbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -19
app.py CHANGED
@@ -11,7 +11,6 @@ import time
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
 
@@ -33,22 +32,18 @@ def analyze_uv_coverage(img, brightness_threshold=150):
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:
@@ -59,49 +54,41 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
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):
70
- # Convert PIL image to OpenCV format
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)
84
 
85
- # Save annotated image temporarily for PDF
86
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img_file:
87
  cv2.imwrite(temp_img_file.name, annotated_img)
88
 
89
- # Save PDF report temporarily
90
  temp_pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
91
- temp_pdf_file.close() # Close so FPDF can write to it
92
  create_pdf_report(coverage_percent, extracted_texts, temp_img_file.name, temp_pdf_file.name)
93
 
94
- # Convert annotated image back to RGB for display
95
  annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
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)
103
 
104
- # Return annotated image, report text, and PDF file path
105
  return annotated_img_rgb, report_text, temp_pdf_file.name
106
 
107
  iface = gr.Interface(
 
11
  # Initialize PaddleOCR once with improved settings
12
  ocr_model = PaddleOCR(use_angle_cls=True, lang='en', rec=True, det=True)
13
 
 
14
  def analyze_uv_coverage(img, brightness_threshold=150):
15
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
16
 
 
32
  annotated_img = cv2.addWeighted(img, 0.6, overlay, 0.4, 0)
33
  return annotated_img, coverage_percent
34
 
 
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
 
 
43
  pdf.set_font("Arial", size=12)
44
  pdf.cell(0, 10, f"Sterilization Coverage: {coverage_percent:.2f}%", ln=True)
45
  pdf.ln(5)
46
 
 
47
  pdf.cell(0, 10, "Extracted Text from Image (OCR):", ln=True)
48
  pdf.set_font("Arial", size=10)
49
  if extracted_texts:
 
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
 
 
60
  pdf.output(output_path)
61
 
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
72
+ for word_info in line:
73
+ extracted_texts.append(word_info[1][0])
74
 
 
75
  annotated_img, coverage_percent = analyze_uv_coverage(img, brightness_threshold)
76
 
 
77
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img_file:
78
  cv2.imwrite(temp_img_file.name, annotated_img)
79
 
 
80
  temp_pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
81
+ temp_pdf_file.close()
82
  create_pdf_report(coverage_percent, extracted_texts, temp_img_file.name, temp_pdf_file.name)
83
 
 
84
  annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
85
 
86
  report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%\n\nExtracted Texts:\n"
87
  report_text += "\n".join(extracted_texts) if extracted_texts else "No text detected."
88
  report_text += f"\n\nOCR Processing Time: {ocr_time:.2f} seconds"
89
 
 
90
  os.unlink(temp_img_file.name)
91
 
 
92
  return annotated_img_rgb, report_text, temp_pdf_file.name
93
 
94
  iface = gr.Interface(