Spaces:
Runtime error
Runtime error
sfmig
commited on
Commit
·
916c574
1
Parent(s):
f1872fb
adding sliders
Browse files
app.py
CHANGED
@@ -60,7 +60,8 @@ def predict_md(im, size=640):
|
|
60 |
|
61 |
return results #Image.fromarray(results.imgs[0]) ---return animals only?
|
62 |
|
63 |
-
def crop_animal_detections(yolo_results,
|
|
|
64 |
## crop if animal and return list of crops
|
65 |
|
66 |
list_labels_as_str = yolo_results.names #['animal', 'person', 'vehicle']
|
@@ -109,44 +110,54 @@ def crop_animal_detections(yolo_results, likelihood_th):
|
|
109 |
# if detections_dict["category"] == "1":
|
110 |
return list_np_animal_crops
|
111 |
|
112 |
-
def predict_dlc(list_np_crops,
|
|
|
|
|
|
|
113 |
# run dlc thru list of crops
|
114 |
dlc_live = DLCLive(DLCmodel, processor=dlc_proc)
|
115 |
dlc_live.init_inference(list_np_crops[0])
|
116 |
|
117 |
list_kpts_per_crop = []
|
|
|
118 |
for crop in list_np_crops:
|
119 |
-
# scale crop?
|
120 |
-
|
121 |
-
|
|
|
|
|
122 |
|
123 |
-
|
124 |
|
125 |
|
126 |
|
127 |
-
def predict_pipeline(img_input
|
|
|
|
|
128 |
|
129 |
-
# these eventually user inputs....
|
130 |
path_to_DLCmodel = "DLC_models/DLC_Cat_resnet_50_iteration-0_shuffle-0"
|
131 |
-
likelihood_th = 0.8
|
132 |
|
133 |
-
|
134 |
md_results = predict_md(img_input) #Image.fromarray(results.imgs[0])
|
135 |
|
136 |
# Obtain animal crops with confidence above th
|
137 |
list_crops = crop_animal_detections(md_results,
|
138 |
-
|
139 |
|
140 |
# Run DLC
|
141 |
# TODO: add llk threshold for kpts too?
|
142 |
dlc_proc = Processor()
|
143 |
list_kpts_per_crop = predict_dlc(list_crops,
|
|
|
144 |
path_to_DLCmodel,
|
145 |
dlc_proc)
|
146 |
|
147 |
|
148 |
# # Produce final image
|
149 |
# fig = plt.Figure(md_results.imgs[0].shape[:2]) #figsize=(10,10)) #md_results.imgs[0].shape)
|
|
|
|
|
150 |
for ic, (np_crop, kpts_crop) in enumerate(zip(list_crops,
|
151 |
list_kpts_per_crop)):
|
152 |
|
@@ -158,18 +169,15 @@ def predict_pipeline(img_input):
|
|
158 |
radius=2,
|
159 |
use_normalized_coordinates=False) # if True, then I should use md_results.xyxyn
|
160 |
|
|
|
|
|
|
|
161 |
## Paste crop in original image
|
162 |
# https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.paste
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
# plt.scatter(kpts_crop[:,0], kpts_crop[:,1], 40,
|
168 |
-
# color='r')
|
169 |
-
# img_overlay = Image.frombytes('RGB',
|
170 |
-
# fig.canvas.get_width_height(),
|
171 |
-
# fig.canvas.tostring_rgb())
|
172 |
-
return img_input #Image.fromarray(list_crops[0]) #Image.fromarray(md_results.imgs[0]) #
|
173 |
|
174 |
|
175 |
##########################################################
|
@@ -179,26 +187,28 @@ def predict_pipeline(img_input):
|
|
179 |
MD_model = torch.hub.load('ultralytics/yolov5', 'custom', "model_weights/md_v5a.0.0.pt")
|
180 |
|
181 |
|
182 |
-
|
183 |
####################################################
|
184 |
# Create user interface and launch
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
188 |
#image = gr.inputs.Image(type="pil", label="Input Image")
|
189 |
#chosen_model = gr.inputs.Dropdown(choices = models, value = "model_weights/md_v5a.0.0.pt",type = "value", label="Model Weight")
|
190 |
#size = 640
|
191 |
|
192 |
-
|
193 |
-
|
194 |
# article = "<p style='text-align: center'>This app makes predictions using a YOLOv5x6 model that was trained to detect animals, humans, and vehicles in camera trap images; find out more about the project on <a href='https://github.com/microsoft/CameraTraps'>GitHub</a>. This app was built by Henry Lydecker but really depends on code and models developed by <a href='http://ecologize.org/'>Ecologize</a> and <a href='http://aka.ms/aiforearth'>Microsoft AI for Earth</a>. Find out more about the YOLO model from the original creator, <a href='https://pjreddie.com/darknet/yolo/'>Joseph Redmon</a>. YOLOv5 is a family of compound-scaled object detection models trained on the COCO dataset and developed by Ultralytics, and includes simple functionality for Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, and export to ONNX, CoreML and TFLite. <a href='https://github.com/ultralytics/yolov5'>Source code</a> | <a href='https://pytorch.org/hub/ultralytics_yolov5'>PyTorch Hub</a></p>"
|
195 |
# examples = [['data/Macropod.jpg'], ['data/koala2.jpg'],['data/cat.jpg'],['data/BrushtailPossum.jpg']]
|
196 |
|
197 |
gr.Interface(predict_pipeline,
|
198 |
-
inputs,
|
199 |
-
outputs,
|
200 |
-
title=
|
201 |
-
description=
|
202 |
theme="huggingface").launch(enable_queue=True)
|
203 |
|
204 |
|
|
|
60 |
|
61 |
return results #Image.fromarray(results.imgs[0]) ---return animals only?
|
62 |
|
63 |
+
def crop_animal_detections(yolo_results,
|
64 |
+
likelihood_th):
|
65 |
## crop if animal and return list of crops
|
66 |
|
67 |
list_labels_as_str = yolo_results.names #['animal', 'person', 'vehicle']
|
|
|
110 |
# if detections_dict["category"] == "1":
|
111 |
return list_np_animal_crops
|
112 |
|
113 |
+
def predict_dlc(list_np_crops,
|
114 |
+
kpts_likelihood_th,
|
115 |
+
DLCmodel, dlc_proc):
|
116 |
+
|
117 |
# run dlc thru list of crops
|
118 |
dlc_live = DLCLive(DLCmodel, processor=dlc_proc)
|
119 |
dlc_live.init_inference(list_np_crops[0])
|
120 |
|
121 |
list_kpts_per_crop = []
|
122 |
+
np_aux = np.empty((1,3)) # can I avoid hardcoding?
|
123 |
for crop in list_np_crops:
|
124 |
+
# scale crop here?
|
125 |
+
keypts_xyp = dlc_live.get_pose(crop) # third column is llk!
|
126 |
+
# set kpts below threhsold to nan
|
127 |
+
keypts_xyp[keypts_xyp[:,-1] < kpts_likelihood_th,:] = np_aux.fill(np.nan)
|
128 |
+
list_kpts_per_crop.append(keypts_xyp)
|
129 |
|
130 |
+
return list_kpts_per_crop
|
131 |
|
132 |
|
133 |
|
134 |
+
def predict_pipeline(img_input,
|
135 |
+
bbox_likelihood_th,
|
136 |
+
kpts_likelihood_th):
|
137 |
|
138 |
+
# these will eventually be user inputs....
|
139 |
path_to_DLCmodel = "DLC_models/DLC_Cat_resnet_50_iteration-0_shuffle-0"
|
|
|
140 |
|
141 |
+
### Run Megadetector
|
142 |
md_results = predict_md(img_input) #Image.fromarray(results.imgs[0])
|
143 |
|
144 |
# Obtain animal crops with confidence above th
|
145 |
list_crops = crop_animal_detections(md_results,
|
146 |
+
bbox_likelihood_th)
|
147 |
|
148 |
# Run DLC
|
149 |
# TODO: add llk threshold for kpts too?
|
150 |
dlc_proc = Processor()
|
151 |
list_kpts_per_crop = predict_dlc(list_crops,
|
152 |
+
kpts_likelihood_th,
|
153 |
path_to_DLCmodel,
|
154 |
dlc_proc)
|
155 |
|
156 |
|
157 |
# # Produce final image
|
158 |
# fig = plt.Figure(md_results.imgs[0].shape[:2]) #figsize=(10,10)) #md_results.imgs[0].shape)
|
159 |
+
# list_annotated_crops = []
|
160 |
+
img_background = Image.fromarray(md_results.imgs[0])
|
161 |
for ic, (np_crop, kpts_crop) in enumerate(zip(list_crops,
|
162 |
list_kpts_per_crop)):
|
163 |
|
|
|
169 |
radius=2,
|
170 |
use_normalized_coordinates=False) # if True, then I should use md_results.xyxyn
|
171 |
|
172 |
+
|
173 |
+
# list_annotated_crops.append(img_crop)
|
174 |
+
|
175 |
## Paste crop in original image
|
176 |
# https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.paste
|
177 |
+
img_background.paste(img_crop,
|
178 |
+
box = tuple([int(math.floor(t)) for t in md_results.xyxy[0][ic,:2]]))
|
179 |
+
|
180 |
+
return img_background #Image.fromarray(list_crops[0]) #Image.fromarray(md_results.imgs[0]) #list_annotated_crops #
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
|
182 |
|
183 |
##########################################################
|
|
|
187 |
MD_model = torch.hub.load('ultralytics/yolov5', 'custom', "model_weights/md_v5a.0.0.pt")
|
188 |
|
189 |
|
|
|
190 |
####################################################
|
191 |
# Create user interface and launch
|
192 |
+
gr_image_input = gr.inputs.Image(type="pil", label="Input Image")
|
193 |
+
gr_image_output = gr.outputs.Image(type="pil", label="Output Image")
|
194 |
+
gr_slider_conf_bboxes = gr.inputs.Slider(0,1,.05,0.8,
|
195 |
+
label='Set confidence threshold for animal detections')
|
196 |
+
gr_slider_conf_keypoints = gr.inputs.Slider(0,1,.05,0,
|
197 |
+
label='Set confidence threshold for keypoints')
|
198 |
#image = gr.inputs.Image(type="pil", label="Input Image")
|
199 |
#chosen_model = gr.inputs.Dropdown(choices = models, value = "model_weights/md_v5a.0.0.pt",type = "value", label="Model Weight")
|
200 |
#size = 640
|
201 |
|
202 |
+
gr_title = "MegaDetector v5 + DLClive"
|
203 |
+
gr_description = "Detect and estimate the pose of animals in camera trap images, using MegaDetector v5a + DeepLabCut-live"
|
204 |
# article = "<p style='text-align: center'>This app makes predictions using a YOLOv5x6 model that was trained to detect animals, humans, and vehicles in camera trap images; find out more about the project on <a href='https://github.com/microsoft/CameraTraps'>GitHub</a>. This app was built by Henry Lydecker but really depends on code and models developed by <a href='http://ecologize.org/'>Ecologize</a> and <a href='http://aka.ms/aiforearth'>Microsoft AI for Earth</a>. Find out more about the YOLO model from the original creator, <a href='https://pjreddie.com/darknet/yolo/'>Joseph Redmon</a>. YOLOv5 is a family of compound-scaled object detection models trained on the COCO dataset and developed by Ultralytics, and includes simple functionality for Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, and export to ONNX, CoreML and TFLite. <a href='https://github.com/ultralytics/yolov5'>Source code</a> | <a href='https://pytorch.org/hub/ultralytics_yolov5'>PyTorch Hub</a></p>"
|
205 |
# examples = [['data/Macropod.jpg'], ['data/koala2.jpg'],['data/cat.jpg'],['data/BrushtailPossum.jpg']]
|
206 |
|
207 |
gr.Interface(predict_pipeline,
|
208 |
+
inputs=[gr_image_input,gr_slider_conf_bboxes,gr_slider_conf_keypoints],
|
209 |
+
outputs=gr_image_output,
|
210 |
+
title=gr_title,
|
211 |
+
description=gr_description,
|
212 |
theme="huggingface").launch(enable_queue=True)
|
213 |
|
214 |
|