Spaces:
Running
Running
File size: 5,180 Bytes
b547fbf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import torch
import numpy as np
import cv2
from huggingface_hub import hf_hub_download
REPO_ID = "idml/Detectron2-FasterRCNN_InsectDetect"
FILENAME = "model.pth"
FILENAME_CONFIG = "config.yml"
# Ensure you have the model file
import cv2
from detectron2.config import get_cfg
from detectron2.engine import DefaultPredictor
from detectron2.data import MetadataCatalog
from detectron2.utils.visualizer import Visualizer, ColorMode
import matplotlib.pyplot as plt
viz_classes = {'thing_classes': ['Acrididae',
'Agapeta',
'Agapeta hamana',
'Animalia',
'Anisopodidae',
'Aphididae',
'Apidae',
'Arachnida',
'Araneae',
'Arctiidae',
'Auchenorrhyncha indet.',
'Baetidae',
'Cabera',
'Caenidae',
'Carabidae',
'Cecidomyiidae',
'Ceratopogonidae',
'Cercopidae',
'Chironomidae',
'Chrysomelidae',
'Chrysopidae',
'Chrysoteuchia culmella',
'Cicadellidae',
'Coccinellidae',
'Coleophoridae',
'Coleoptera',
'Collembola',
'Corixidae',
'Crambidae',
'Culicidae',
'Curculionidae',
'Dermaptera',
'Diptera',
'Eilema',
'Empididae',
'Ephemeroptera',
'Erebidae',
'Fanniidae',
'Formicidae',
'Gastropoda',
'Gelechiidae',
'Geometridae',
'Hemiptera',
'Hydroptilidae',
'Hymenoptera',
'Ichneumonidae',
'Idaea',
'Insecta',
'Lepidoptera',
'Leptoceridae',
'Limoniidae',
'Lomaspilis marginata',
'Miridae',
'Mycetophilidae',
'Nepticulidae',
'Neuroptera',
'Noctuidae',
'Notodontidae',
'Object',
'Opiliones',
'Orthoptera',
'Panorpa germanica',
'Panorpa vulgaris',
'Parasitica indet.',
'Plutellidae',
'Psocodea',
'Psychodidae',
'Pterophoridae',
'Pyralidae',
'Pyrausta',
'Sepsidae',
'Spilosoma',
'Staphylinidae',
'Stratiomyidae',
'Syrphidae',
'Tettigoniidae',
'Tipulidae',
'Tomoceridae',
'Tortricidae',
'Trichoptera',
'Triodia sylvina',
'Yponomeuta',
'Yponomeutidae']}
def detectron_process_image(image):
cfg = get_cfg()
cfg.merge_from_file(hf_hub_download(repo_id=REPO_ID, filename=FILENAME_CONFIG))
cfg.MODEL.WEIGHTS = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.2
cfg.MODEL.DEVICE='cpu'
predictor = DefaultPredictor(cfg)
numpy_image = np.array(image)
im = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
v = Visualizer(im[:, :, ::-1],
viz_classes,
scale=0.5)
outputs = predictor(im)
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
results = out.get_image()[:, :, ::-1]
rgb_image = cv2.cvtColor(results, cv2.COLOR_BGR2RGB)
return rgb_image
|