lokesh341 commited on
Commit
13105d4
·
1 Parent(s): dd4bee9

Update services/crack_detection_service.py

Browse files
Files changed (1) hide show
  1. services/crack_detection_service.py +56 -43
services/crack_detection_service.py CHANGED
@@ -1,55 +1,68 @@
1
- import importlib.util
2
- import sys
3
- from transformers import DetrImageProcessor, DetrForObjectDetection
4
- import torch
5
- from PIL import Image
6
  import cv2
7
- import random
8
 
9
- # Ensure timm is installed
10
- if not importlib.util.find_spec("timm"):
11
  try:
12
- import pip
13
- pip.main(['install', 'timm==0.9.2'])
14
- except Exception as e:
15
- raise RuntimeError("Failed to install timm. Please ensure it is included in requirements.txt and restart the runtime.")
16
 
17
- # Load model with error handling
18
- try:
19
- processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", force_download=True)
20
- model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", force_download=True)
21
- except Exception as e:
22
- raise RuntimeError(f"Failed to load DETR model: {str(e)}")
23
 
24
- def detect_cracks_and_objects(frame):
25
- try:
26
- image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
27
- inputs = processor(images=image, return_tensors="pt")
28
- outputs = model(**inputs)
29
 
30
- target_sizes = torch.tensor([image.size[::-1]])
31
- results = processor.post_process_object_detection(outputs, threshold=0.9, target_sizes=target_sizes)[0]
32
 
33
  items = []
34
- for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
35
- if score >= 0.9:
36
- box_area = (box[2] - box[0]) * (box[3] - box[1])
37
- label_str = model.config.id2label[label.item()]
38
- if "crack" in label_str.lower() or random.random() > 0.3: # Simulate crack detection
39
- severity = "Large" if box_area > 1000 else "Small"
40
- items.append({
41
- 'type': 'crack',
42
- 'box': box.tolist(),
43
- 'severity': severity,
44
- 'confidence': score.item()
45
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  else:
47
- items.append({
48
- 'type': 'object',
49
- 'box': box.tolist(),
50
- 'label': label_str,
51
- 'confidence': score.item()
52
- })
 
 
 
 
53
 
54
  return items
55
  except Exception as e:
 
 
 
 
 
 
1
  import cv2
2
+ import numpy as np
3
 
4
+ def detect_cracks_and_holes(frame):
 
5
  try:
6
+ # Convert frame to grayscale
7
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
8
+ blurred = cv2.GaussianBlur(gray, (5, 5), 0)
 
9
 
10
+ # Edge detection using Canny
11
+ edges = cv2.Canny(blurred, 50, 150)
 
 
 
 
12
 
13
+ # Morphological operations to enhance cracks and holes
14
+ kernel = np.ones((3, 3), np.uint8)
15
+ dilated = cv2.dilate(edges, kernel, iterations=1)
16
+ eroded = cv2.erode(dilated, kernel, iterations=1)
 
17
 
18
+ # Find contours
19
+ contours, _ = cv2.findContours(eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
20
 
21
  items = []
22
+ for contour in contours:
23
+ # Filter small contours (noise)
24
+ area = cv2.contourArea(contour)
25
+ if area < 50:
26
+ continue
27
+
28
+ # Get bounding box
29
+ x, y, w, h = cv2.boundingRect(contour)
30
+ area = w * h
31
+
32
+ # Calculate aspect ratio and circularity
33
+ aspect_ratio = float(w) / h if h > 0 else 0
34
+ perimeter = cv2.arcLength(contour, True)
35
+ circularity = 4 * np.pi * area / (perimeter * perimeter) if perimeter > 0 else 0
36
+
37
+ # Classify as crack or hole
38
+ if 0.1 < aspect_ratio < 10 and circularity < 0.5: # Long, thin shapes are cracks
39
+ item_type = 'crack'
40
+ # Check if it's an underlying crack (longer and thinner)
41
+ if aspect_ratio > 5:
42
+ severity = 'Underlying'
43
+ elif area > 5000:
44
+ severity = 'Severe'
45
+ elif area > 1000:
46
+ severity = 'Moderate'
47
+ else:
48
+ severity = 'Minor'
49
+ elif circularity > 0.7: # Circular shapes are holes
50
+ item_type = 'hole'
51
+ if area > 5000:
52
+ severity = 'Severe'
53
+ elif area > 1000:
54
+ severity = 'Moderate'
55
  else:
56
+ severity = 'Minor'
57
+ else:
58
+ continue # Skip objects that don't match crack or hole criteria
59
+
60
+ items.append({
61
+ 'type': item_type,
62
+ 'box': [x, y, x + w, y + h],
63
+ 'severity': severity,
64
+ 'confidence': 0.95 # Simulated confidence
65
+ })
66
 
67
  return items
68
  except Exception as e: