Komal133 commited on
Commit
2c1d06f
·
verified ·
1 Parent(s): 6ce3beb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -32
app.py CHANGED
@@ -33,25 +33,10 @@ except Exception as e:
33
  logging.error(f"Failed to connect to Salesforce: {str(e)}")
34
  raise Exception(f"Failed to connect to Salesforce: {str(e)}")
35
 
36
- # Load the Faster R-CNN pretrained model (replace with your fine-tuned weights if any)
37
  model = models.detection.fasterrcnn_resnet50_fpn(weights="FasterRCNN_ResNet50_FPN_Weights.COCO_V1")
38
  model.eval()
39
 
40
- # COCO categories list for mapping labels (standard)
41
- COCO_INSTANCE_CATEGORY_NAMES = [
42
- '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
43
- 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter',
44
- 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra',
45
- 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis',
46
- 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
47
- 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon',
48
- 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
49
- 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet',
50
- 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven',
51
- 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
52
- 'hair drier', 'toothbrush'
53
- ]
54
-
55
  # Image transformation for the model input
56
  transform = transforms.Compose([
57
  transforms.ToTensor(),
@@ -66,17 +51,10 @@ def get_severity(score):
66
  else:
67
  return "Minor"
68
 
69
- # Temporary mapping from COCO labels to defect types (replace with your own)
70
- COCO_TO_DEFECT_MAPPING = {
71
- 'car': 'Crack',
72
- 'person': 'Rust',
73
- 'bicycle': 'Deformation',
74
- 'truck': 'Corrosion',
75
- 'boat': 'Spalling',
76
- }
77
-
78
- def map_defect_type(coco_label):
79
- return COCO_TO_DEFECT_MAPPING.get(coco_label, "Crack")
80
 
81
  # Upload annotated image to Salesforce as ContentVersion record
82
  def upload_image_to_salesforce(image, filename="detected_image.jpg", record_id=None):
@@ -113,7 +91,7 @@ def detect_defects(image):
113
  result_image = image.copy()
114
  draw = ImageDraw.Draw(result_image)
115
 
116
- # Optional: Use a truetype font for nicer text, fallback if not available
117
  try:
118
  font = ImageFont.truetype("arial.ttf", 18)
119
  except:
@@ -126,10 +104,8 @@ def detect_defects(image):
126
  continue
127
 
128
  box = predictions[0]['boxes'][i].tolist()
129
- label_idx = predictions[0]['labels'][i].item()
130
- coco_label = COCO_INSTANCE_CATEGORY_NAMES[label_idx]
131
 
132
- defect_type = map_defect_type(coco_label)
133
  severity = get_severity(score)
134
 
135
  # Append defect info to output list
@@ -137,7 +113,6 @@ def detect_defects(image):
137
  "type": defect_type,
138
  "confidence": round(score, 2),
139
  "severity": severity,
140
- "coco_label": coco_label
141
  })
142
 
143
  # Draw rectangle and label
 
33
  logging.error(f"Failed to connect to Salesforce: {str(e)}")
34
  raise Exception(f"Failed to connect to Salesforce: {str(e)}")
35
 
36
+ # Load the Faster R-CNN pretrained model
37
  model = models.detection.fasterrcnn_resnet50_fpn(weights="FasterRCNN_ResNet50_FPN_Weights.COCO_V1")
38
  model.eval()
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  # Image transformation for the model input
41
  transform = transforms.Compose([
42
  transforms.ToTensor(),
 
51
  else:
52
  return "Minor"
53
 
54
+ # Simple defect type mapping (no COCO labels)
55
+ # Here, just return a generic "Defect" label or you can customize per your need
56
+ def map_defect_type():
57
+ return "Defect"
 
 
 
 
 
 
 
58
 
59
  # Upload annotated image to Salesforce as ContentVersion record
60
  def upload_image_to_salesforce(image, filename="detected_image.jpg", record_id=None):
 
91
  result_image = image.copy()
92
  draw = ImageDraw.Draw(result_image)
93
 
94
+ # Use default font
95
  try:
96
  font = ImageFont.truetype("arial.ttf", 18)
97
  except:
 
104
  continue
105
 
106
  box = predictions[0]['boxes'][i].tolist()
 
 
107
 
108
+ defect_type = map_defect_type()
109
  severity = get_severity(score)
110
 
111
  # Append defect info to output list
 
113
  "type": defect_type,
114
  "confidence": round(score, 2),
115
  "severity": severity,
 
116
  })
117
 
118
  # Draw rectangle and label