ThRi commited on
Commit
ac2aecb
·
1 Parent(s): 39a2576

fix categories

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -31,7 +31,6 @@ from detectron2.checkpoint import DetectionCheckpointer
31
  from demo.demo import VisualizationDemo, get_parser
32
  import numpy as np
33
  from detectron2.data.detection_utils import read_image
34
- from detrex.data.datasets.register_ade20k_full import _get_ade20k_full_meta
35
  import matplotlib.pyplot as plt
36
  import cv2
37
 
@@ -40,6 +39,7 @@ warnings.filterwarnings("ignore")
40
 
41
  config_file = os.getcwd() + '/projects/dino/configs/odor3_fn_l_lrf_384_fl4_5scale_50ep.py'
42
  ckpt_pth = os.getcwd() + '/utils/focaldino_ep18.pth'
 
43
 
44
  try:
45
  cfg = LazyConfig.load(config_file)
@@ -60,30 +60,25 @@ demo = VisualizationDemo(
60
  img_format='RGB',
61
  metadata_dataset='odor_test')
62
 
 
 
 
 
 
 
 
 
63
  def treat_grayscale(img):
64
  if len(img.shape) == 2:
65
  return np.stack((img,)*3, axis=-1)
66
  else:
67
  return img
68
 
69
- def get_name_by_id(contiguous_id, ade20k_meta):
70
- stuff_dataset_id_to_contiguous_id = ade20k_meta["stuff_dataset_id_to_contiguous_id"]
71
- stuff_classes = ade20k_meta["stuff_classes"]
72
- dataset_id = -1
73
- print("stuff:")
74
- print(stuff_classes)
75
- print("con id:")
76
- print(contiguous_id)
77
- if contiguous_id in stuff_dataset_id_to_contiguous_id:
78
- dataset_id = stuff_dataset_id_to_contiguous_id[contiguous_id]
79
- print("data id:")
80
- print(dataset_id)
81
-
82
- if 0 <= dataset_id < len(stuff_classes):
83
- class_name = stuff_classes[dataset_id]
84
- return class_name
85
-
86
- return "Unknown"
87
 
88
  def predict(link, text, threshold):
89
  if(link):
@@ -102,15 +97,15 @@ def predict(link, text, threshold):
102
 
103
  output_text = ""
104
  for i in range(len(pred_boxes)):
105
- continuous_id = pred_classes[i].item()
106
- class_name = get_name_by_id(continuous_id, _get_ade20k_full_meta())
107
  score = scores[i].item()
108
  output_text += f"{class_name}: {score:.4f}\n"
109
 
110
  output_json = []
111
  for i in range(len(pred_boxes)):
112
- continuous_id = pred_classes[i].item()
113
- class_name = get_name_by_id(continuous_id, _get_ade20k_full_meta())
114
  score = scores[i].item()
115
  box_coords = pred_boxes[i].tensor.tolist()
116
  output_json.append({
@@ -119,7 +114,6 @@ def predict(link, text, threshold):
119
  "box_coordinates": box_coords
120
  })
121
  output_json = json.dumps(output_json, indent=4)
122
- output_json = ""
123
 
124
  return visualized_output.get_image(), output_text, output_json
125
 
 
31
  from demo.demo import VisualizationDemo, get_parser
32
  import numpy as np
33
  from detectron2.data.detection_utils import read_image
 
34
  import matplotlib.pyplot as plt
35
  import cv2
36
 
 
39
 
40
  config_file = os.getcwd() + '/projects/dino/configs/odor3_fn_l_lrf_384_fl4_5scale_50ep.py'
41
  ckpt_pth = os.getcwd() + '/utils/focaldino_ep18.pth'
42
+ categories = read_json_categories(os.getcwd() + '/annotations/instances_train2017.json')
43
 
44
  try:
45
  cfg = LazyConfig.load(config_file)
 
60
  img_format='RGB',
61
  metadata_dataset='odor_test')
62
 
63
+ def read_json_categories(jsonFile):
64
+ categories_dict = {}
65
+ with open(jsonFile, 'r') as file:
66
+ data = json.load(file)
67
+ if 'categories' in data:
68
+ categories_dict = data['categories']
69
+ return categories_dict
70
+
71
  def treat_grayscale(img):
72
  if len(img.shape) == 2:
73
  return np.stack((img,)*3, axis=-1)
74
  else:
75
  return img
76
 
77
+ def get_name_by_id(categories, id):
78
+ for cg in categories:
79
+ if cg['id'] == id:
80
+ return cg['name']
81
+ return 'Unknown'
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  def predict(link, text, threshold):
84
  if(link):
 
97
 
98
  output_text = ""
99
  for i in range(len(pred_boxes)):
100
+ id = pred_classes[i].item()
101
+ class_name = get_name_by_id(categories, id)
102
  score = scores[i].item()
103
  output_text += f"{class_name}: {score:.4f}\n"
104
 
105
  output_json = []
106
  for i in range(len(pred_boxes)):
107
+ id = pred_classes[i].item()
108
+ class_name = get_name_by_id(categories, id)
109
  score = scores[i].item()
110
  box_coords = pred_boxes[i].tensor.tolist()
111
  output_json.append({
 
114
  "box_coordinates": box_coords
115
  })
116
  output_json = json.dumps(output_json, indent=4)
 
117
 
118
  return visualized_output.get_image(), output_text, output_json
119