Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from fpdf import FPDF
|
|
@@ -11,7 +11,6 @@ from datetime import datetime
|
|
| 11 |
import base64
|
| 12 |
import io
|
| 13 |
import logging
|
| 14 |
-
from PIL import Image
|
| 15 |
|
| 16 |
# Set up logging to debug file writing issues
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -180,42 +179,19 @@ def save_record_to_salesforce(annotated_image_url, coverage_percent, original_im
|
|
| 180 |
})
|
| 181 |
|
| 182 |
def process_image(input_img, brightness_threshold=150):
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
# Validate input image
|
| 186 |
-
if input_img is None:
|
| 187 |
-
raise ValueError("No image provided")
|
| 188 |
-
|
| 189 |
-
if isinstance(input_img, Image.Image):
|
| 190 |
-
img = np.array(input_img)
|
| 191 |
-
else:
|
| 192 |
-
raise ValueError(f"Unsupported image type: {type(input_img)}")
|
| 193 |
|
| 194 |
-
|
| 195 |
-
raise ValueError("Invalid image format: Expected a 3-channel color image")
|
| 196 |
-
|
| 197 |
-
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 198 |
-
|
| 199 |
-
# Resize large images
|
| 200 |
max_dim = 640
|
| 201 |
h, w = img.shape[:2]
|
| 202 |
-
if h < 10 or w < 10 or h > 4096 or w > 4096:
|
| 203 |
-
raise ValueError(f"Invalid image dimensions: {w}x{h}")
|
| 204 |
-
if img.size == 0:
|
| 205 |
-
raise ValueError("Empty image provided")
|
| 206 |
-
|
| 207 |
if max(h, w) > max_dim:
|
| 208 |
scale = max_dim / max(h, w)
|
| 209 |
img = cv2.resize(img, (int(w * scale), int(h * scale)))
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
ocr_result = ocr_model.predict(img)
|
| 215 |
-
ocr_time = time.time() - start_time
|
| 216 |
-
except Exception as e:
|
| 217 |
-
logger.error(f"PaddleOCR failed: {str(e)}", exc_info=True)
|
| 218 |
-
raise RuntimeError(f"PaddleOCR processing failed: {str(e)}")
|
| 219 |
|
| 220 |
extracted_texts = []
|
| 221 |
for line in ocr_result:
|
|
@@ -235,31 +211,33 @@ def process_image(input_img, brightness_threshold=150):
|
|
| 235 |
temp_pdf_file.close()
|
| 236 |
create_pdf_report(coverage_percent, extracted_texts, annotated_img_path, temp_pdf_file.name)
|
| 237 |
|
|
|
|
| 238 |
annotated_image_url = upload_image_and_get_url(annotated_img_path)
|
|
|
|
|
|
|
| 239 |
save_record_to_salesforce(annotated_image_url, coverage_percent, input_img)
|
| 240 |
|
| 241 |
annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
|
|
|
| 242 |
report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%"
|
|
|
|
|
|
|
| 243 |
os.unlink(annotated_img_path)
|
| 244 |
|
| 245 |
return annotated_img_rgb, report_text, temp_pdf_file.name
|
| 246 |
|
| 247 |
iface = gr.Interface(
|
| 248 |
fn=process_image,
|
| 249 |
-
inputs=[
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
gr.Image(type="numpy", label="Annotated Image"),
|
| 255 |
-
gr.Textbox(label="UV Sterilization Report", lines=5),
|
| 256 |
-
gr.File(label="Download PDF Report")
|
| 257 |
-
],
|
| 258 |
title="UV Sterilization Coverage Analyzer",
|
| 259 |
description="Upload a post-UV sterilization image to analyze surface coverage and generate a compliance report."
|
| 260 |
)
|
| 261 |
|
| 262 |
-
iface.queue(
|
| 263 |
|
| 264 |
if __name__ == "__main__":
|
| 265 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from fpdf import FPDF
|
|
|
|
| 11 |
import base64
|
| 12 |
import io
|
| 13 |
import logging
|
|
|
|
| 14 |
|
| 15 |
# Set up logging to debug file writing issues
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 179 |
})
|
| 180 |
|
| 181 |
def process_image(input_img, brightness_threshold=150):
|
| 182 |
+
img = np.array(input_img) # Convert PIL Image to NumPy array
|
| 183 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Convert from RGB to BGR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
# Resize large images for faster processing, preserving aspect ratio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
max_dim = 640
|
| 187 |
h, w = img.shape[:2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
if max(h, w) > max_dim:
|
| 189 |
scale = max_dim / max(h, w)
|
| 190 |
img = cv2.resize(img, (int(w * scale), int(h * scale)))
|
| 191 |
|
| 192 |
+
start_time = time.time()
|
| 193 |
+
ocr_result = ocr_model.predict(img) # Use the `predict` method here instead of `ocr`
|
| 194 |
+
ocr_time = time.time() - start_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
extracted_texts = []
|
| 197 |
for line in ocr_result:
|
|
|
|
| 211 |
temp_pdf_file.close()
|
| 212 |
create_pdf_report(coverage_percent, extracted_texts, annotated_img_path, temp_pdf_file.name)
|
| 213 |
|
| 214 |
+
# Upload annotated image and get URL
|
| 215 |
annotated_image_url = upload_image_and_get_url(annotated_img_path)
|
| 216 |
+
|
| 217 |
+
# Save record in Salesforce
|
| 218 |
save_record_to_salesforce(annotated_image_url, coverage_percent, input_img)
|
| 219 |
|
| 220 |
annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
| 221 |
+
|
| 222 |
report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%"
|
| 223 |
+
|
| 224 |
+
# Clean up temp image file after PDF generation
|
| 225 |
os.unlink(annotated_img_path)
|
| 226 |
|
| 227 |
return annotated_img_rgb, report_text, temp_pdf_file.name
|
| 228 |
|
| 229 |
iface = gr.Interface(
|
| 230 |
fn=process_image,
|
| 231 |
+
inputs=[gr.Image(type="pil", label="Upload Post-UV Sterilization Image"),
|
| 232 |
+
gr.Slider(50, 255, value=150, step=1, label="Brightness Threshold")],
|
| 233 |
+
outputs=[gr.Image(type="numpy", label="Annotated Image"),
|
| 234 |
+
gr.Textbox(label="UV Sterilization Report", lines=5),
|
| 235 |
+
gr.File(label="Download PDF Report")],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
title="UV Sterilization Coverage Analyzer",
|
| 237 |
description="Upload a post-UV sterilization image to analyze surface coverage and generate a compliance report."
|
| 238 |
)
|
| 239 |
|
| 240 |
+
iface.queue() # Enable request queuing to improve UX on heavy processing
|
| 241 |
|
| 242 |
if __name__ == "__main__":
|
| 243 |
iface.launch()
|