This commit is contained in:
Semi212 committed 2022-04-28 22:02:20 +08:00
1 parent 54ccfb11fe
commit a0818377bb
194 files changed
+41681 -262

No files matched your search

+199 -48
View File
@@ -26,11 +26,12 @@ from ssd.utils.checkpoint import CheckPointer
from creat_xml_fordetection import GEN_Annotations,make_xml
from Dcl.models.LoadModel import MainModel
from Dcl.DclConfig import LoadConfig, load_data_transformers
os.environ['CUDA_VISIBLE_DEVICES'] = '7'
os.environ['CUDA_VISIBLE_DEVICES'] = '3'
from PIL import ImageFile
import time
import cv2
import torch,os
from torch import nn
import numpy as np
from numpy import random
import os,sys
@@ -41,8 +42,19 @@ from Y5.utils.datasets import letterbox
from Y5.utils.general import non_max_suppression, \
scale_coords
from Y5.utils.plots import plot_one_box
from network_multi import Multi_transform_image
ImageFile.LOAD_TRUNCATED_IMAGES = True
fine_class_names = ("Anser albifrons","Fulica atra","Leucogeranus leucogeranus","Egretta garzetta","Platalea leucorodia","Mergus albellus","Grus monacha","Tringa ochropus","Numenius arquata","Anser indicus","Anas zonorhyncha","Ardea cinerea","Anas strepera","Mareca penelope","Tadorna ferruginea","Ardea alba","Numenius madagascariensis","Botaurus stellaris","Ciconia boyciana","Anser fabalis","Recurvirostra avosetta","Podiceps cristatus","Vanellus vanellus","Aythya fuligula","Tringa erythropus","Calidris alpina","Ciconia nigra","Gallinula chloropus","Porphyrio poliocephalus","Larus ridibundus","Anser cygnoides","Charadrius alexandrinus","Pluvialis squatarola","Grus grus" ,"Anser anser","Pluvialis fulva","Charadrius dubius","Anas crecca","Anas platyrhynchos","Spatula clypeata","Phalacrocorax carbo","Mergus merganser","Tadorna tadorna","Tringa nebulariaw","Bucephala clangula","Gallinago gallinago","Anser erythropus","Cygnus columbianus","Ichthyaetus ichthyaetus","Anas acuta","Ardea intermedia","Lalage melaschistos","Zosterops japonicus","Acridotheres cristatellus","Circus spilonotus","Motacilla alba","Garrulax sannio","Pycnonotus sinensis","Aythya nyroca","Lonchura striata","Turdus eunomus","Limosa lapponica","Lonchura punctulata","Phoenicurus auroreus","Ardea purpurea","Aethopyga christinae","Ardeola bacchus","Prinia inornata","Cuculus canorus","Parus major","Upupa epops","Asio fiammeus","Anthus roseatus","Phylloscopus reguloides","Phylloscopus fuscatus","Himantopus himantopus","Saxicola torquata","Dicrurus macrocercus","Eophona migratoria","Milvus migrans","Oriolus chinensis","Anthus cervinus","Falco tinnunculus","Stachyris ruficeps","Aegithalos concinnus","Turdus naumanni","Urocissa erythrorhyncha","Sibirionetta formosa","Phasianus colchicus","Anthus rubescens","Parus venustulus","Motacilla citreola","Emberiza aureola","Motacilla cinerea","Sturnus cineraceus","Picus canus","Vanellus cinereus","Emberiza spodocephala","Bambusicola thoracicus","Streptopelia tranquebarica","Actitis hypoleucos","Hirundo rustica","Carduelis sinica","Hirundo daurica","Luscinia svecicus","Monticola solitarius","Anthus richardi","Ixobrychus cinnamomeus","Spizixos semitorques","Mareca falcata","Parus monticolus","Passer montanus","Bubulcus coromandus","Turdus merula","Buteo buteo","Alcedo atthis","Horornis fortipes","Accipiter nisus","Copsychus saularis","Circus melanoleucos","Anthus hodgsoni","Anthus spinoletta","Spodiopsar sericeus","Cuculus micropterus","Motacilla flava","Pica pica","Tachybaptus ruficollis","Emberiza pusilla","Alauda gulgula","Lanius sphenocercus","Riparia riparia","Nycticorax nycticorax","Terpsiphone paradisi","Cuculus sparverioides","Falco Peregrinus","Aix galericulata","Charadrius placidus","Gallinago stenura","Mergus squamatus","Spilopelia chinensis","Lanius schach","Pomatorhinus ruficollis","Abroscopus albogularis","Turnix suscitator","Cisticola juncidis","Paradoxornis webbianus")
from cv2AddChineseText import cv2AddChineseText
#fine_class_names = ("Anser albifrons","Fulica atra","Leucogeranus leucogeranus","Egretta garzetta","Platalea leucorodia","Mergus albellus","Grus monacha","Tringa ochropus","Numenius arquata","Anser indicus","Anas zonorhyncha","Ardea cinerea","Anas strepera","Mareca penelope","Tadorna ferruginea","Ardea alba","Numenius madagascariensis","Botaurus stellaris","Ciconia boyciana","Anser fabalis","Recurvirostra avosetta","Podiceps cristatus","Vanellus vanellus","Aythya fuligula","Tringa erythropus","Calidris alpina","Ciconia nigra","Gallinula chloropus","Porphyrio poliocephalus","Larus ridibundus","Anser cygnoides","Charadrius alexandrinus","Pluvialis squatarola","Grus grus" ,"Anser anser","Pluvialis fulva","Charadrius dubius","Anas crecca","Anas platyrhynchos","Spatula clypeata","Phalacrocorax carbo","Mergus merganser","Tadorna tadorna","Tringa nebulariaw","Bucephala clangula","Gallinago gallinago","Anser erythropus","Cygnus columbianus","Ichthyaetus ichthyaetus","Anas acuta","Ardea intermedia","Lalage melaschistos","Zosterops japonicus","Acridotheres cristatellus","Circus spilonotus","Motacilla alba","Garrulax sannio","Pycnonotus sinensis","Aythya nyroca","Lonchura striata","Turdus eunomus","Limosa lapponica","Lonchura punctulata","Phoenicurus auroreus","Ardea purpurea","Aethopyga christinae","Ardeola bacchus","Prinia inornata","Cuculus canorus","Parus major","Upupa epops","Asio fiammeus","Anthus roseatus","Phylloscopus reguloides","Phylloscopus fuscatus","Himantopus himantopus","Saxicola torquata","Dicrurus macrocercus","Eophona migratoria","Milvus migrans","Oriolus chinensis","Anthus cervinus","Falco tinnunculus","Stachyris ruficeps","Aegithalos concinnus","Turdus naumanni","Urocissa erythrorhyncha","Sibirionetta formosa","Phasianus colchicus","Anthus rubescens","Parus venustulus","Motacilla citreola","Emberiza aureola","Motacilla cinerea","Sturnus cineraceus","Picus canus","Vanellus cinereus","Emberiza spodocephala","Bambusicola thoracicus","Streptopelia tranquebarica","Actitis hypoleucos","Hirundo rustica","Carduelis sinica","Hirundo daurica","Luscinia svecicus","Monticola solitarius","Anthus richardi","Ixobrychus cinnamomeus","Spizixos semitorques","Mareca falcata","Parus monticolus","Passer montanus","Bubulcus coromandus","Turdus merula","Buteo buteo","Alcedo atthis","Horornis fortipes","Accipiter nisus","Copsychus saularis","Circus melanoleucos","Anthus hodgsoni","Anthus spinoletta","Spodiopsar sericeus","Cuculus micropterus","Motacilla flava","Pica pica","Tachybaptus ruficollis","Emberiza pusilla","Alauda gulgula","Lanius sphenocercus","Riparia riparia","Nycticorax nycticorax","Terpsiphone paradisi","Cuculus sparverioides","Falco Peregrinus","Aix galericulata","Charadrius placidus","Gallinago stenura","Mergus squamatus","Spilopelia chinensis","Lanius schach","Pomatorhinus ruficollis","Abroscopus albogularis","Turnix suscitator","Cisticola juncidis","Paradoxornis webbianus")
#CUB fine_class_names = ['kong','Black_Footed_Albatross', 'Laysan_Albatross', 'Sooty_Albatross', 'Groove_Billed_Ani', 'Crested_Auklet', 'Least_Auklet', 'Parakeet_Auklet', 'Rhinoceros_Auklet', 'Brewer_Blackbird', 'Red_Winged_Blackbird', 'Rusty_Blackbird', 'Yellow_Headed_Blackbird', 'Bobolink', 'Indigo_Bunting', 'Lazuli_Bunting', 'Painted_Bunting', 'Cardinal', 'Spotted_Catbird', 'Gray_Catbird', 'Yellow_Breasted_Chat', 'Eastern_Towhee', 'Chuck_Will_Widow', 'Brandt_Cormorant', 'Red_Faced_Cormorant', 'Pelagic_Cormorant', 'Bronzed_Cowbird', 'Shiny_Cowbird', 'Brown_Creeper', 'American_Crow', 'Fish_Crow', 'Black_Billed_Cuckoo', 'Mangrove_Cuckoo', 'Yellow_Billed_Cuckoo', 'Gray_Crowned_Rosy_Finch', 'Purple_Finch', 'Northern_Flicker', 'Acadian_Flycatcher', 'Great_Crested_Flycatcher', 'Least_Flycatcher', 'Olive_Sided_Flycatcher', 'Scissor_Tailed_Flycatcher', 'Vermilion_Flycatcher', 'Yellow_Bellied_Flycatcher', 'Frigatebird', 'Northern_Fulmar', 'Gadwall', 'American_Goldfinch', 'European_Goldfinch', 'Boat_Tailed_Grackle', 'Eared_Grebe', 'Horned_Grebe', 'Pied_Billed_Grebe', 'Western_Grebe', 'Blue_Grosbeak', 'Evening_Grosbeak', 'Pine_Grosbeak', 'Rose_Breasted_Grosbeak', 'Pigeon_Guillemot', 'California_Gull', 'Glaucous_Winged_Gull', 'Heermann_Gull', 'Herring_Gull', 'Ivory_Gull', 'Ring_Billed_Gull', 'Slaty_Backed_Gull', 'Western_Gull', 'Anna_Hummingbird', 'Ruby_Throated_Hummingbird', 'Rufous_Hummingbird', 'Green_Violetear', 'Long_Tailed_Jaeger', 'Pomarine_Jaeger', 'Blue_Jay', 'Florida_Jay', 'Green_Jay', 'Dark_Eyed_Junco', 'Tropical_Kingbird', 'Gray_Kingbird', 'Belted_Kingfisher', 'Green_Kingfisher', 'Pied_Kingfisher', 'Ringed_Kingfisher', 'White_Breasted_Kingfisher', 'Red_Legged_Kittiwake', 'Horned_Lark', 'Pacific_Loon', 'Mallard', 'Western_Meadowlark', 'Hooded_Merganser', 'Red_Breasted_Merganser', 'Mockingbird', 'Nighthawk', 'Clark_Nutcracker', 'White_Breasted_Nuthatch', 'Baltimore_Oriole', 'Hooded_Oriole', 'Orchard_Oriole', 'Scott_Oriole', 'Ovenbird', 'Brown_Pelican', 'White_Pelican', 'Western_Wood_Pewee', 'Sayornis', 'American_Pipit', 'Whip_Poor_Will', 'Horned_Puffin', 'Common_Raven', 'White_Necked_Raven', 'American_Redstart', 'Geococcyx', 'Loggerhead_Shrike', 'Great_Grey_Shrike', 'Baird_Sparrow', 'Black_Throated_Sparrow', 'Brewer_Sparrow', 'Chipping_Sparrow', 'Clay_Colored_Sparrow', 'House_Sparrow', 'Field_Sparrow', 'Fox_Sparrow', 'Grasshopper_Sparrow', 'Harris_Sparrow', 'Henslow_Sparrow', 'Le_Conte_Sparrow', 'Lincoln_Sparrow', 'Nelson_Sharp_Tailed_Sparrow', 'Savannah_Sparrow', 'Seaside_Sparrow', 'Song_Sparrow', 'Tree_Sparrow', 'Vesper_Sparrow', 'White_Crowned_Sparrow', 'White_Throated_Sparrow', 'Cape_Glossy_Starling', 'Bank_Swallow', 'Barn_Swallow', 'Cliff_Swallow', 'Tree_Swallow', 'Scarlet_Tanager', 'Summer_Tanager', 'Artic_Tern', 'Black_Tern', 'Caspian_Tern', 'Common_Tern', 'Elegant_Tern', 'Forsters_Tern', 'Least_Tern', 'Green_Tailed_Towhee', 'Brown_Thrasher', 'Sage_Thrasher', 'Black_Capped_Vireo', 'Blue_Headed_Vireo', 'Philadelphia_Vireo', 'Red_Eyed_Vireo', 'Warbling_Vireo', 'White_Eyed_Vireo', 'Yellow_Throated_Vireo', 'Bay_Breasted_Warbler', 'Black_And_White_Warbler', 'Black_Throated_Blue_Warbler', 'Blue_Winged_Warbler', 'Canada_Warbler', 'Cape_May_Warbler', 'Cerulean_Warbler', 'Chestnut_Sided_Warbler', 'Golden_Winged_Warbler', 'Hooded_Warbler', 'Kentucky_Warbler', 'Magnolia_Warbler', 'Mourning_Warbler', 'Myrtle_Warbler', 'Nashville_Warbler', 'Orange_Crowned_Warbler', 'Palm_Warbler', 'Pine_Warbler', 'Prairie_Warbler', 'Prothonotary_Warbler', 'Swainson_Warbler', 'Tennessee_Warbler', 'Wilson_Warbler', 'Worm_Eating_Warbler', 'Yellow_Warbler', 'Northern_Waterthrush', 'Louisiana_Waterthrush', 'Bohemian_Waxwing', 'Cedar_Waxwing', 'American_Three_Toed_Woodpecker', 'Pileated_Woodpecker', 'Red_Bellied_Woodpecker', 'Red_Cockaded_Woodpecker', 'Red_Headed_Woodpecker', 'Downy_Woodpecker', 'Bewick_Wren', 'Cactus_Wren', 'Carolina_Wren', 'House_Wren', 'Marsh_Wren', 'Rock_Wren', 'Winter_Wren', 'Common_Yellowthroat']
CUB_CUIfine_class_names = ['kong','Black_Footed_Albatross', 'Laysan_Albatross', 'Sooty_Albatross', 'Groove_Billed_Ani', 'Crested_Auklet', 'Least_Auklet', 'Parakeet_Auklet', 'Rhinoceros_Auklet', 'Brewer_Blackbird', 'Red_Winged_Blackbird', 'Rusty_Blackbird', 'Yellow_Headed_Blackbird', 'Bobolink', 'Indigo_Bunting', 'Lazuli_Bunting', 'Painted_Bunting', 'Cardinal', 'Spotted_Catbird', 'Gray_Catbird', 'Yellow_Breasted_Chat', 'Eastern_Towhee', 'Chuck_Will_Widow', 'Brandt_Cormorant', 'Red_Faced_Cormorant', 'Pelagic_Cormorant', 'Bronzed_Cowbird', 'Shiny_Cowbird', 'Brown_Creeper', 'American_Crow', 'Fish_Crow', 'Black_Billed_Cuckoo', 'Mangrove_Cuckoo', 'Yellow_Billed_Cuckoo', 'Gray_Crowned_Rosy_Finch', 'Purple_Finch', 'Northern_Flicker', 'Acadian_Flycatcher', 'Great_Crested_Flycatcher', 'Least_Flycatcher', 'Olive_Sided_Flycatcher', 'Scissor_Tailed_Flycatcher', 'Vermilion_Flycatcher', 'Yellow_Bellied_Flycatcher', 'Frigatebird', 'Northern_Fulmar', 'Gadwall', 'American_Goldfinch', 'European_Goldfinch', 'Boat_Tailed_Grackle', 'Eared_Grebe', 'Horned_Grebe', 'Pied_Billed_Grebe', 'Western_Grebe', 'Blue_Grosbeak', 'Evening_Grosbeak', 'Pine_Grosbeak', 'Rose_Breasted_Grosbeak', 'Pigeon_Guillemot', 'California_Gull', 'Glaucous_Winged_Gull', 'Heermann_Gull', 'Herring_Gull', 'Ivory_Gull', 'Ring_Billed_Gull', 'Slaty_Backed_Gull', 'Western_Gull', 'Anna_Hummingbird', 'Ruby_Throated_Hummingbird', 'Rufous_Hummingbird', 'Green_Violetear', 'Long_Tailed_Jaeger', 'Pomarine_Jaeger', 'Blue_Jay', 'Florida_Jay', 'Green_Jay', 'Dark_Eyed_Junco', 'Tropical_Kingbird', 'Gray_Kingbird', 'Belted_Kingfisher', 'Green_Kingfisher', 'Pied_Kingfisher', 'Ringed_Kingfisher', 'White_Breasted_Kingfisher', 'Red_Legged_Kittiwake', 'Horned_Lark', 'Pacific_Loon', 'Mallard', 'Western_Meadowlark', 'Hooded_Merganser', 'Red_Breasted_Merganser', 'Mockingbird', 'Nighthawk', 'Clark_Nutcracker', 'White_Breasted_Nuthatch', 'Baltimore_Oriole', 'Hooded_Oriole', 'Orchard_Oriole', 'Scott_Oriole', 'Ovenbird', 'Brown_Pelican', 'White_Pelican', 'Western_Wood_Pewee', 'Sayornis', 'American_Pipit', 'Whip_Poor_Will', 'Horned_Puffin', 'Common_Raven', 'White_Necked_Raven', 'American_Redstart', 'Geococcyx', 'Loggerhead_Shrike', 'Great_Grey_Shrike', 'Baird_Sparrow', 'Black_Throated_Sparrow', 'Brewer_Sparrow', 'Chipping_Sparrow', 'Clay_Colored_Sparrow', 'House_Sparrow', 'Field_Sparrow', 'Fox_Sparrow', 'Grasshopper_Sparrow', 'Harris_Sparrow', 'Henslow_Sparrow', 'Le_Conte_Sparrow', 'Lincoln_Sparrow', 'Nelson_Sharp_Tailed_Sparrow', 'Savannah_Sparrow', 'Seaside_Sparrow', 'Song_Sparrow', 'Tree_Sparrow', 'Vesper_Sparrow', 'White_Crowned_Sparrow', 'White_Throated_Sparrow', 'Cape_Glossy_Starling', 'Bank_Swallow', 'Barn_Swallow', 'Cliff_Swallow', 'Tree_Swallow', 'Scarlet_Tanager', 'Summer_Tanager', 'Artic_Tern', 'Black_Tern', 'Caspian_Tern', 'Common_Tern', 'Elegant_Tern', 'Forsters_Tern', 'Least_Tern', 'Green_Tailed_Towhee', 'Brown_Thrasher', 'Sage_Thrasher', 'Black_Capped_Vireo', 'Blue_Headed_Vireo', 'Philadelphia_Vireo', 'Red_Eyed_Vireo', 'Warbling_Vireo', 'White_Eyed_Vireo', 'Yellow_Throated_Vireo', 'Bay_Breasted_Warbler', 'Black_And_White_Warbler', 'Black_Throated_Blue_Warbler', 'Blue_Winged_Warbler', 'Canada_Warbler', 'Cape_May_Warbler', 'Cerulean_Warbler', 'Chestnut_Sided_Warbler', 'Golden_Winged_Warbler', 'Hooded_Warbler', 'Kentucky_Warbler', 'Magnolia_Warbler', 'Mourning_Warbler', 'Myrtle_Warbler', 'Nashville_Warbler', 'Orange_Crowned_Warbler', 'Palm_Warbler', 'Pine_Warbler', 'Prairie_Warbler', 'Prothonotary_Warbler', 'Swainson_Warbler', 'Tennessee_Warbler', 'Wilson_Warbler', 'Worm_Eating_Warbler', 'Yellow_Warbler', 'Northern_Waterthrush', 'Louisiana_Waterthrush', 'Bohemian_Waxwing', 'Cedar_Waxwing', 'American_Three_Toed_Woodpecker', 'Pileated_Woodpecker', 'Red_Bellied_Woodpecker', 'Red_Cockaded_Woodpecker', 'Red_Headed_Woodpecker', 'Downy_Woodpecker', 'Bewick_Wren', 'Cactus_Wren', 'Carolina_Wren', 'House_Wren', 'Marsh_Wren', 'Rock_Wren', 'Winter_Wren', 'Common_Yellowthroat','Anas_poecilorhyncha', 'Greylag_Goose', 'Swan_Goose', 'Domestic_goose', 'Podiceps_ruficollis']
PYH_3_class = ['Grus_vipio','Grus_leucogeranus','Grus_monacha']
fine_class_names = PYH_3_class
# CUI BHQ_name = ["Mallard", "Anas_poecilorhyncha", "Greylag_Goose", "Swan_Goose", "Domestic_goose","Podiceps_ruficollis"]
BHQ_name = ['Grus_vipio','Grus_leucogeranus','Grus_monacha']
# CUI BHQ_chinese = ['绿头鸭', '斑嘴鸭', '灰雁', '鸿雁', '家鹅', '小鷿鷈']
BHQ_chinese = ['白枕鹤', '白鹤', '白头鹤']
class Detector:
def __init__(self, device):
@@ -53,7 +65,7 @@ class Detector:
self.stride = 1
#self.weights = "/data1/Hatcher/model/yolov5/runs/train/exp15/weights/best.pt"
#self.weights = '/data1/Hatcher/model/yolov5/runs/train/exp10/weights/best.pt'
self.weights = '/data1/Hatcher/model/data_MPKFNet/weigth/detection/Y/best.pt'
self.weights = '/data1/Hatcher/model/yolov5/runs/train/exp19/weights/best.pt'
self.device = device
#self.device = select_device(self.device)
model = attempt_load(self.weights, map_location=self.device)
@@ -80,6 +92,7 @@ class Detector:
#im 原图 imo 1024 img 640
def detect(self, im):
img_detect = cv2.resize(im, (1024, 768))
# twice image resize or-->1024--->640
im0, img = self.preprocess(img_detect)
pred = self.model(img, augment=False)[0]
@@ -192,7 +205,57 @@ class Fine_grained:
print("fine data final is ", (t4 - t3) * 1000)
return imagelist, labellist, scorelist
class Fine_grained_Transformer:
def __init__(self,device):
self.device = device
self.WeightPath = '/data/Hatcher/data/weigth/areaproject/resnet50-PYH-3_0.7/_3911_PYH-3/best.pth'
self.net = Multi_transform_image().to(device)
model_dict = self.net.state_dict()
pretrained_dict = torch.load(self.WeightPath)
pretrained_dict = {k: v for k, v in pretrained_dict.items() if
k not in ['classifier.weight', 'classifier_swap.weight', 'Convmask.weight', 'Convmask.bias']}
model_dict.update(pretrained_dict)
self.net.load_state_dict(model_dict)
self.transformers = load_data_transformers(600, 448, [7, 7])
print("load model weight is ok")
self.net.train(False)
self.softmax = nn.Softmax()
def inference(self,img,imagelist,labellist,scorelist):
with torch.no_grad():
ratio_x = (3840.0 / 1024.0)
ratio_y = (2160.0 / 768.0)
for i,box in enumerate(imagelist):
Img = img.copy()[int(box[1]):int(box[3]), int(box[0]):int(box[2]), :]
#cv2.imwrite("/data/Hatcher/detectout6/{}.jpg".format(str(i)),Img)
#height_or, width_or = Img.shape[:2]
#Img = cv2.resize(Img,(int(width_or/ratio_x),int(height_or/ratio_y)))
if (int(box[3]) - int(box[1]) > 100) and (int(box[2]) - int(box[0]) > 100):
t1 = time.time()
Img = Image.fromarray(cv2.cvtColor(Img, cv2.COLOR_BGR2RGB))
Img = self.transformers['test_totensor'](Img)
Img = Img.unsqueeze(0)
inputs = Variable(Img.cuda())
t2 = time.time()
print("fine data deal is ", (t2 - t1) * 1000)
outputs = self.net(inputs)
t3 = time.time()
print("fine inference is ", (t3 - t2) * 1000)
outputs[0] = self.softmax(outputs[0])
top3_val, top3_pos = torch.topk(outputs[0], 3) # topk返回的是一个元组,第一个元素是每一行最大的三个值(默认是按行取最大值),第二个元素是每一行最大的值的排序
Species = top3_pos.cpu().numpy()[0]
ConfidenceLevel = top3_val.cpu().numpy()[0]
ConfidenceLevel = ConfidenceLevel.astype(np.float64)
#ConfidenceLevel = ConfidenceLevel/26.0
print("hhhhhhhhhhhhhhhhhhhhhhh is ",ConfidenceLevel)
labellist[i] = Species
scorelist[i] = ConfidenceLevel
t4 = time.time()
print("fine data final is ", (t4 - t3) * 1000)
return imagelist, labellist, scorelist
def parse_args():
parser = argparse.ArgumentParser(description="SSD Demo.")
parser.add_argument(
@@ -351,13 +414,14 @@ def run_demo_fine_SSD(cfg, ckpt, score_threshold, images_dir, output_dir, datase
Image.fromarray(drawn_image).save(os.path.join(output_dir, image_name))
def run_demo_fine_Y5(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type):
device = torch.device(cfg.MODEL.DEVICE)
fine1 = Fine_grained(device)
fine1 = Fine_grained_Transformer(device)
detector = Detector(device)
image_paths = glob.glob(os.path.join(images_dir, '*.jpg'))
image_paths.sort()
mkdir(output_dir)
cpu_device = torch.device("cpu")
num_image = 0
for i_D, image_path in enumerate(image_paths):
image_name = os.path.basename(image_path)
@@ -373,17 +437,19 @@ def run_demo_fine_Y5(cfg, ckpt, score_threshold, images_dir, output_dir, dataset
continue
indices_label_left = labels < 1
indices_label_right = labels <1
indices = []
indices=torch.empty(len(labels)).bool()
for i in range(len(indices_label_left)):
indices.append(indices_label_left[i] and indices_label_right[i])
indices_temp = (indices_label_left[i] and indices_label_right[i])
#print(indices_temp.item())
indices[i] = indices_temp.item()
#indices = torch.Tensor(indices)
pred_boxes = pred_boxes[indices]
labels = labels[indices]
scores = scores[indices] # 因为莫名奇妙多了一个list维度,所以加了这个0
if len(labels)>0:
boxes = pred_boxes[0]
labels = labels[0]
scores = scores[0]
boxes = pred_boxes
# labels = labels[0]
# scores = scores[0]
print(len(scores))
ratio_x = (width_or / 1024.0)
ratio_y = (height_or / 768.0)
@@ -410,8 +476,11 @@ def run_demo_fine_Y5(cfg, ckpt, score_threshold, images_dir, output_dir, dataset
]
)
print('({:04d}/{:04d}) {}: {}'.format(i_D + 1, len(image_paths), image_name, meters))
num_detect = 0
spiec_number = [0 for i in range(len(BHQ_chinese))]
for i in range(len(scores)):
xyxy = boxes[i]
conf = scores[i]
cls = labels[i]
@@ -419,39 +488,70 @@ def run_demo_fine_Y5(cfg, ckpt, score_threshold, images_dir, output_dir, dataset
#label = f'{int(cls)} {conf:.2f}'
#if int(cls)==3:
if int(cls)==99999:
label = f'{int(cls)} {conf:.2f}'
label = f'{int(cls)}'
else:
label = f'{fine_class_names[int(cls)]} {conf:.2f}'
label = f'{fine_class_names[int(cls)]}'
# if int(cls) == 11:
# label = f'{"苍鹭"} {conf:.2f}'
# if label == 3:
# label = "白鹭"
# if label ==11:
# label = "苍鹭"
print(label)
if label in BHQ_name:
label = BHQ_chinese[BHQ_name.index(label)]
spiec_number[BHQ_chinese.index(label)] +=1
else:
#label = '未知鸟类'
label = None
cls = -1
palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
colors = [int((p * (cls ** 2 - cls + 1)) % 255) for p in palette]
plot_one_box(xyxy, img, label=label, color=colors, line_thickness=3)
img = plot_one_box(xyxy, img, label=label, color=colors, line_thickness=2)
num_detect+=1
else:
boxes, labels, scores = [],[],[]
#drawn_image = draw_boxes(image, boxes_fine, labels_fine, scores_fine).astype(np.uint8)
xml_outputpath = os.path.join(output_dir, image_name).replace("/result/","/result_xml/").replace(".jpg",".xml")
make_xml(image_name,im0.shape[0],im0.shape[1],boxes,labels,scores,xml_outputpath)
cv2.imwrite(os.path.join(output_dir, image_name),im0)
if num_image%10 ==0:
num_count_old = num_detect
spiec_number_old = spiec_number
if width_or == 2560:
img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)), (50, 1200), textColor=(255, 255, 255),
textSize=40)
img = cv2AddChineseText(img, "分类识别:", (50, 1250), textColor=(255, 255, 255),
textSize=40)
for i in range(len(spiec_number)):
img = cv2AddChineseText(img, "{}数量:{}只".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
(250 + i * 300, 1250), textColor=(255, 255, 255),
textSize=40)
if width_or == 1920:
radio_w = 1920 / 2560.0
radio_h = 1080 / 1440.0
img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)),
(int(50 * radio_w), int(1200 * radio_h)), textColor=(255, 255, 255),
textSize=40)
img = cv2AddChineseText(img, "分类识别:", (int(50 * radio_w), int(1250 * radio_h)), textColor=(255, 255, 255),
textSize=40)
for i in range(len(spiec_number)):
img = cv2AddChineseText(img, "{}数量:{}只".format(str(BHQ_chinese[i]), str(spiec_number_old[i])),
(int(250) + i * int(300), int(1250 * radio_h)), textColor=(255, 255, 255),
textSize=40)
cv2.imwrite(os.path.join(output_dir, image_name),img)
num_image += 1
#Image.fromarray(drawn_image).save(os.path.join(output_dir, image_name))
def run_demo_Y5(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type):
device = torch.device(cfg.MODEL.DEVICE)
#fine1 = Fine_grained(device)
fine1 = Fine_grained_Transformer(device)
detector = Detector(device)
image_paths = glob.glob(os.path.join(images_dir, '*.jpg'))
image_paths.sort()
mkdir(output_dir)
cpu_device = torch.device("cpu")
num_image = 0
for i_D, image_path in enumerate(image_paths):
image_name = os.path.basename(image_path)
print(image_name)
#a = Image.open(image_path)
img = cv2.imread(image_path) # BG
height_or, width_or = img.shape[:2]
t3 = time.time()
@@ -461,34 +561,85 @@ def run_demo_Y5(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type
print(type(labels))
if len(labels) == 0:
continue
meters = ' | '.join(
[
'objects {:02d}'.format(len(pred_boxes)),
]
)
print('({:04d}/{:04d}) {}: {}'.format(i_D + 1, len(image_paths), image_name, meters))
ratio_x = (width_or / 1024.0)
ratio_y = (height_or/ 768.0)
indices_label_left = labels < 1
indices_label_right = labels < 1
indices = torch.empty(len(labels)).bool()
for i in range(len(indices_label_left)):
indices_temp = (indices_label_left[i] and indices_label_right[i])
indices[i] = indices_temp.item()
pred_boxes = pred_boxes[indices]
labels = labels[indices]
scores = scores[indices] # 因为莫名奇妙多了一个list维度,所以加了这个0
if len(labels) > 0:
boxes = pred_boxes
print(len(scores))
ratio_x = (width_or / 1024.0)
ratio_y = (height_or / 768.0)
for i in range(len(boxes)):
boxes[i][0] = boxes[i][0] * ratio_x
boxes[i][2] = boxes[i][2] * ratio_x
boxes[i][1] = boxes[i][1] * ratio_y
boxes[i][3] = boxes[i][3] * ratio_y
if boxes[i][0] < 0:
boxes[i][0] = 0
if boxes[i][1] < 0:
boxes[i][1] = 0
if boxes[i][2] < 0:
boxes[i][2] = 0
if boxes[i][3] < 0:
boxes[i][3] = 0
for i in range(len(labels)):
labels[i] = 99999
boxes_fine, labels_fine, scores_fine = boxes, labels, scores
meters = ' | '.join(
[
'objects {:02d}'.format(len(boxes_fine)),
]
)
print('({:04d}/{:04d}) {}: {}'.format(i_D + 1, len(image_paths), image_name, meters))
num_detect = 0
for i in range(len(scores)):
xyxy = boxes[i]
cls = labels[i]
print(cls)
if int(cls) == 99999:
label = f'{int(cls)}'
else:
label = f'{fine_class_names[int(cls)]}'
for i in range(len(scores)):
xyxy = pred_boxes[i]
xyxy[0] = xyxy[0] * ratio_x
xyxy[2] = xyxy[2] * ratio_x
xyxy[1] = xyxy[1] * ratio_y
xyxy[3] = xyxy[3] * ratio_y
conf = scores[i]
cls = labels[i]
print(cls)
label = f'{int(cls)} {conf:.2f}'
palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
colors = [int((p * ((cls+10) ** 2 - cls + 1)) % 255) for p in palette]
plot_one_box(xyxy, im0, label=label, color=colors, line_thickness=6)
#drawn_image = draw_boxes(image, boxes_fine, labels_fine, scores_fine).astype(np.uint8)
xml_outputpath = os.path.join(output_dir, image_name).replace("/result/","/result_xml/").replace(".jpg",".xml")
make_xml(image_name,im0.shape[0],im0.shape[1],pred_boxes,labels,scores,xml_outputpath)
cv2.imwrite(os.path.join(output_dir, image_name),im0)
#Image.fromarray(drawn_image).save(os.path.join(output_dir, image_name))
label = None
cls = -1
palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
colors = [int((p * (cls ** 2 - cls + 1)) % 255) for p in palette]
img = plot_one_box(xyxy, img, label=label, color=colors, line_thickness=2)
num_detect += 1
else:
boxes, labels, scores = [], [], []
# drawn_image = draw_boxes(image, boxes_fine, labels_fine, scores_fine).astype(np.uint8)
xml_outputpath = os.path.join(output_dir, image_name).replace("/result/", "/result_xml/").replace(".jpg",
".xml")
make_xml(image_name, im0.shape[0], im0.shape[1], boxes, labels, scores, xml_outputpath)
if num_image % 10 == 0:
num_count_old = num_detect
if width_or == 2560:
img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)), (50, 1200), textColor=(255, 255, 255),
textSize=70)
if width_or == 1920:
radio_w = 1920 / 2560.0
radio_h = 1080 / 1440.0
img = cv2AddChineseText(img, "识别总数: {}只".format(str(num_count_old)),
(int(50 * radio_w), int(1200 * radio_h)), textColor=(255, 255, 255),
textSize=70)
cv2.imwrite(os.path.join(output_dir, image_name), img)
num_image += 1
def main(args):
@@ -511,7 +662,7 @@ def main(args):
if not os.path.exists(output_temp):
os.mkdir(output_temp)
run_demo_fine_Y5(cfg=cfg,
ckpt=args.ckpt,
ckpt=None,
score_threshold=args.score_threshold,
images_dir=args.images_dir,
output_dir=args.output_dir,