diff --git a/DetectFineSortVideo.py b/DetectFineSortVideo.py index 2164e33..a2ae5f2 100755 --- a/DetectFineSortVideo.py +++ b/DetectFineSortVideo.py @@ -107,12 +107,12 @@ class DetFineVid(): tm2 = time.time() if gp.DebugPrint_flag: print("main1 time is ", (tm2 - tm1) * 1000) - # cv2.imshow("AI-ECO", img_show) - # #cv2.imwrite("../data_MPKFNet/RTSP3/" + "{}.jpg".format(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))),img_show) - # if cv2.waitKey(1) & 0xFF == ord('r'): - # break - save_path =os.path.join(self.SavePath,"{}.jpg".format(str(num_detection).zfill(6))) - cv2.imwrite(save_path,img_show) + cv2.imshow("AI-ECO", img_show) + #cv2.imwrite("../data_MPKFNet/RTSP3/" + "{}.jpg".format(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))),img_show) + if cv2.waitKey(1) & 0xFF == ord('r'): + break + # save_path =os.path.join(self.SavePath,"{}.jpg".format(str(num_detection).zfill(6))) + # cv2.imwrite(save_path,img_show) tm3 = time.time() if gp.DebugPrint_flag: print("main2 time is ", (tm3 - tm2) * 1000) diff --git a/DetectionAILAbel.py b/DetectionAILAbel.py new file mode 100644 index 0000000..5db1bb2 --- /dev/null +++ b/DetectionAILAbel.py @@ -0,0 +1,30 @@ +import os,argparse,torch +from System_I import Detection_Y5 +from tqdm import tqdm + + + +if __name__ == '__main__': + parser1 = argparse.ArgumentParser(description="AILabel") + parser1.add_argument("--RawPath", default='/data/DataManagement/Project/001_cuihu/0012_20220517_RTSP/imagesAILabel', type=str, + help='RawPath.') + parser1.add_argument("--GPU", default="1", type=str, + help='GPU.') + parser1.add_argument("--thresh", default=0.4, type=float, + help='thresh.') + parser1.add_argument("--Weight", default="/data/Hatcher/data/Detection_weigth/BJCHKMCH-Bird-1-4/weights/best.pt", type=str, + help='Weight.') + args = parser1.parse_args() + os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU + devices = torch.device('cuda:{}'.format(str(int(args.GPU))) if torch.cuda.is_available() else 'cpu') + if not os.path.exists(args.RawPath.replace("imagesAILabel","NoResult")): + os.makedirs(args.RawPath.replace("imagesAILabel","NoResult")) + if not os.path.exists(args.RawPath.replace("imagesAILabel","YesResult")): + os.makedirs(args.RawPath.replace("imagesAILabel","YesResult")) + if not os.path.exists(args.RawPath.replace("imagesAILabel","LabelXML")): + os.makedirs(args.RawPath.replace("imagesAILabel","LabelXML")) + Detection = Detection_Y5(device=devices,root=" ", detect_thresh=args.thresh,DetWeight=args.Weight) + imagepaths = os.listdir(args.RawPath) + for imagepath in tqdm(imagepaths): + imagepath = os.path.join(args.RawPath,imagepath) + Detection.AIlabel(imagepath) \ No newline at end of file diff --git a/System_I.py b/System_I.py index db2057f..e0c2b63 100755 --- a/System_I.py +++ b/System_I.py @@ -7,7 +7,7 @@ import json import random from configs.configs import model_head -import os,sys +import os,sys,shutil import torch from torch import nn from PIL import Image, ImageDraw, ImageFont @@ -23,6 +23,7 @@ import numpy as np from fine.models.LoadModel import MainModel from fine.fineConfig import LoadConfig, load_data_transformers from torch.autograd import Variable +from creat_xml_fordetection import make_xml import threading from common import global_para as gp import time,statistics @@ -288,17 +289,14 @@ class Detection_S: print("check final is ", (t5 - t4) * 1000) return flag,labels,boxes,scores class Detection_Y5: - def __init__(self,device,root): + def __init__(self,device,root,detect_thresh,DetWeight): self.device = device self.root = root self.img_size = 640 - self.conf_thres = gp.detect_thresh + self.conf_thres = detect_thresh self.iou_thres = 0.4 self.stride = 1 - if gp.red_flag: - self.weights = '../data_MPKFNet/test/2_r.pt' - else: - self.weights = gp.DetWeight + self.weights = DetWeight # self.device = select_device(self.device) model = attempt_load(self.weights, map_location=self.device) model.to(self.device).eval() @@ -320,6 +318,68 @@ class Detection_Y5: img = img.unsqueeze(0) return img0, img + def AIlabel(self,imagename): + with torch.no_grad(): + image = cv2.imread(imagename) + image_or = image + img_detect = cv2.resize(image, (1024, 768)) + t1 = time.time() + image_scale,image = self.preprocess(img_detect) + t2 = time.time() + if gp.DebugPrint_flag: + print("Detection data deal is ",(t2-t1)*1000) + pred = self.model(image, augment=False)[0] + pred = pred.float() + pred = non_max_suppression(pred, self.conf_thres, self.iou_thres) + t3 = time.time() + if gp.DebugPrint_flag: + print("Detection inference is ",(t3-t2)*1000) + boxes = [] + scores = [] + labels = [] + for det in pred: + if det is not None and len(det): + det[:, :4] = scale_coords( + image.shape[2:], det[:, :4], image_scale.shape).round() + + boxes.append(det[:, :4]) + scores.append(det[:, 4]) + labels.append((det[:, -1])) + boxes = boxes[0] + scores = scores[0] + labels = labels[0] + t5 = time.time() + if gp.DebugPrint_flag: + print("Detection get result is ", (t5 - t3) * 1000) + if len(labels) > 0: + ratio_x = (image_or.shape[1] / 1024.0) + ratio_y = (image_or.shape[0] / 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] = "bird" + t4 = time.time() + if gp.DebugPrint_flag: + print("Detection final is ", (t4 - t3) * 1000) + xml_outputpath = imagename.replace("imagesAILabel","LabelXML").replace(".jpg",".xml") + make_xml(imagename,image_or.shape[0],image_or.shape[1],boxes,labels,scores,xml_outputpath) + shutil.copy(imagename, imagename.replace("imagesAILabel", "YesResult")) + else: + shutil.copy(imagename,imagename.replace("imagesAILabel","NoResult")) + def inference(self,image,imagetime,index,fine_grained): with torch.no_grad(): rootpath = os.path.join(self.root, imagetime) @@ -1578,7 +1638,7 @@ class Density_mul (threading.Thread): gp.destory_image = True if gp.DebugPrint_flag: print(self.name + self.imagetime," is over") -class Detecntion_mul (threading.Thread): +class Detection_mul (threading.Thread): def __init__(self, threadID, name,imagetime,DetectionClass,Fine_grainedClass): threading.Thread.__init__(self) self.threadID = threadID diff --git a/common.py b/common.py index f7f4140..399b7ef 100755 --- a/common.py +++ b/common.py @@ -7,28 +7,28 @@ import os,torch, threading,argparse class global_para(): #check() - parser1 = argparse.ArgumentParser(description="CUI") - parser1.add_argument("video_num", default='/data1/Hatcher/model/project/data_MPKFNet/rawdata/竹林嘴-3月3日候鸟视频.mp4', type=str, - help='Video name.') - parser1.add_argument("GPU", default='2', type=str, - help='GPU.') - parser1.add_argument("--thresh", default=0.4, type=float, - help='GPU.') - parser1.add_argument("--red", default=0, type=int, - help='red.') - parser1.add_argument("--add", default=0, type=str, - help='add.') - - args = parser1.parse_args() + # parser1 = argparse.ArgumentParser(description="CUI") + # parser1.add_argument("video_num", default='/data1/Hatcher/model/project/data_MPKFNet/rawdata/竹林嘴-3月3日候鸟视频.mp4', type=str, + # help='Video name.') + # parser1.add_argument("GPU", default='2', type=str, + # help='GPU.') + # parser1.add_argument("--thresh", default=0.4, type=float, + # help='GPU.') + # parser1.add_argument("--red", default=0, type=int, + # help='red.') + # parser1.add_argument("--add", default=0, type=str, + # help='add.') + # + # args = parser1.parse_args() #print(args) - root = "/home/gyd/output1" - # os.environ['CUDA_VISIBLE_DEVICES'] = '0' - os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU - devices = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') - camera_address = args.add - VideoPath = args.video_num - red_flag = args.red - detect_thresh = args.thresh + # root = "/home/gyd/output1" + # # os.environ['CUDA_VISIBLE_DEVICES'] = '0' + # os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU + # devices = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') + # camera_address = args.add + # VideoPath = args.video_num + # red_flag = args.red + # detect_thresh = args.thresh #fine_thresh2 = torch.tensor(fine_thresh2).cuda() birdIndex = 99999 @@ -51,7 +51,7 @@ class global_para(): showThresh = 25 destory_image = False - Camera_device = args.video_num # ÉãÏñÍ·±àºÅ£¬0001 + # Camera_device = args.video_num # ÉãÏñÍ·±àºÅ£¬0001 image_time = None # µ±Ç°´¦ÀíµÄ30ÊÓÆµµÄ±àºÅ ImgDealQueue = [] # 750Ö¡ÊÓÆµ£¬»º´æ¿Õ¼ä img_show = None @@ -80,28 +80,28 @@ class global_para(): # DetWeight = "/data1/Hatcher/model/Detection/yolov5/runs/train/exp21/weights/best.pt" # FineWeight = "/data/Hatcher/data/weigth/areaproject/resnet50-JXPYH-Bird-3-4-20220429/_42911_PYH-3/best.pth" - DetWeight = "/data1/Hatcher/model/Detection/yolov5/runs/train/KunMingCuiHu-Fish2/weights/best.pt" - FineWeight = "/data/Hatcher/data/weigth/areaproject/resnet50-KMDC-Fish-5-1-20220505/_5510_DianChiFish-4/best.pth" - # DetWeight = "/data1/Hatcher/model/Detection/yolov5/runs/train/BeijingCuiHu-Bird26/weights/best.pt" - # FineWeight = "/data/Hatcher/data/weigth/areaproject/resnet50-cui90_1model0.7/_42111_CUI-94-1/best.pth" + #DetWeight = "/data1/Hatcher/model/Detection/yolov5/runs/train/KunMingCuiHu-Fish2/weights/best.pt" + #FineWeight = "/data/Hatcher/data/weigth/areaproject/resnet50-KMDC-Fish-5-1-20220505/_5510_DianChiFish-4/best.pth" + DetWeight = "/data1/Hatcher/model/Detection/yolov5/runs/train/BeijingCuiHu-Bird26/weights/best.pt" + FineWeight = "/data/Hatcher/data/weigth/areaproject/VIT-BJCH_Bird_94_DDP/_5914_CUI-94-1/best.pth" #BHG_chineseName = ["kong", '白鹤', '白头鹤','白枕鹤'] - BHG_chineseName = ["kong","鲫鱼","麦穗鱼","鳑鲏","鲈鱼"] + #BHG_chineseName = ["kong","鲫鱼","麦穗鱼","鳑鲏","鲈鱼"] - # BHG_chineseName = ['Kong', '白额雁', '白骨顶', '苍鹭', '苍鹭', '白秋沙鸭', '白腰草鹬', '斑头雁', '斑嘴鸭', '苍鹭', '赤膀鸭', '赤颈鸭', '苍鹭', '大麻鳽', - # '豆雁', '反嘴鹬', '凤头鸊鷉', '凤头麦鸡', '凤头潜鸭', '黑鹳', '黑水鸡', '红嘴鸥', '鸿雁', '灰鹤', '灰雁', '绿翅鸭', '绿头鸭', '琵嘴鸭', - # '普通鸬鹚', '普通秋沙鸭', '翘鼻麻鸭', '鹊鸭', '扇尾沙锥', '小白额雁', '小天鹅', '针尾鸭', '苍鹭', '暗绿绣眼鸟', '八哥', '白腹鹞', '白鹡鸰', - # '白眼潜鸭', '草鹭', '池鹭', '戴胜', '黑翅长脚鹬', '黑鸢', '红隼', '花脸鸭', '环颈雉', '灰头绿啄木鸟', '灰头麦鸡', '矶鹬', '栗苇鳽', - # '罗纹鸭', '牛背鹭', '普通翠鸟', '雀鹰', '喜鹊', '苍鹭', '鸳鸯', '棕背伯劳', '红胸秋沙鸭', '普通燕鸥', '小白额燕鸥', '丘鹬', '凤头蜂鹰', - # '大天鹅', '大鸨', '小田鸡', '山斑鸠', '帆背潜鸭', '日本松雀鹰', '普通鵟', '松雀鹰', '毛脚鵟', '灰斑鸠', '白头鹞', '白尾鹞', '白眉鸭', - # '白鹈鹕', '红头潜鸭', '苍鹰', '蒙古银鸥', '西方秧鸡', '赤嘴潜鸭', '阔嘴鹬', '雕鸮', '鹌鹑', '鹗', '黄苇鳽', '黑颈鹤', '小䴙䴘', '赤麻鸭'] + BHG_chineseName = ['Kong', '白额雁', '白骨顶', '苍鹭', '苍鹭', '白秋沙鸭', '白腰草鹬', '斑头雁', '斑嘴鸭', '苍鹭', '赤膀鸭', '赤颈鸭', '苍鹭', '大麻鳽', + '豆雁', '反嘴鹬', '凤头鸊鷉', '凤头麦鸡', '凤头潜鸭', '黑鹳', '黑水鸡', '红嘴鸥', '鸿雁', '灰鹤', '灰雁', '绿翅鸭', '绿头鸭', '琵嘴鸭', + '普通鸬鹚', '普通秋沙鸭', '翘鼻麻鸭', '鹊鸭', '扇尾沙锥', '小白额雁', '小天鹅', '针尾鸭', '苍鹭', '暗绿绣眼鸟', '八哥', '白腹鹞', '白鹡鸰', + '白眼潜鸭', '草鹭', '池鹭', '戴胜', '黑翅长脚鹬', '黑鸢', '红隼', '花脸鸭', '环颈雉', '灰头绿啄木鸟', '灰头麦鸡', '矶鹬', '栗苇鳽', + '罗纹鸭', '牛背鹭', '普通翠鸟', '雀鹰', '喜鹊', '苍鹭', '鸳鸯', '棕背伯劳', '红胸秋沙鸭', '普通燕鸥', '小白额燕鸥', '丘鹬', '凤头蜂鹰', + '大天鹅', '大鸨', '小田鸡', '山斑鸠', '帆背潜鸭', '日本松雀鹰', '普通鵟', '松雀鹰', '毛脚鵟', '灰斑鸠', '白头鹞', '白尾鹞', '白眉鸭', + '白鹈鹕', '红头潜鸭', '苍鹰', '蒙古银鸥', '西方秧鸡', '赤嘴潜鸭', '阔嘴鹬', '雕鸮', '鹌鹑', '鹗', '黄苇鳽', '黑颈鹤', '小䴙䴘', '赤麻鸭'] # BHG_chineseName =["kong","绿头鸭","斑嘴鸭","灰雁","鸿雁","kong","小鷿鷈"] spiec_number_old = [0 for i in range(len(BHG_chineseName))] - FineClassNum = 5 + FineClassNum = 94 color_show = (255, 255, 255) color_box = (255, 0, 0) - NoneLabel = "鱼"#"鹤类" # "鱼" - fine_thresh1 = 6000 - fine_thresh2 = 6000 + NoneLabel = None#"鹤类" # "鱼" + fine_thresh1 = 10000 + fine_thresh2 = 10000 ShowAllNumFlag = True fontScale = 1 """ diff --git a/creat_xml_fordetection.py b/creat_xml_fordetection.py index 01c3ecf..e3ee1a6 100755 --- a/creat_xml_fordetection.py +++ b/creat_xml_fordetection.py @@ -95,7 +95,7 @@ def make_xml(filename,size_x,size_y,boxes,labels,scores,savename): xmax = int(boxes[i][2]) ymax = int(boxes[i][3]) score = int(scores[i]*1000) - anno.add_pic_attr(str(labels[i]), xmin, ymin, xmax, ymax,score) + anno.add_pic_attr("bird", xmin, ymin, xmax, ymax,score) anno.savefile(savename) def make_xml_iccv(filename,size_x,size_y,boxes,savename): anno = GEN_Annotations(filename) diff --git a/detection_negative_samples.py b/detection_negative_samples.py index 271a52a..c77647a 100755 --- a/detection_negative_samples.py +++ b/detection_negative_samples.py @@ -3,16 +3,15 @@ import cv2 import random from PIL import Image from lxml import etree -path_unit= "/data1/zwq/model/data_MPKFNet/data/ConstructionSite/Hatcher_One" -path_raw = "/data1/zwq/model/data_MPKFNet/data/ConstructionSite/NO" -result_path = "/data1/zwq/model/data_MPKFNet/data/ConstructionSite/result" +import argparse,random +from tqdm import tqdm class GEN_Annotations: def __init__(self, filename): self.root = etree.Element("annotation") child1 = etree.SubElement(self.root, "folder") - child1.text = "VOC2007" + child1.text = "CDB" child2 = etree.SubElement(self.root, "filename") child2.text = filename @@ -20,7 +19,7 @@ class GEN_Annotations: child3 = etree.SubElement(self.root, "source") child4 = etree.SubElement(child3, "annotation") - child4.text = "PASCAL VOC2007" + child4.text = "CDB" child5 = etree.SubElement(child3, "database") child5.text = "Unknown" @@ -57,16 +56,21 @@ class GEN_Annotations: ymaxn.text = str(ymax) -def negative_samples(): +def negative_samples(path_unit,path_raw): unit_list = os.listdir(path_unit) + random.shuffle(unit_list) + if len(unit_list) < 1000: + unit_list = unit_list + else: + unit_list = unit_list[:1000] unit_img_list = [] for unit_img in unit_list: img1 = Image.open(os.path.join(path_unit,unit_img)) unit_img_list.append(img1) raw_list = os.listdir(path_raw) - for raw_path in raw_list: - print(raw_list) - print("\n") + for raw_path in tqdm(raw_list): + # print(raw_list) + # print("\n") raw_img = Image.open(os.path.join(path_raw,raw_path)) unit_num = random.randint(0,10) x_left = random.randint(0,raw_img.size[0]-unit_img_list[unit_num].size[0]) @@ -74,15 +78,33 @@ def negative_samples(): x_right = x_left+unit_img_list[unit_num].size[0] y_right = y_left+unit_img_list[unit_num].size[1] raw_img.paste(unit_img_list[unit_num],(x_left,y_left,x_right,y_right)) - raw_img.save(os.path.join(path_raw,raw_path).replace("/NO/","/result/Images/")) + raw_img.save(os.path.join(path_raw,raw_path).replace("/NOObject/","/result/Images/")) filename = raw_path anno = GEN_Annotations(filename) anno.set_size(raw_img.size[0], raw_img.size[1], 3) anno.add_pic_attr("person", x_left, y_left, x_right, y_right) - anno.savefile(os.path.join(path_raw,raw_path).replace("/NO/","/result/Ground_truth/").replace(".jpg",".xml")) + anno.savefile(os.path.join(path_raw,raw_path).replace("/NOObject/","/result/Ground_truth/").replace(".jpg",".xml")) if __name__ == '__main__': + parser1 = argparse.ArgumentParser(description="CUI") + parser1.add_argument("path_unit", default='/data/DataManagement/Project/001_cuihu/CAS_CuihuBird/CMB_Cuihubird/FinalData/data', + type=str, + help='path to object unit') + parser1.add_argument("path_raw", default='/data/DataManagement/Project/001_cuihu/0009_20220507_RTSP/CDB_CuihuNobird/NOObject', type=str, + help='path to image to deal with') - negative_samples() \ No newline at end of file + + args = parser1.parse_args() + path_unit = args.path_unit + path_raw = args.path_raw + save_path1 = path_raw.replace("NOObject","result/Images/") + print(save_path1) + if not os.path.exists(save_path1): + os.makedirs(save_path1) + save_path2 = path_raw.replace("NOObject","result/Ground_truth/") + print(save_path2) + if not os.path.exists(save_path2): + os.makedirs(save_path2) + negative_samples(path_unit,path_raw) \ No newline at end of file