Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from PIL import Image, ImageDraw, ImageFont
|
| 3 |
import torch
|
| 4 |
from torchvision import models, transforms
|
|
@@ -7,6 +6,9 @@ import base64
|
|
| 7 |
from io import BytesIO
|
| 8 |
import logging
|
| 9 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Setup logging
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -43,11 +45,9 @@ transform = transforms.Compose([
|
|
| 43 |
])
|
| 44 |
|
| 45 |
# Define valid picklist values for Fault_Type__c and Severity__c
|
| 46 |
-
# Replace these with your actual Salesforce picklist values
|
| 47 |
VALID_FAULT_TYPES = ["Crack", "Rust", "Spalling", "Deformation", "Corrosion"]
|
| 48 |
VALID_SEVERITIES = ["Minor", "Moderate", "Critical"]
|
| 49 |
|
| 50 |
-
# Map confidence score to severity level ensuring valid picklist values
|
| 51 |
def get_severity(score):
|
| 52 |
if score >= 0.9:
|
| 53 |
return "Critical"
|
|
@@ -56,13 +56,10 @@ def get_severity(score):
|
|
| 56 |
else:
|
| 57 |
return "Minor"
|
| 58 |
|
| 59 |
-
# Map defect to a valid picklist fault type
|
| 60 |
def map_defect_type():
|
| 61 |
-
#
|
| 62 |
-
# For now, always return the first valid fault type
|
| 63 |
return VALID_FAULT_TYPES[0]
|
| 64 |
|
| 65 |
-
# Upload annotated image to Salesforce as ContentVersion record
|
| 66 |
def upload_image_to_salesforce(image, filename="detected_image.jpg", record_id=None):
|
| 67 |
try:
|
| 68 |
buffered = BytesIO()
|
|
@@ -81,23 +78,58 @@ def upload_image_to_salesforce(image, filename="detected_image.jpg", record_id=N
|
|
| 81 |
logging.error(f"Failed to upload image to Salesforce: {str(e)}")
|
| 82 |
raise Exception(f"Failed to upload image to Salesforce: {str(e)}")
|
| 83 |
|
| 84 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
def detect_defects(image):
|
| 86 |
if image is None:
|
| 87 |
return None, "No image provided"
|
| 88 |
|
| 89 |
try:
|
| 90 |
-
# Convert image and prepare input tensor
|
| 91 |
image_tensor = transform(image).unsqueeze(0)
|
| 92 |
-
|
| 93 |
with torch.no_grad():
|
| 94 |
predictions = model(image_tensor)
|
| 95 |
|
| 96 |
-
# Copy original image to draw bounding boxes and labels
|
| 97 |
result_image = image.copy()
|
| 98 |
draw = ImageDraw.Draw(result_image)
|
| 99 |
-
|
| 100 |
-
# Use default font
|
| 101 |
try:
|
| 102 |
font = ImageFont.truetype("arial.ttf", 18)
|
| 103 |
except:
|
|
@@ -106,27 +138,20 @@ def detect_defects(image):
|
|
| 106 |
output = []
|
| 107 |
for i in range(len(predictions[0]['boxes'])):
|
| 108 |
score = predictions[0]['scores'][i].item()
|
| 109 |
-
if score < 0.3:
|
| 110 |
continue
|
| 111 |
-
|
| 112 |
box = predictions[0]['boxes'][i].tolist()
|
| 113 |
-
|
| 114 |
defect_type = map_defect_type()
|
| 115 |
severity = get_severity(score)
|
| 116 |
-
|
| 117 |
-
# Append defect info to output list
|
| 118 |
output.append({
|
| 119 |
"type": defect_type,
|
| 120 |
"confidence": round(score, 2),
|
| 121 |
"severity": severity,
|
| 122 |
})
|
| 123 |
-
|
| 124 |
-
# Draw rectangle and label
|
| 125 |
draw.rectangle(box, outline="red", width=3)
|
| 126 |
text = f"{defect_type}: {severity}"
|
| 127 |
draw.text((box[0], box[1] - 20 if box[1] > 20 else box[1],), text, fill="red", font=font)
|
| 128 |
|
| 129 |
-
# If defects found, create Salesforce record & upload annotated image
|
| 130 |
if output:
|
| 131 |
current_date = datetime.now().strftime("%Y-%m-%d")
|
| 132 |
inspection_name = f"Inspection-{current_date}-{len(output):03d}"
|
|
@@ -144,18 +169,30 @@ def detect_defects(image):
|
|
| 144 |
|
| 145 |
record_id = inspection_record.get("id")
|
| 146 |
|
| 147 |
-
|
| 148 |
result_image,
|
| 149 |
filename=f"detected_defect_{record_id}.jpg",
|
| 150 |
record_id=record_id
|
| 151 |
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
output.append({"salesforce_record_id": record_id})
|
|
|
|
| 159 |
except Exception as e:
|
| 160 |
output.append({"error": f"Failed to create Salesforce record: {str(e)}"})
|
| 161 |
|
|
@@ -167,7 +204,6 @@ def detect_defects(image):
|
|
| 167 |
logging.error(f"Detection failed: {str(e)}")
|
| 168 |
return None, f"Detection failed: {str(e)}"
|
| 169 |
|
| 170 |
-
# Gradio interface definition
|
| 171 |
demo = gr.Interface(
|
| 172 |
fn=detect_defects,
|
| 173 |
inputs=gr.Image(type="pil", label="Upload Drone Image"),
|
|
|
|
|
|
|
| 1 |
from PIL import Image, ImageDraw, ImageFont
|
| 2 |
import torch
|
| 3 |
from torchvision import models, transforms
|
|
|
|
| 6 |
from io import BytesIO
|
| 7 |
import logging
|
| 8 |
from datetime import datetime
|
| 9 |
+
from reportlab.lib.pagesizes import letter
|
| 10 |
+
from reportlab.pdfgen import canvas
|
| 11 |
+
import gradio as gr # Add this import here
|
| 12 |
|
| 13 |
# Setup logging
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 45 |
])
|
| 46 |
|
| 47 |
# Define valid picklist values for Fault_Type__c and Severity__c
|
|
|
|
| 48 |
VALID_FAULT_TYPES = ["Crack", "Rust", "Spalling", "Deformation", "Corrosion"]
|
| 49 |
VALID_SEVERITIES = ["Minor", "Moderate", "Critical"]
|
| 50 |
|
|
|
|
| 51 |
def get_severity(score):
|
| 52 |
if score >= 0.9:
|
| 53 |
return "Critical"
|
|
|
|
| 56 |
else:
|
| 57 |
return "Minor"
|
| 58 |
|
|
|
|
| 59 |
def map_defect_type():
|
| 60 |
+
# Always return first valid fault type (customize if needed)
|
|
|
|
| 61 |
return VALID_FAULT_TYPES[0]
|
| 62 |
|
|
|
|
| 63 |
def upload_image_to_salesforce(image, filename="detected_image.jpg", record_id=None):
|
| 64 |
try:
|
| 65 |
buffered = BytesIO()
|
|
|
|
| 78 |
logging.error(f"Failed to upload image to Salesforce: {str(e)}")
|
| 79 |
raise Exception(f"Failed to upload image to Salesforce: {str(e)}")
|
| 80 |
|
| 81 |
+
# New function: generate PDF report bytes from defect list
|
| 82 |
+
def create_pdf_report(defect_list):
|
| 83 |
+
buffer = BytesIO()
|
| 84 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
| 85 |
+
width, height = letter
|
| 86 |
+
|
| 87 |
+
c.setFont("Helvetica-Bold", 14)
|
| 88 |
+
c.drawString(30, height - 50, "Structural Defect Detection Report")
|
| 89 |
+
|
| 90 |
+
c.setFont("Helvetica", 12)
|
| 91 |
+
y = height - 80
|
| 92 |
+
for i, defect in enumerate(defect_list, 1):
|
| 93 |
+
text = f"{i}. Type: {defect['type']}, Confidence: {defect['confidence']}, Severity: {defect['severity']}"
|
| 94 |
+
c.drawString(30, y, text)
|
| 95 |
+
y -= 20
|
| 96 |
+
if y < 50:
|
| 97 |
+
c.showPage()
|
| 98 |
+
c.setFont("Helvetica", 12)
|
| 99 |
+
y = height - 50
|
| 100 |
+
|
| 101 |
+
c.save()
|
| 102 |
+
pdf = buffer.getvalue()
|
| 103 |
+
buffer.close()
|
| 104 |
+
return pdf
|
| 105 |
+
|
| 106 |
+
# New function: upload PDF bytes to Salesforce ContentVersion
|
| 107 |
+
def upload_pdf_to_salesforce(pdf_bytes, filename="report.pdf", record_id=None):
|
| 108 |
+
try:
|
| 109 |
+
pdf_data = base64.b64encode(pdf_bytes).decode("utf-8")
|
| 110 |
+
content_version = sf.ContentVersion.create({
|
| 111 |
+
"Title": filename,
|
| 112 |
+
"PathOnClient": filename,
|
| 113 |
+
"VersionData": pdf_data,
|
| 114 |
+
"FirstPublishLocationId": record_id if record_id else SITE_RECORD_ID
|
| 115 |
+
})
|
| 116 |
+
logging.info(f"PDF uploaded to Salesforce ContentVersion ID: {content_version['id']}")
|
| 117 |
+
return content_version["id"]
|
| 118 |
+
except Exception as e:
|
| 119 |
+
logging.error(f"Failed to upload PDF to Salesforce: {str(e)}")
|
| 120 |
+
raise Exception(f"Failed to upload PDF to Salesforce: {str(e)}")
|
| 121 |
+
|
| 122 |
def detect_defects(image):
|
| 123 |
if image is None:
|
| 124 |
return None, "No image provided"
|
| 125 |
|
| 126 |
try:
|
|
|
|
| 127 |
image_tensor = transform(image).unsqueeze(0)
|
|
|
|
| 128 |
with torch.no_grad():
|
| 129 |
predictions = model(image_tensor)
|
| 130 |
|
|
|
|
| 131 |
result_image = image.copy()
|
| 132 |
draw = ImageDraw.Draw(result_image)
|
|
|
|
|
|
|
| 133 |
try:
|
| 134 |
font = ImageFont.truetype("arial.ttf", 18)
|
| 135 |
except:
|
|
|
|
| 138 |
output = []
|
| 139 |
for i in range(len(predictions[0]['boxes'])):
|
| 140 |
score = predictions[0]['scores'][i].item()
|
| 141 |
+
if score < 0.3:
|
| 142 |
continue
|
|
|
|
| 143 |
box = predictions[0]['boxes'][i].tolist()
|
|
|
|
| 144 |
defect_type = map_defect_type()
|
| 145 |
severity = get_severity(score)
|
|
|
|
|
|
|
| 146 |
output.append({
|
| 147 |
"type": defect_type,
|
| 148 |
"confidence": round(score, 2),
|
| 149 |
"severity": severity,
|
| 150 |
})
|
|
|
|
|
|
|
| 151 |
draw.rectangle(box, outline="red", width=3)
|
| 152 |
text = f"{defect_type}: {severity}"
|
| 153 |
draw.text((box[0], box[1] - 20 if box[1] > 20 else box[1],), text, fill="red", font=font)
|
| 154 |
|
|
|
|
| 155 |
if output:
|
| 156 |
current_date = datetime.now().strftime("%Y-%m-%d")
|
| 157 |
inspection_name = f"Inspection-{current_date}-{len(output):03d}"
|
|
|
|
| 169 |
|
| 170 |
record_id = inspection_record.get("id")
|
| 171 |
|
| 172 |
+
content_version_id_img = upload_image_to_salesforce(
|
| 173 |
result_image,
|
| 174 |
filename=f"detected_defect_{record_id}.jpg",
|
| 175 |
record_id=record_id
|
| 176 |
)
|
| 177 |
|
| 178 |
+
pdf_bytes = create_pdf_report(output)
|
| 179 |
+
content_version_id_pdf = upload_pdf_to_salesforce(
|
| 180 |
+
pdf_bytes,
|
| 181 |
+
filename=f"defect_report_{record_id}.pdf",
|
| 182 |
+
record_id=record_id
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
update_data = {}
|
| 186 |
+
if content_version_id_img:
|
| 187 |
+
update_data["Annotated_Image_URL__c"] = f"/sfc/servlet.shepherd/version/download/{content_version_id_img}"
|
| 188 |
+
if content_version_id_pdf:
|
| 189 |
+
update_data["Report_PDF__c"] = f"/sfc/servlet.shepherd/version/download/{content_version_id_pdf}"
|
| 190 |
+
|
| 191 |
+
if update_data:
|
| 192 |
+
sf.Drone_Structure_Inspection__c.update(record_id, update_data)
|
| 193 |
|
| 194 |
output.append({"salesforce_record_id": record_id})
|
| 195 |
+
|
| 196 |
except Exception as e:
|
| 197 |
output.append({"error": f"Failed to create Salesforce record: {str(e)}"})
|
| 198 |
|
|
|
|
| 204 |
logging.error(f"Detection failed: {str(e)}")
|
| 205 |
return None, f"Detection failed: {str(e)}"
|
| 206 |
|
|
|
|
| 207 |
demo = gr.Interface(
|
| 208 |
fn=detect_defects,
|
| 209 |
inputs=gr.Image(type="pil", label="Upload Drone Image"),
|