Aastha commited on
Commit
94cd336
β€’
1 Parent(s): 4a692a0

Add application file

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from efficientnet_pytorch import EfficientNet
3
+ from torchvision import transforms
4
+ from PIL import Image
5
+ import gradio as gr
6
+ from super_gradients.training import models
7
+ import cv2
8
+ import numpy as np
9
+
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+
12
+ # Load the YOLO-NAS model
13
+ yolo_nas_l = models.get("yolo_nas_l", pretrained_weights="coco")
14
+
15
+ def bounding_boxes_overlap(box1, box2):
16
+ """Check if two bounding boxes overlap or touch."""
17
+ x1, y1, x2, y2 = box1
18
+ x3, y3, x4, y4 = box2
19
+ return not (x3 > x2 or x4 < x1 or y3 > y2 or y4 < y1)
20
+
21
+ def merge_boxes(box1, box2):
22
+ """Return the encompassing bounding box of two boxes."""
23
+ x1, y1, x2, y2 = box1
24
+ x3, y3, x4, y4 = box2
25
+ x = min(x1, x3)
26
+ y = min(y1, y3)
27
+ w = max(x2, x4)
28
+ h = max(y2, y4)
29
+ return (x, y, w, h)
30
+
31
+ def save_merged_boxes(predictions, image_np):
32
+ """Save merged bounding boxes as separate images."""
33
+ processed_boxes = set()
34
+ roi = None # Initialize roi to None
35
+
36
+ for image_prediction in predictions:
37
+ bboxes = image_prediction.prediction.bboxes_xyxy
38
+ for box1 in bboxes:
39
+ for box2 in bboxes:
40
+ if np.array_equal(box1, box2):
41
+ continue
42
+ if bounding_boxes_overlap(box1, box2) and tuple(box1) not in processed_boxes and tuple(box2) not in processed_boxes:
43
+ merged_box = merge_boxes(box1, box2)
44
+ roi = image_np[int(merged_box[1]):int(merged_box[3]), int(merged_box[0]):int(merged_box[2])]
45
+ processed_boxes.add(tuple(box1))
46
+ processed_boxes.add(tuple(box2))
47
+ break # Exit the inner loop once a match is found
48
+ if roi is not None:
49
+ break # Exit the outer loop once a match is found
50
+
51
+ return roi
52
+
53
+ # Load the EfficientNet model
54
+ def load_model(model_path):
55
+ model = torch.load(model_path)
56
+ model = model.to(device)
57
+ model.eval() # Set the model to evaluation mode
58
+ return model
59
+
60
+ # Perform inference on an image
61
+ def predict_image(image, model):
62
+ # First, get the ROI using YOLO-NAS
63
+ image_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
64
+ predictions = yolo_nas_l.predict(image_np, iou=0.3, conf=0.35)
65
+ roi_new = save_merged_boxes(predictions, image_np)
66
+
67
+ if roi_new is None:
68
+ roi_new = image_np # Use the original image if no ROI is found
69
+
70
+ # Convert ROI back to PIL Image for EfficientNet
71
+ roi_image = Image.fromarray(cv2.cvtColor(roi_new, cv2.COLOR_BGR2RGB))
72
+
73
+ # Define the image transformations
74
+ transform = transforms.Compose([
75
+ transforms.Resize(256),
76
+ transforms.CenterCrop(224),
77
+ transforms.ToTensor(),
78
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
79
+ ])
80
+
81
+ # Convert PIL Image to Tensor
82
+ roi_image_tensor = transform(roi_image).unsqueeze(0).to(device)
83
+
84
+ with torch.no_grad():
85
+ outputs = model(roi_image_tensor)
86
+ _, predicted = outputs.max(1)
87
+ prediction_text = 'Accident' if predicted.item() == 0 else 'No accident'
88
+
89
+ return roi_image, prediction_text # Return both the roi_image and the prediction text
90
+
91
+ # Load the EfficientNet model outside the function to avoid loading it multiple times
92
+ model_path = 'vehicle.pt'
93
+ model = load_model(model_path)
94
+
95
+ # Gradio UI
96
+ title = "Vehicle Collision Classification"
97
+ description = "Upload an image to determine if it depicts a vehicle accident. Powered by EfficientNet."
98
+ examples = [["roi_none.png"], ["test2.jpeg"]] # Replace with your example image path
99
+
100
+ gr.Interface(fn=lambda img: predict_image(img, model),
101
+ inputs=gr.inputs.Image(type="pil"),
102
+ outputs=[gr.outputs.Image(type="pil"), "text"], # Updated outputs to handle both image and text
103
+ title=title,
104
+ description=description,
105
+ examples=examples).launch()