Spaces:
Build error
Build error
Commit
β’
66e5fd8
1
Parent(s):
567ff55
added megadet model & bodypart colour
Browse files
app.py
CHANGED
@@ -30,7 +30,9 @@ FONTS = {'amiko': "font/Amiko-Regular.ttf",
|
|
30 |
'animals': "font/UncialAnimals.ttf",
|
31 |
'zen': "font/ZEN.TTF"}
|
32 |
|
33 |
-
|
|
|
|
|
34 |
#############################################
|
35 |
# %%
|
36 |
# Create user interface and launch: all inputs
|
@@ -60,7 +62,10 @@ gr_slider_font_size = gr.inputs.Slider(5,30,1,8,
|
|
60 |
|
61 |
gr_slider_pose_size = gr.inputs.Slider(1,20,1,2,
|
62 |
label='Set pose size')
|
63 |
-
|
|
|
|
|
|
|
64 |
inputs = [gr_image_input,
|
65 |
gr_dlc_model_input,
|
66 |
gr_dlc_only_checkbox,
|
@@ -70,6 +75,7 @@ inputs = [gr_image_input,
|
|
70 |
gr_slider_font_size,
|
71 |
gr_keypt_color,
|
72 |
gr_slider_pose_size,
|
|
|
73 |
]
|
74 |
|
75 |
#image = gr.inputs.Image(type="pil", label="Input Image")
|
@@ -122,17 +128,17 @@ def draw_keypoints_on_image(image,
|
|
122 |
# draw.text((x, y),"Sample Text",(r,g,b))
|
123 |
draw.text((keypoint_x + gr_slider_pose_size, keypoint_y + gr_slider_pose_size),#(0.5*im_width, 0.5*im_height), #-------
|
124 |
map_label_id_to_str[i],#"Sample Text",
|
125 |
-
(
|
126 |
font=font)
|
127 |
|
128 |
############################################
|
129 |
# %%
|
130 |
# Predict detections with MegaDetector v5a model
|
131 |
-
def predict_md(im, size=640):
|
132 |
# resize image
|
133 |
g = (size / max(im.size)) # gain
|
134 |
im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize
|
135 |
-
|
136 |
## detect objects
|
137 |
results = MD_model(im) # inference # vars(results).keys()= dict_keys(['imgs', 'pred', 'names', 'files', 'times', 'xyxy', 'xywh', 'xyxyn', 'xywhn', 'n', 't', 's'])
|
138 |
results.render() # updates results.imgs with boxes and labels
|
@@ -222,6 +228,7 @@ def predict_pipeline(img_input,
|
|
222 |
gr_slider_font_size=gr_slider_font_size,
|
223 |
gr_keypt_color=gr_keypt_color,
|
224 |
gr_slider_pose_size=gr_slider_pose_size,
|
|
|
225 |
):
|
226 |
|
227 |
############################################################
|
@@ -251,7 +258,7 @@ def predict_pipeline(img_input,
|
|
251 |
|
252 |
############################################################
|
253 |
# ### Run Megadetector
|
254 |
-
md_results = predict_md(img_input) #Image.fromarray(results.imgs[0])
|
255 |
|
256 |
################################################################
|
257 |
# Obtain animal crops for bboxes with confidence above th
|
@@ -318,7 +325,7 @@ def predict_pipeline(img_input,
|
|
318 |
# Get MegaDetector model
|
319 |
# TODO: Allow user selectable model?
|
320 |
# models = ["model_weights/md_v5a.0.0.pt","model_weights/md_v5b.0.0.pt"]
|
321 |
-
MD_model = torch.hub.load('ultralytics/yolov5', 'custom', "megadet_model/md_v5b.0.0.pt")
|
322 |
|
323 |
|
324 |
|
|
|
30 |
'animals': "font/UncialAnimals.ttf",
|
31 |
'zen': "font/ZEN.TTF"}
|
32 |
|
33 |
+
Megadet_Models = {'md_v5a': "megadet_model/md_v5a.0.0.pt",
|
34 |
+
'md_v5b': "megadet_model/md_v5b.0.0.pt"}
|
35 |
+
|
36 |
#############################################
|
37 |
# %%
|
38 |
# Create user interface and launch: all inputs
|
|
|
62 |
|
63 |
gr_slider_pose_size = gr.inputs.Slider(1,20,1,2,
|
64 |
label='Set pose size')
|
65 |
+
gr_mega_model_input = gr.inputs.Dropdown(choices=['md_v5a','md_v5a'],
|
66 |
+
default='md_v5a', # default option
|
67 |
+
type='value', # Type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
|
68 |
+
label='Select Megadetector model')
|
69 |
inputs = [gr_image_input,
|
70 |
gr_dlc_model_input,
|
71 |
gr_dlc_only_checkbox,
|
|
|
75 |
gr_slider_font_size,
|
76 |
gr_keypt_color,
|
77 |
gr_slider_pose_size,
|
78 |
+
gr_mega_model_input,
|
79 |
]
|
80 |
|
81 |
#image = gr.inputs.Image(type="pil", label="Input Image")
|
|
|
128 |
# draw.text((x, y),"Sample Text",(r,g,b))
|
129 |
draw.text((keypoint_x + gr_slider_pose_size, keypoint_y + gr_slider_pose_size),#(0.5*im_width, 0.5*im_height), #-------
|
130 |
map_label_id_to_str[i],#"Sample Text",
|
131 |
+
(gr_keypt_color), # rgb
|
132 |
font=font)
|
133 |
|
134 |
############################################
|
135 |
# %%
|
136 |
# Predict detections with MegaDetector v5a model
|
137 |
+
def predict_md(im, size=640, gr_mega_model_input='md_v5a'):
|
138 |
# resize image
|
139 |
g = (size / max(im.size)) # gain
|
140 |
im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize
|
141 |
+
MD_model = torch.hub.load('ultralytics/yolov5', 'custom', Megadet_Models[gr_mega_model_input])
|
142 |
## detect objects
|
143 |
results = MD_model(im) # inference # vars(results).keys()= dict_keys(['imgs', 'pred', 'names', 'files', 'times', 'xyxy', 'xywh', 'xyxyn', 'xywhn', 'n', 't', 's'])
|
144 |
results.render() # updates results.imgs with boxes and labels
|
|
|
228 |
gr_slider_font_size=gr_slider_font_size,
|
229 |
gr_keypt_color=gr_keypt_color,
|
230 |
gr_slider_pose_size=gr_slider_pose_size,
|
231 |
+
gr_mega_model_input=gr_mega_model_input,
|
232 |
):
|
233 |
|
234 |
############################################################
|
|
|
258 |
|
259 |
############################################################
|
260 |
# ### Run Megadetector
|
261 |
+
md_results = predict_md(img_input, gr_mega_model_input=gr_mega_model_input) #Image.fromarray(results.imgs[0])
|
262 |
|
263 |
################################################################
|
264 |
# Obtain animal crops for bboxes with confidence above th
|
|
|
325 |
# Get MegaDetector model
|
326 |
# TODO: Allow user selectable model?
|
327 |
# models = ["model_weights/md_v5a.0.0.pt","model_weights/md_v5b.0.0.pt"]
|
328 |
+
#MD_model = torch.hub.load('ultralytics/yolov5', 'custom', "megadet_model/md_v5b.0.0.pt")
|
329 |
|
330 |
|
331 |
|