bacngv commited on
Commit
adb7bb3
·
verified ·
1 Parent(s): f0d9452

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -1,15 +1,13 @@
1
  import gradio as gr
2
  import torch
3
- import torchvision.transforms.functional as F
4
- from torchvision.models.detection import fasterrcnn_resnet50_fpn
5
  import numpy as np
 
6
  from PIL import Image, ImageDraw
7
  import albumentations as A
8
  from albumentations.pytorch import ToTensorV2
9
- from torchvision import datasets, models
10
 
11
  # Initialize Faster R-CNN model
12
- model = models.detection.fasterrcnn_resnet50_fpn_v2(pretrained=True)
13
 
14
  # Load the saved state_dict
15
  state_dict = torch.load("faster-rcnn.pth", map_location=torch.device('cpu'))
@@ -36,11 +34,15 @@ def draw_boxes_with_labels(image, boxes, labels):
36
 
37
  for box, label in zip(boxes, labels):
38
  x1, y1, x2, y2 = box
 
 
 
 
39
  color = COLOR_MAP[label]
40
  draw.rectangle([x1, y1, x2, y2], outline=color, width=4)
41
 
42
  text_position = (x1, y1 - 10)
43
-
44
  # Draw label background
45
  text_bbox = draw.textbbox(text_position, label)
46
  draw.rectangle(text_bbox, fill=color)
@@ -92,9 +94,6 @@ def predict_image(image):
92
  # Convert the original image back to PIL format
93
  pil_image = Image.fromarray(image)
94
 
95
- # Scale image_tensor back to uint8 format
96
- image_uint8 = (image_tensor * 255).byte().cpu()
97
-
98
  # Draw the boxes and labels on the image
99
  drawn_image = draw_boxes_with_labels(pil_image, boxes.cpu(), label_names)
100
 
 
1
  import gradio as gr
2
  import torch
 
 
3
  import numpy as np
4
+ from torchvision.models.detection import fasterrcnn_resnet50_fpn_v2
5
  from PIL import Image, ImageDraw
6
  import albumentations as A
7
  from albumentations.pytorch import ToTensorV2
 
8
 
9
  # Initialize Faster R-CNN model
10
+ model = fasterrcnn_resnet50_fpn_v2(pretrained=True)
11
 
12
  # Load the saved state_dict
13
  state_dict = torch.load("faster-rcnn.pth", map_location=torch.device('cpu'))
 
34
 
35
  for box, label in zip(boxes, labels):
36
  x1, y1, x2, y2 = box
37
+
38
+ # Correct the box coordinates if they are not integers
39
+ x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
40
+
41
  color = COLOR_MAP[label]
42
  draw.rectangle([x1, y1, x2, y2], outline=color, width=4)
43
 
44
  text_position = (x1, y1 - 10)
45
+
46
  # Draw label background
47
  text_bbox = draw.textbbox(text_position, label)
48
  draw.rectangle(text_bbox, fill=color)
 
94
  # Convert the original image back to PIL format
95
  pil_image = Image.fromarray(image)
96
 
 
 
 
97
  # Draw the boxes and labels on the image
98
  drawn_image = draw_boxes_with_labels(pil_image, boxes.cpu(), label_names)
99