rohithb commited on
Commit
613cc5e
·
1 Parent(s): 367f9ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -17,7 +17,7 @@ from PIL import Image
17
 
18
  warnings.filterwarnings("ignore")
19
 
20
- def scale_image(input_image):
21
  original_shape = input_image.shape
22
  im = Image.fromarray(input_image)
23
  newsize = (config.IMAGE_SIZE, config.IMAGE_SIZE)
@@ -25,7 +25,7 @@ def scale_image(input_image):
25
  input_image_resized = np.array(im_resized)
26
  return input_image_resized, original_shape
27
 
28
- def convert_to_transformed_tensor(input_image):
29
  test_transforms = A.Compose(
30
  [
31
  #A.LongestMaxSize(max_size=config.IMAGE_SIZE),
@@ -45,7 +45,7 @@ def convert_to_transformed_tensor(input_image):
45
 
46
 
47
  def get_detection_output(input_image, cam, targets, conf_threshold, iou_threshold, renormalize_cam, image_weight):
48
- input_tensor = convert_to_transformed_tensor(input_image)
49
  boxes_list, classes_list, labels_list, confidences_list = predict_single_image(input_tensor,
50
  yolo_model_trained,
51
  scaled_anchors,
@@ -58,7 +58,7 @@ def get_detection_output(input_image, cam, targets, conf_threshold, iou_threshol
58
  print(confidences_list)
59
  grayscale_cam_input_image = cam(input_tensor, targets=targets)
60
 
61
- input_image_np, original_shape = scale_image(input_image)
62
  input_image_np = input_image_np/255.
63
  original_width, original_height, _ = original_shape
64
 
@@ -94,7 +94,7 @@ def get_detection_output(input_image, cam, targets, conf_threshold, iou_threshol
94
  return image_w_bb_resized, composite_img_w_bb_resized
95
 
96
 
97
- def get_detection_on_samples():
98
  """
99
  Run inference on one batch of test images
100
  """
@@ -134,7 +134,7 @@ def get_detection_on_samples():
134
  return sample_detections_list, sample_grad_cam_maps_list
135
 
136
 
137
- def yolo_v3_interface(input_image,
138
  conf_threshold=0.6,
139
  iou_threshold=0.3,
140
  renormalize_cam=False,
@@ -213,25 +213,21 @@ cam = EigenCAM(yolo_model_trained,
213
  target_layers,
214
  use_cuda=torch.cuda.is_available(),
215
  reshape_transform=None)
216
- sample_detections_list, sample_grad_cam_maps_list = get_detection_on_samples()
217
 
218
  # Define Interface
219
  description = 'Perform object detection using YOLOv3 algorithm, trained from scratch on PASCAL VOC dataset. Display the bounding boxes of the detected objects, and show the corresponding EigenCAM outputs'
220
  #title = 'Interface to perform Object detection using YOLOv3 algorithm'
221
  title = 'YOLOv3 object detection trained from scratch'
222
 
223
- yolo_examples = [['bicycle.webp', 0.6, 0.3, False, 0.8, True],
224
- ['bus.jpg', 0.6, 0.3, False, 0.8, True],
225
- ['dining_table.webp', 0.6, 0.3, False, 0.8, True],
226
- ['potted_plant.jpeg', 0.6, 0.3, False, 0.8, True],
227
- ['traffic.jpg', 0.6, 0.3, False, 0.8, True],
228
- ['boat.webp', 0.6, 0.3, False, 0.8, True],
229
- ['car.jpg', 0.6, 0.3, False, 0.8, True],
230
- ['motorcycle.jpg', 0.6, 0.3, False, 0.8, True],
231
- ['sheep_and_horse', 0.6, 0.3, False, 0.8, True],
232
- ['train.jpeg', 0.6, 0.3, False, 0.8, True]]
233
-
234
- demo = gr.Interface(yolo_v3_interface,
235
  inputs = [#gr.Image(shape=(config.IMAGE_SIZE, config.IMAGE_SIZE)),
236
  gr.Image(),
237
  gr.Slider(0,1.,0.6, label="Confidence Threshold"),
 
17
 
18
  warnings.filterwarnings("ignore")
19
 
20
+ def scale_input_image(input_image):
21
  original_shape = input_image.shape
22
  im = Image.fromarray(input_image)
23
  newsize = (config.IMAGE_SIZE, config.IMAGE_SIZE)
 
25
  input_image_resized = np.array(im_resized)
26
  return input_image_resized, original_shape
27
 
28
+ def get_transformed_image(input_image):
29
  test_transforms = A.Compose(
30
  [
31
  #A.LongestMaxSize(max_size=config.IMAGE_SIZE),
 
45
 
46
 
47
  def get_detection_output(input_image, cam, targets, conf_threshold, iou_threshold, renormalize_cam, image_weight):
48
+ input_tensor = get_transformed_image(input_image)
49
  boxes_list, classes_list, labels_list, confidences_list = predict_single_image(input_tensor,
50
  yolo_model_trained,
51
  scaled_anchors,
 
58
  print(confidences_list)
59
  grayscale_cam_input_image = cam(input_tensor, targets=targets)
60
 
61
+ input_image_np, original_shape = scale_input_image(input_image)
62
  input_image_np = input_image_np/255.
63
  original_width, original_height, _ = original_shape
64
 
 
94
  return image_w_bb_resized, composite_img_w_bb_resized
95
 
96
 
97
+ def detect_samples():
98
  """
99
  Run inference on one batch of test images
100
  """
 
134
  return sample_detections_list, sample_grad_cam_maps_list
135
 
136
 
137
+ def yolo_predictor(input_image,
138
  conf_threshold=0.6,
139
  iou_threshold=0.3,
140
  renormalize_cam=False,
 
213
  target_layers,
214
  use_cuda=torch.cuda.is_available(),
215
  reshape_transform=None)
216
+ sample_detections_list, sample_grad_cam_maps_list = detect_samples()
217
 
218
  # Define Interface
219
  description = 'Perform object detection using YOLOv3 algorithm, trained from scratch on PASCAL VOC dataset. Display the bounding boxes of the detected objects, and show the corresponding EigenCAM outputs'
220
  #title = 'Interface to perform Object detection using YOLOv3 algorithm'
221
  title = 'YOLOv3 object detection trained from scratch'
222
 
223
+ yolo_examples = [['images/cycle.jpg', 0.6, 0.3, False, 0.8, True],
224
+ ['images/bus.webp', 0.6, 0.3, False, 0.8, True],
225
+ ['images/train.jpg', 0.6, 0.3, False, 0.8, True],
226
+ ['images/motorcycle.jpg', 0.6, 0.3, False, 0.8, True],
227
+ ['images/boat.jpg', 0.6, 0.3, False, 0.8, True],
228
+ ['images/car.jpg', 0.6, 0.3, False, 0.8, True]]
229
+
230
+ demo = gr.Interface(yolo_predictor,
 
 
 
 
231
  inputs = [#gr.Image(shape=(config.IMAGE_SIZE, config.IMAGE_SIZE)),
232
  gr.Image(),
233
  gr.Slider(0,1.,0.6, label="Confidence Threshold"),