Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,28 @@
|
|
|
|
|
| 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 |
-
|
| 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]
|
| 35 |
-
overlay[binary_mask == 0] = [0, 0, 255]
|
| 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
|
| 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)
|
|
@@ -67,37 +39,56 @@ def save_report(image_path, coverage_percent, extracted_texts, output_folder='ou
|
|
| 67 |
pdf.multi_cell(0, 8, f"- {text}")
|
| 68 |
|
| 69 |
pdf.ln(10)
|
| 70 |
-
pdf.cell(0, 10, "Annotated Image
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 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 io
|
| 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)
|
| 13 |
|
|
|
|
| 14 |
total_pixels = binary_mask.size
|
| 15 |
sterilized_pixels = cv2.countNonZero(binary_mask)
|
| 16 |
coverage_percent = (sterilized_pixels / total_pixels) * 100
|
| 17 |
|
|
|
|
| 18 |
overlay = img.copy()
|
| 19 |
+
overlay[binary_mask == 255] = [0, 255, 0] # Green sterilized
|
| 20 |
+
overlay[binary_mask == 0] = [0, 0, 255] # Red unsterilized
|
| 21 |
|
|
|
|
| 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()
|
| 28 |
pdf.set_font("Arial", size=14)
|
|
|
|
| 39 |
pdf.multi_cell(0, 8, f"- {text}")
|
| 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 main():
|
| 47 |
+
st.title("UV Sterilization Coverage Analyzer")
|
| 48 |
+
|
| 49 |
+
uploaded_file = st.file_uploader("Upload post-UV sterilization image", type=['png','jpg','jpeg'])
|
| 50 |
+
brightness_threshold = st.slider("Brightness Threshold (for sterilized zone detection)", 50, 255, 150)
|
| 51 |
+
|
| 52 |
+
if uploaded_file is not None:
|
| 53 |
+
# Read image
|
| 54 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 55 |
+
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 56 |
+
|
| 57 |
+
# Run OCR with Streamlit experimental API (uses Tesseract)
|
| 58 |
+
pil_image = Image.open(uploaded_file)
|
| 59 |
+
ocr_result = st.experimental_text_ocr(pil_image)
|
| 60 |
+
extracted_texts = [item['text'] for item in ocr_result if item['text'].strip() != ""]
|
| 61 |
+
|
| 62 |
+
# Analyze UV coverage
|
| 63 |
+
annotated_img, coverage_percent = analyze_uv_coverage(img, brightness_threshold)
|
| 64 |
+
|
| 65 |
+
# Show results
|
| 66 |
+
st.subheader("Extracted Text from Image:")
|
| 67 |
+
if extracted_texts:
|
| 68 |
+
for txt in extracted_texts:
|
| 69 |
+
st.write(f"- {txt}")
|
| 70 |
+
else:
|
| 71 |
+
st.write("No text detected.")
|
| 72 |
+
|
| 73 |
+
st.subheader(f"UV Sterilization Coverage: {coverage_percent:.2f}%")
|
| 74 |
+
st.image(cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB), caption="Annotated Image (Green = Sterilized, Red = Unsterilized)")
|
| 75 |
+
|
| 76 |
+
# Save annotated image temporarily
|
| 77 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img_file:
|
| 78 |
+
cv2.imwrite(temp_img_file.name, annotated_img)
|
| 79 |
+
|
| 80 |
+
# Create PDF report
|
| 81 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf_file:
|
| 82 |
+
create_pdf_report(coverage_percent, extracted_texts, temp_img_file.name, temp_pdf_file.name)
|
| 83 |
+
|
| 84 |
+
# Provide download link for PDF
|
| 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 |
+
# Cleanup temp files on app rerun
|
| 90 |
+
os.unlink(temp_img_file.name)
|
| 91 |
+
os.unlink(temp_pdf_file.name)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
main()
|