sabrinabenas commited on
Commit
3c2de81
β€’
1 Parent(s): 6376064

add crop in orig img

Browse files
Files changed (2) hide show
  1. app.py +23 -16
  2. model/models.py +1 -1
app.py CHANGED
@@ -112,22 +112,25 @@ def predict_md(im,
112
 
113
  ## detect objects
114
  results = MD_model(im) # inference # vars(results).keys()= dict_keys(['imgs', 'pred', 'names', 'files', 'times', 'xyxy', 'xywh', 'xyxyn', 'xywhn', 'n', 't', 's'])
115
- results.render() # updates results.imgs with boxes and labels
116
-
117
  return results
118
 
119
  ##########################################
120
- def crop_animal_detections(yolo_results,
 
121
  likelihood_th):
122
 
123
  ## Extract animal crops
124
  #print(yolo_results)
125
  list_labels_as_str = [i for i in yolo_results.names.values()] # ['animal', 'person', 'vehicle']
126
  list_np_animal_crops = []
127
- # for every image
128
- #pdb.set_trace()
129
- for img, det_array in zip(yolo_results.ims,
130
- yolo_results.xyxy):
 
 
131
 
132
  # for every detection
133
  for j in range(det_array.shape[0]):
@@ -141,14 +144,15 @@ def crop_animal_detections(yolo_results,
141
 
142
  pred_llk = det_array[j,4]
143
  pred_label = det_array[j,5]
144
-
145
  # keep animal crops above threshold
146
  #pdb.set_trace()
147
  if (pred_label == list_labels_as_str.index('animal')) and \
148
  (pred_llk >= likelihood_th):
149
  area = (xmin_rd, ymin_rd, xmax_rd, ymax_rd)
150
 
151
- crop = Image.fromarray(img).crop(area)
 
152
  crop_np = np.asarray(crop)
153
 
154
  # add to list
@@ -199,8 +203,8 @@ def predict_pipeline(img_input,
199
  ## Get DLC model and labels as strings
200
  # TODO: make a dict as for megadetector
201
  # pdb.set_trace()
202
- path_to_DLCmodel = DownloadModel(dlc_model_input_str, '/home/sabrina/MegaDetector_DeepLabCut/model/')
203
- pose_cfg_path = '/home/sabrina/MegaDetector_DeepLabCut/model/pose_cfg.yaml'
204
  #pdb.set_trece()
205
  # extract map label ids to strings
206
  # pose_cfg_dict['all_joints'] is a list of one-element lists,
@@ -218,7 +222,8 @@ def predict_pipeline(img_input,
218
  ################################################################
219
  # Obtain animal crops for bboxes with confidence above th
220
 
221
- list_crops = crop_animal_detections(md_results,
 
222
  bbox_likelihood_th)
223
 
224
  ##############################################################
@@ -250,13 +255,15 @@ def predict_pipeline(img_input,
250
  kpts_likelihood_th,
251
  path_to_DLCmodel,
252
  dlc_proc)
253
- print(list_kpts_per_crop)
254
- print(list_crops)
255
  # Produce final image
256
- img_background = Image.fromarray(md_results.ims[0]) # img_input or Image.fromarray(md_results.imgs[0])?
257
  # Image.fromarray(md_results.imgs[0]) --> (640, 479)
258
  # img_input.size ---> (259, 194)
259
- # pdb.set_trace()
 
 
 
260
 
261
  # resize image to match megadetector output
262
  # g = (640 / max(img_background.size)) # gain
 
112
 
113
  ## detect objects
114
  results = MD_model(im) # inference # vars(results).keys()= dict_keys(['imgs', 'pred', 'names', 'files', 'times', 'xyxy', 'xywh', 'xyxyn', 'xywhn', 'n', 't', 's'])
115
+ # results.render() # updates results.imgs with boxes and labels
116
+ #pdb.set_trace()
117
  return results
118
 
119
  ##########################################
120
+ def crop_animal_detections(img_in,
121
+ yolo_results,
122
  likelihood_th):
123
 
124
  ## Extract animal crops
125
  #print(yolo_results)
126
  list_labels_as_str = [i for i in yolo_results.names.values()] # ['animal', 'person', 'vehicle']
127
  list_np_animal_crops = []
128
+
129
+ # image to crop (scale as input for megadetector)
130
+ img_in = img_in.resize((yolo_results.ims[0].shape[1],
131
+ yolo_results.ims[0].shape[0]))
132
+ # for every detection in the img
133
+ for det_array in yolo_results.xyxy:
134
 
135
  # for every detection
136
  for j in range(det_array.shape[0]):
 
144
 
145
  pred_llk = det_array[j,4]
146
  pred_label = det_array[j,5]
147
+ #pdb.set_trace()
148
  # keep animal crops above threshold
149
  #pdb.set_trace()
150
  if (pred_label == list_labels_as_str.index('animal')) and \
151
  (pred_llk >= likelihood_th):
152
  area = (xmin_rd, ymin_rd, xmax_rd, ymax_rd)
153
 
154
+ #pdb.set_trace()
155
+ crop = img_in.crop(area) #Image.fromarray(img_in).crop(area)
156
  crop_np = np.asarray(crop)
157
 
158
  # add to list
 
203
  ## Get DLC model and labels as strings
204
  # TODO: make a dict as for megadetector
205
  # pdb.set_trace()
206
+ path_to_DLCmodel = DownloadModel(dlc_model_input_str, 'model/')
207
+ pose_cfg_path = 'model/pose_cfg.yaml'
208
  #pdb.set_trece()
209
  # extract map label ids to strings
210
  # pose_cfg_dict['all_joints'] is a list of one-element lists,
 
222
  ################################################################
223
  # Obtain animal crops for bboxes with confidence above th
224
 
225
+ list_crops = crop_animal_detections(img_input,
226
+ md_results,
227
  bbox_likelihood_th)
228
 
229
  ##############################################################
 
255
  kpts_likelihood_th,
256
  path_to_DLCmodel,
257
  dlc_proc)
258
+
 
259
  # Produce final image
260
+ # img_background = Image.fromarray(md_results.ims[0]) # img_input or Image.fromarray(md_results.imgs[0])?
261
  # Image.fromarray(md_results.imgs[0]) --> (640, 479)
262
  # img_input.size ---> (259, 194)
263
+
264
+ img_background = img_input.resize((md_results.ims[0].shape[1],
265
+ md_results.ims[0].shape[0]))
266
+ #pdb.set_trace()
267
 
268
  # resize image to match megadetector output
269
  # g = (640 / max(img_background.size)) # gain
model/models.py CHANGED
@@ -33,7 +33,7 @@ def DownloadModel(modelname, target_dir):
33
  member.path = member.path[l:]
34
  yield member
35
 
36
- neturls = read_plainconfig("/home/sabrina/MegaDetector_DeepLabCut/model/pretrained_model_urls.yaml") #FIXME
37
 
38
  if modelname in neturls.keys():
39
  url = neturls[modelname]
 
33
  member.path = member.path[l:]
34
  yield member
35
 
36
+ neturls = read_plainconfig("model/pretrained_model_urls.yaml") #FIXME
37
 
38
  if modelname in neturls.keys():
39
  url = neturls[modelname]