Update app.py
Browse files
app.py
CHANGED
@@ -90,6 +90,78 @@ def predict_pipeline(img_input,
|
|
90 |
pose_cfg_dict['all_joints_names'])])
|
91 |
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
|
95 |
|
|
|
90 |
pose_cfg_dict['all_joints_names'])])
|
91 |
|
92 |
|
93 |
+
##############################################################
|
94 |
+
# Run DLC and visualize results
|
95 |
+
dlc_proc = Processor()
|
96 |
+
|
97 |
+
# if required: ignore MD crops and run DLC on full image [mostly for testing]
|
98 |
+
if flag_dlc_only:
|
99 |
+
# compute kpts on input img
|
100 |
+
list_kpts_per_crop = predict_dlc([np.asarray(img_input)],
|
101 |
+
kpts_likelihood_th,
|
102 |
+
path_to_DLCmodel,
|
103 |
+
dlc_proc)
|
104 |
+
# draw kpts on input img #fix!
|
105 |
+
draw_keypoints_on_image(img_input,
|
106 |
+
list_kpts_per_crop[0], # a numpy array with shape [num_keypoints, 2].
|
107 |
+
map_label_id_to_str,
|
108 |
+
flag_show_str_labels,
|
109 |
+
use_normalized_coordinates=False,
|
110 |
+
font_style=font_style,
|
111 |
+
font_size=font_size,
|
112 |
+
keypt_color=keypt_color,
|
113 |
+
marker_size=marker_size)
|
114 |
+
|
115 |
+
donw_file = save_results_only_dlc(list_kpts_per_crop[0], map_label_id_to_str,dlc_model_input_str)
|
116 |
+
|
117 |
+
return img_input, donw_file
|
118 |
+
|
119 |
+
else:
|
120 |
+
# Compute kpts for each crop
|
121 |
+
list_kpts_per_crop = predict_dlc(list_crops,
|
122 |
+
kpts_likelihood_th,
|
123 |
+
path_to_DLCmodel,
|
124 |
+
dlc_proc)
|
125 |
+
|
126 |
+
# resize input image to match megadetector output
|
127 |
+
img_background = img_input.resize((md_results.ims[0].shape[1],
|
128 |
+
md_results.ims[0].shape[0]))
|
129 |
+
|
130 |
+
# draw keypoints on each crop and paste to background img
|
131 |
+
for ic, (np_crop, kpts_crop) in enumerate(zip(list_crops,
|
132 |
+
list_kpts_per_crop)):
|
133 |
+
|
134 |
+
img_crop = Image.fromarray(np_crop)
|
135 |
+
|
136 |
+
# Draw keypts on crop
|
137 |
+
draw_keypoints_on_image(img_crop,
|
138 |
+
kpts_crop, # a numpy array with shape [num_keypoints, 2].
|
139 |
+
map_label_id_to_str,
|
140 |
+
flag_show_str_labels,
|
141 |
+
use_normalized_coordinates=False, # if True, then I should use md_results.xyxyn for list_kpts_crop
|
142 |
+
font_style=font_style,
|
143 |
+
font_size=font_size,
|
144 |
+
keypt_color=keypt_color,
|
145 |
+
marker_size=marker_size)
|
146 |
+
|
147 |
+
# Paste crop in original image
|
148 |
+
img_background.paste(img_crop,
|
149 |
+
box = tuple([int(t) for t in md_results.xyxy[0][ic,:2]]))
|
150 |
+
|
151 |
+
# Plot bbox
|
152 |
+
bb_per_animal = md_results.xyxy[0].tolist()[ic]
|
153 |
+
pred = md_results.xyxy[0].tolist()[ic][4]
|
154 |
+
if bbox_likelihood_th < pred:
|
155 |
+
draw_bbox_w_text(img_background,
|
156 |
+
bb_per_animal,
|
157 |
+
font_style=font_style,
|
158 |
+
font_size=font_size) # TODO: add selectable color for bbox?
|
159 |
+
|
160 |
+
|
161 |
+
# Save detection results as json
|
162 |
+
download_file = save_results_as_json(md_results,list_kpts_per_crop,map_label_id_to_str, bbox_likelihood_th,dlc_model_input_str,mega_model_input)
|
163 |
+
|
164 |
+
return img_background, download_file
|
165 |
|
166 |
|
167 |
|