|
|
| from tkinter import W |
| import gradio as gr |
| from matplotlib import cm |
| import torch |
| import torchvision |
| import matplotlib |
| from PIL import Image, ImageColor, ImageFont, ImageDraw |
| import numpy as np |
| import math |
|
|
|
|
| import yaml |
| import pdb |
|
|
| |
| |
| def predict_md(im, |
| megadetector_model, |
| size=640): |
| |
| |
| g = (size / max(im.size)) |
| im = im.resize((int(x * g) for x in im.size), |
| Image.ANTIALIAS) |
| |
| if torch.cuda.is_available(): |
| md_device = torch.device('cuda') |
| else: |
| md_device = torch.device('cpu') |
|
|
| |
| MD_model = torch.hub.load('ultralytics/yolov5', |
| 'custom', |
| megadetector_model, |
| force_reload=True, |
| device=md_device) |
| |
| |
| if (md_device == torch.device('cuda')): |
| print('Sending model to GPU') |
| MD_model.to(md_device) |
|
|
| |
| results = MD_model(im) |
| |
| return results |
|
|
|
|
| |
| def crop_animal_detections(img_in, |
| yolo_results, |
| likelihood_th): |
|
|
| |
| list_labels_as_str = [i for i in yolo_results.names.values()] |
| list_np_animal_crops = [] |
|
|
| |
| img_in = img_in.resize((yolo_results.ims[0].shape[1], |
| yolo_results.ims[0].shape[0])) |
| |
| for det_array in yolo_results.xyxy: |
|
|
| |
| for j in range(det_array.shape[0]): |
|
|
| |
| xmin_rd = int(math.floor(det_array[j,0])) |
| ymin_rd = int(math.floor(det_array[j,1])) |
|
|
| xmax_rd = int(math.ceil(det_array[j,2])) |
| ymax_rd = int(math.ceil(det_array[j,3])) |
|
|
| pred_llk = det_array[j,4] |
| pred_label = det_array[j,5] |
| |
| if (pred_label == list_labels_as_str.index('animal')) and \ |
| (pred_llk >= likelihood_th): |
| area = (xmin_rd, ymin_rd, xmax_rd, ymax_rd) |
|
|
| |
| crop = img_in.crop(area) |
| crop_np = np.asarray(crop) |
|
|
| |
| list_np_animal_crops.append(crop_np) |
|
|
| return list_np_animal_crops |