Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
-
import
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from fpdf import FPDF
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
from PIL import Image
|
| 8 |
-
import
|
| 9 |
|
|
|
|
| 10 |
def analyze_uv_coverage(img, brightness_threshold=150):
|
| 11 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 12 |
_, binary_mask = cv2.threshold(gray, brightness_threshold, 255, cv2.THRESH_BINARY)
|
|
@@ -22,6 +23,7 @@ def analyze_uv_coverage(img, brightness_threshold=150):
|
|
| 22 |
annotated_img = cv2.addWeighted(img, 0.6, overlay, 0.4, 0)
|
| 23 |
return annotated_img, coverage_percent
|
| 24 |
|
|
|
|
| 25 |
def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, output_path):
|
| 26 |
pdf = FPDF()
|
| 27 |
pdf.add_page()
|
|
@@ -35,60 +37,66 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
|
|
| 35 |
|
| 36 |
pdf.cell(0, 10, "Extracted Texts from Image (OCR):", ln=True)
|
| 37 |
pdf.set_font("Arial", size=10)
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
pdf.ln(10)
|
| 42 |
pdf.cell(0, 10, "Annotated Image:", ln=True)
|
| 43 |
pdf.image(annotated_image_path, x=10, y=pdf.get_y(), w=pdf.w - 20)
|
| 44 |
pdf.output(output_path)
|
| 45 |
|
| 46 |
-
def
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 55 |
-
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
extracted_texts = [item['text'] for item in ocr_result if item['text'].strip() != ""]
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
for txt in extracted_texts:
|
| 69 |
-
st.write(f"- {txt}")
|
| 70 |
-
else:
|
| 71 |
-
st.write("No text detected.")
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
create_pdf_report(coverage_percent, extracted_texts, temp_img_file.name, temp_pdf_file.name)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
with open(temp_pdf_file.name, "rb") as f:
|
| 86 |
-
pdf_bytes = f.read()
|
| 87 |
-
st.download_button(label="Download UV Sterilization Report (PDF)", data=pdf_bytes, file_name="UV_Sterilization_Report.pdf")
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from fpdf import FPDF
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
from PIL import Image
|
| 8 |
+
import pytesseract
|
| 9 |
|
| 10 |
+
# Analyze UV coverage by brightness threshold
|
| 11 |
def analyze_uv_coverage(img, brightness_threshold=150):
|
| 12 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 13 |
_, binary_mask = cv2.threshold(gray, brightness_threshold, 255, cv2.THRESH_BINARY)
|
|
|
|
| 23 |
annotated_img = cv2.addWeighted(img, 0.6, overlay, 0.4, 0)
|
| 24 |
return annotated_img, coverage_percent
|
| 25 |
|
| 26 |
+
# Create PDF report with coverage, OCR text, and annotated image
|
| 27 |
def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, output_path):
|
| 28 |
pdf = FPDF()
|
| 29 |
pdf.add_page()
|
|
|
|
| 37 |
|
| 38 |
pdf.cell(0, 10, "Extracted Texts from Image (OCR):", ln=True)
|
| 39 |
pdf.set_font("Arial", size=10)
|
| 40 |
+
if extracted_texts:
|
| 41 |
+
for text in extracted_texts:
|
| 42 |
+
pdf.multi_cell(0, 8, f"- {text}")
|
| 43 |
+
else:
|
| 44 |
+
pdf.cell(0, 8, "No text detected.", ln=True)
|
| 45 |
|
| 46 |
pdf.ln(10)
|
| 47 |
pdf.cell(0, 10, "Annotated Image:", ln=True)
|
| 48 |
pdf.image(annotated_image_path, x=10, y=pdf.get_y(), w=pdf.w - 20)
|
| 49 |
pdf.output(output_path)
|
| 50 |
|
| 51 |
+
def process_image(input_img, brightness_threshold=150):
|
| 52 |
+
# Convert PIL image to OpenCV format
|
| 53 |
+
img = cv2.cvtColor(np.array(input_img), cv2.COLOR_RGB2BGR)
|
| 54 |
|
| 55 |
+
# Run OCR using pytesseract
|
| 56 |
+
ocr_result = pytesseract.image_to_string(input_img)
|
| 57 |
+
extracted_texts = [line.strip() for line in ocr_result.split('\n') if line.strip()]
|
| 58 |
|
| 59 |
+
# Analyze UV coverage
|
| 60 |
+
annotated_img, coverage_percent = analyze_uv_coverage(img, brightness_threshold)
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
# Save annotated image temporarily for PDF
|
| 63 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img_file:
|
| 64 |
+
cv2.imwrite(temp_img_file.name, annotated_img)
|
|
|
|
| 65 |
|
| 66 |
+
# Save PDF report temporarily
|
| 67 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf_file:
|
| 68 |
+
create_pdf_report(coverage_percent, extracted_texts, temp_img_file.name, temp_pdf_file.name)
|
| 69 |
|
| 70 |
+
# Read PDF bytes for download
|
| 71 |
+
with open(temp_pdf_file.name, "rb") as f:
|
| 72 |
+
pdf_bytes = f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Convert annotated image back to RGB for display
|
| 75 |
+
annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
| 76 |
|
| 77 |
+
# Clean up temp files
|
| 78 |
+
os.unlink(temp_img_file.name)
|
| 79 |
+
os.unlink(temp_pdf_file.name)
|
| 80 |
|
| 81 |
+
report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%\n\nExtracted Texts:\n"
|
| 82 |
+
report_text += "\n".join(extracted_texts) if extracted_texts else "No text detected."
|
|
|
|
| 83 |
|
| 84 |
+
return annotated_img_rgb, report_text, pdf_bytes
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
iface = gr.Interface(
|
| 87 |
+
fn=process_image,
|
| 88 |
+
inputs=[
|
| 89 |
+
gr.Image(type="pil", label="Upload Post-UV Sterilization Image"),
|
| 90 |
+
gr.Slider(50, 255, value=150, step=1, label="Brightness Threshold")
|
| 91 |
+
],
|
| 92 |
+
outputs=[
|
| 93 |
+
gr.Image(type="numpy", label="Annotated Image"),
|
| 94 |
+
gr.Textbox(label="UV Sterilization Report", lines=10),
|
| 95 |
+
gr.File(label="Download PDF Report")
|
| 96 |
+
],
|
| 97 |
+
title="UV Sterilization Coverage Analyzer",
|
| 98 |
+
description="Upload a post-UV sterilization image to analyze surface coverage and generate a compliance report."
|
| 99 |
+
)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
iface.launch()
|