Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,10 @@ 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')
|
|
@@ -78,6 +82,43 @@ def create_pdf_report(coverage_percent, extracted_texts, annotated_image_path, o
|
|
| 78 |
|
| 79 |
pdf.output(output_path)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def process_image(input_img, brightness_threshold=150):
|
| 82 |
img = cv2.cvtColor(np.array(input_img), cv2.COLOR_RGB2BGR)
|
| 83 |
|
|
@@ -96,7 +137,6 @@ def process_image(input_img, brightness_threshold=150):
|
|
| 96 |
for line in ocr_result:
|
| 97 |
if line:
|
| 98 |
for word_info in line:
|
| 99 |
-
# Filter short strings and whitespace only
|
| 100 |
text = word_info[1][0].strip()
|
| 101 |
if len(text) > 1:
|
| 102 |
extracted_texts.append(text)
|
|
@@ -111,11 +151,15 @@ def process_image(input_img, brightness_threshold=150):
|
|
| 111 |
temp_pdf_file.close()
|
| 112 |
create_pdf_report(coverage_percent, extracted_texts, annotated_img_path, temp_pdf_file.name)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
| 115 |
|
| 116 |
report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%"
|
| 117 |
-
# Optionally include OCR time for debugging:
|
| 118 |
-
# report_text += f"\nOCR Processing Time: {ocr_time:.2f} seconds"
|
| 119 |
|
| 120 |
# Clean up temp image file after PDF generation
|
| 121 |
os.unlink(annotated_img_path)
|
|
|
|
| 6 |
import os
|
| 7 |
from paddleocr import PaddleOCR
|
| 8 |
import time
|
| 9 |
+
from simple_salesforce import Salesforce
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
import base64
|
| 12 |
+
import io
|
| 13 |
|
| 14 |
# Initialize PaddleOCR once with updated parameters
|
| 15 |
ocr_model = PaddleOCR(use_textline_orientation=True, lang='en')
|
|
|
|
| 82 |
|
| 83 |
pdf.output(output_path)
|
| 84 |
|
| 85 |
+
def upload_image_and_get_url(image_path):
|
| 86 |
+
"""
|
| 87 |
+
TODO: Implement your image upload to public storage here.
|
| 88 |
+
For now, returns a placeholder URL.
|
| 89 |
+
"""
|
| 90 |
+
# Example: upload to AWS S3, Azure Blob Storage, or other service
|
| 91 |
+
# Return the public URL to the uploaded image
|
| 92 |
+
return "https://example.com/path/to/your/annotated_image.jpg"
|
| 93 |
+
|
| 94 |
+
def save_record_to_salesforce(annotated_image_url, coverage_percent, original_image_pil, compliance_threshold=80):
|
| 95 |
+
sf = Salesforce(
|
| 96 |
+
username=os.environ['SF_USERNAME'],
|
| 97 |
+
password=os.environ['SF_PASSWORD'],
|
| 98 |
+
security_token=os.environ['SF_SECURITY_TOKEN'],
|
| 99 |
+
domain=os.environ.get('SF_DOMAIN', 'login') # 'test' for sandbox
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Encode original image to base64 string for storage
|
| 103 |
+
buffered = io.BytesIO()
|
| 104 |
+
original_image_pil.save(buffered, format="JPEG")
|
| 105 |
+
original_img_b64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 106 |
+
|
| 107 |
+
compliance_status = 'Pass' if coverage_percent >= compliance_threshold else 'Fail'
|
| 108 |
+
technician_id = os.environ.get('SF_TECHNICIAN_ID') # Salesforce UserId lookup
|
| 109 |
+
|
| 110 |
+
record_name = f"UV Verification - {datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')}"
|
| 111 |
+
|
| 112 |
+
sf.UV_Verification__c.create({
|
| 113 |
+
'Name': record_name,
|
| 114 |
+
'Annotated_Image__c': annotated_image_url,
|
| 115 |
+
'Coverage_Percentage__c': round(coverage_percent, 2),
|
| 116 |
+
'Original_Image__c': original_img_b64,
|
| 117 |
+
'Compliance_Status__c': compliance_status,
|
| 118 |
+
'Technician_ID__c': technician_id,
|
| 119 |
+
'Verified_On__c': datetime.utcnow().isoformat()
|
| 120 |
+
})
|
| 121 |
+
|
| 122 |
def process_image(input_img, brightness_threshold=150):
|
| 123 |
img = cv2.cvtColor(np.array(input_img), cv2.COLOR_RGB2BGR)
|
| 124 |
|
|
|
|
| 137 |
for line in ocr_result:
|
| 138 |
if line:
|
| 139 |
for word_info in line:
|
|
|
|
| 140 |
text = word_info[1][0].strip()
|
| 141 |
if len(text) > 1:
|
| 142 |
extracted_texts.append(text)
|
|
|
|
| 151 |
temp_pdf_file.close()
|
| 152 |
create_pdf_report(coverage_percent, extracted_texts, annotated_img_path, temp_pdf_file.name)
|
| 153 |
|
| 154 |
+
# Upload annotated image and get URL
|
| 155 |
+
annotated_image_url = upload_image_and_get_url(annotated_img_path)
|
| 156 |
+
|
| 157 |
+
# Save record in Salesforce
|
| 158 |
+
save_record_to_salesforce(annotated_image_url, coverage_percent, input_img)
|
| 159 |
+
|
| 160 |
annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
|
| 161 |
|
| 162 |
report_text = f"UV Sterilization Coverage: {coverage_percent:.2f}%"
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# Clean up temp image file after PDF generation
|
| 165 |
os.unlink(annotated_img_path)
|