first commit

This commit is contained in:
Semi212 committed 2021-09-15 11:33:58 +08:00
commit 54ccfb11fe
218 files changed
+19525

No files matched your search

Executable
+941
View File
@@ -0,0 +1,941 @@
"""
密度估计
"""
import cv2
import json
import random
from configs.configs import model_head
from network import PKSNet
import os,sys
import torch
from PIL import Image
from vizer.draw import draw_boxes
from ssd.config import cfg
from ssd.data.datasets import VOCDataset
import numpy as np
from ssd.data.transforms import build_transforms
from ssd.modeling.detector import build_detection_model
from ssd.utils.checkpoint import CheckPointer
from creat_xml_fordetection import make_xml
from Dcl.models.LoadModel import MainModel
from Dcl.DclConfig import LoadConfig, load_data_transformers
from torch.autograd import Variable
import threading
from common import global_para as gp
import time
from numpy import random
root_path = os.getcwd()
sys.path.insert(0,root_path+"/Y5")
from Y5.models.experimental import attempt_load
from Y5.utils.datasets import letterbox
from Y5.utils.general import non_max_suppression, \
scale_coords
from Y5.utils.plots import plot_one_box
#np.set_printoptions(threshold=np.inf)
class Density:
def __init__(self, device, root):
self.device = device
self.root = root
self.net = PKSNet().to(self.device)
self.pth = "../data_MPKFNet/weigth/final.pth"
# print("creat model is ", (t4 - t3) * 1000)
# self.checkpointer = CheckPointer(self.net)
# self.net.load_state_dict(self.checkpointer['net'])
# self.checkpointer.load(self.checkpointer['net'])
checkpoint = torch.load(self.pth)
self.net.load_state_dict(checkpoint['net'])
if gp.DebugPrint_flag:
print("load model weight ok")
np.set_printoptions(threshold=np.inf)
def inference(self,image,imagetime,index):
rootpath = os.path.join(self.root, imagetime)
if not os.path.exists(rootpath):
os.mkdir(rootpath)
if not os.path.exists(rootpath + "/" + "Density"+"/"):
os.mkdir(rootpath + "/" + "Density"+"/")
target_h, target_w = image.shape[0], image.shape[1]
t1 = time.time()
image = cv2.resize(image, (1024, 768)) / 255.0
image = np.transpose(image, [2, 0, 1])
image = np.expand_dims(image, axis=0)
image = torch.tensor(image)
image = image.to(self.device)
t2 = time.time()
if gp.DebugPrint_flag:
print("Density data deal is ", (t2 - t1) * 1000)
# print("data dell is ", (t7 - t6) * 1000)
if model_head:
targets_pred, posture_pred, spice_pred = self.net(image)
else:
targets_pred = self.net(image)
t3=time.time()
if gp.DebugPrint_flag:
print("Density inference is ", (t3 - t2) * 1000)
targets_pred1 = targets_pred.cpu()
num = targets_pred1.data.sum().numpy()
targets_pred1 = targets_pred1.data.numpy()
t4=time.time()
if gp.DebugPrint_flag:
print("Density final is ", (t4 - t3) * 1000)
data_dict = {"sum": str(num), "density": (str(targets_pred1[0, 0, :, :]))}
json_outputpath = (rootpath + "/" + "Density"+"/"+str(index).zfill(3)+".json")
json_data = json.dumps(data_dict, ensure_ascii=True, indent=4)
with open (json_outputpath,'w',encoding='utf-8') as f:
f.write(str(json_data))
t5=time.time()
if gp.DebugPrint_flag:
print("Density save is ", (t5 - t4) * 1000)
def inference_init(self, image):
image = cv2.resize(image, (1024, 768)) / 255.0
image = np.transpose(image, [2, 0, 1])
image = np.expand_dims(image, axis=0)
image = torch.tensor(image)
image = image.to(self.device)
if model_head:
targets_pred, posture_pred, spice_pred = self.net(image)
else:
targets_pred = self.net(image)
targets_pred1 = targets_pred.cpu()
num = targets_pred1.data.sum().numpy()
targets_pred1 = targets_pred1.data.numpy()
if gp.DebugPrint_flag:
print("Density initing")
"""
目标检测
"""
class Detection_S:
def __init__(self,device,root):
self.device = device
self.root = root
cfg.merge_from_file("ssd/config/hat-512.yaml")
cfg.freeze()
self.net =build_detection_model(cfg).to(self.device)
self.checkpointer = CheckPointer(self.net, save_dir=cfg.OUTPUT_DIR)
self.ckpt = "../data_MPKFNet/weigth/detection/detection.pth"
self.checkpointer.load(self.ckpt, use_latest=self.ckpt is None)
self.transforms = build_transforms(cfg, is_train=False)
self.threshold = gp.detect_thresh
self.net.eval()
def inference(self,image,imagetime,index,fine_grained):
with torch.no_grad():
class_names = VOCDataset.class_names
rootpath = os.path.join(self.root, imagetime)
if not os.path.exists(rootpath):
os.mkdir(rootpath)
if not os.path.exists(rootpath + "/" + "Detection" + "/"):
os.mkdir(rootpath + "/" + "Detection" + "/")
image_or = image
height, width = image.shape[:2]
t1 = time.time()
image = self.transforms(image)[0].unsqueeze(0)
t2 = time.time()
if gp.DebugPrint_flag:
print("Detection data deal is ",(t2-t1)*1000)
result = self.net(image.to(self.device))[0]
t3 = time.time()
if gp.DebugPrint_flag:
print("Detection inference is ",(t3-t2)*1000)
result = result.to(torch.device("cpu"))
result = result.resize((width, height)).numpy()
t5 = time.time()
if gp.DebugPrint_flag:
print("Detection get result is ", (t5 - t3) * 1000)
boxes, labels, scores = result['boxes'], result['labels'], result['scores']
indices_score = (scores > self.threshold)
indices_label_left = labels <4
indices_label_right = labels >2
indices = []
for i in range(len(indices_score)):
indices.append(indices_score[i] and indices_label_left[i] and indices_label_right[i])
boxes = boxes[indices]
labels = labels[indices]
scores = scores[indices]
for i in range(len(boxes)):
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 = fine_grained.inference(image_or, boxes, labels, scores)
t4 = time.time()
if gp.DebugPrint_flag:
print("Detection final is ", (t4 - t3) * 1000)
#numb = 2
#fine_grained.inference(image_or,boxes,labels,scores)
# for box in boxes:
# img1 = image_or.copy()[int(box[1]):int(box[3]), int(box[0]):int(box[2]), :]
# cv2.imwrite(rootpath + "/" + "Detection" + "/"+"{}.jpg".format(numb), img1)
# numb+=1
#drawn_image = draw_boxes(image_or, boxes_fine, labels_fine, scores_fine).astype(np.uint8)
xml_outputpath = rootpath + "/" + "Detection"+"/" + index+".xml"
make_xml(str(index),image_or.shape[0],image_or.shape[1],boxes_fine,labels_fine,scores_fine,xml_outputpath,class_names)
#Image.fromarray(drawn_image).save(xml_outputpath.replace(".xml",".jpg"))
t5 = time.time()
if gp.DebugPrint_flag:
print("Detection save is ", (t5 - t4) * 1000)
def inference_Red(self,image,ImagePath,fine_grained):
with torch.no_grad():
class_names = VOCDataset.class_names
image_or = image
height, width = image.shape[:2]
t1 = time.time()
image = self.transforms(image)[0].unsqueeze(0)
t2 = time.time()
if gp.DebugPrint_flag:
print("data deal is ",(t2-t1)*1000)
result = self.net(image.to(self.device))[0]
t3 = time.time()
if gp.DebugPrint_flag:
print("inference is ",(t3-t2)*1000)
result = result.to(torch.device("cpu"))
result = result.resize((width, height)).numpy()
boxes, labels, scores = result['boxes'], result['labels'], result['scores']
indices_score = (scores > self.threshold)
indices_label_left = labels <4
indices_label_right = labels >2
indices = []
for i in range(len(indices_score)):
indices.append(indices_score[i] and indices_label_left[i] and indices_label_right[i])
boxes = boxes[indices]
for i in range(len(boxes)):
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
labels = labels[indices]
scores = scores[indices]
for i in range(len(labels)):
labels[i] = 99999
boxes_fine, labels_fine, scores_fine = fine_grained.inference(image_or, boxes, labels, scores)
t4 = time.time()
if gp.DebugPrint_flag:
print("final is ", (t4 - t3) * 1000)
#numb = 2
#fine_grained.inference(image_or,boxes,labels,scores)
# for box in boxes:
# img1 = image_or.copy()[int(box[1]):int(box[3]), int(box[0]):int(box[2]), :]
# cv2.imwrite(rootpath + "/" + "Detection" + "/"+"{}.jpg".format(numb), img1)
# numb+=1
#drawn_image = draw_boxes(image_or, boxes_fine, labels_fine, scores_fine).astype(np.uint8)
xml_outputpath = ImagePath.replace("/ftp/", "/ftp_Result/").replace(".JPG", ".xml").replace(".jpg",".xml")
make_xml(str(ImagePath),image_or.shape[0],image_or.shape[1],boxes_fine,labels_fine,scores_fine,xml_outputpath)
#Image.fromarray(drawn_image).save(xml_outputpath.replace(".xml",".jpg"))
t5 = time.time()
if gp.DebugPrint_flag:
print("save is ", (t5 - t4) * 1000)
def check_object(self,image):
flag = False
with torch.no_grad():
height, width = image.shape[:2]
img_temp = image
t1 = time.time()
image = self.transforms(image)[0].unsqueeze(0)
t2 = time.time()
if gp.DebugPrint_flag:
print("check datadeal is ",(t2-t1)*1000)
result = self.net(image.to(self.device))[0]
t3 = time.time()
if gp.DebugPrint_flag:
print("check inference is ", (t3 - t2) * 1000)
result = result.to(torch.device("cpu"))
t4 = time.time()
if gp.DebugPrint_flag:
print("check result deal is ", (t4 - t3) * 1000)
result = result.resize((width, height)).numpy()
boxes, labels, scores = result['boxes'], result['labels'], result['scores']
indices_score = (scores > self.threshold)
indices_label_left = labels <4
indices_label_right = labels >2
indices = []
for i in range(len(indices_score)):
indices.append(indices_score[i] and indices_label_left[i] and indices_label_right[i])
labels = labels[indices]
boxes = boxes[indices]
scores=scores[indices]
if 3 in labels:
flag = True
if gp.DebugPrint_flag:
print("boxes = ",boxes)
print("check flag is ",flag)
t5 = time.time()
if gp.DebugPrint_flag:
print("check final is ", (t5 - t4) * 1000)
return flag,labels,boxes,scores
class Detection_Y5:
def __init__(self,device,root):
self.device = device
self.root = root
self.img_size = 640
self.conf_thres = gp.detect_thresh
self.iou_thres = 0.4
self.stride = 1
if gp.red_flag:
self.weights = '../data_MPKFNet/weigth/detection/Y/best_r.pt'
else:
self.weights = '../data_MPKFNet/weigth/detection/Y/best.pt'
# self.device = select_device(self.device)
model = attempt_load(self.weights, map_location=self.device)
model.to(self.device).eval()
model.half()
self.model = model
self.names = model.module.names if hasattr(
model, 'module') else model.names
def preprocess(self, img):
img0 = img.copy()
#img = cv2.resize(img, (1024, 768))
img = letterbox(img, new_shape=self.img_size, stride=self.stride)[0]
img = img[:, :, ::-1].transpose(2, 0, 1)
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(self.device)
img = img.half() # °ë¾«¶È
img /= 255.0 # ͼÏñ¹éÒ»»¯
if img.ndimension() == 3:
img = img.unsqueeze(0)
return img0, img
def inference(self,image,imagetime,index,fine_grained):
with torch.no_grad():
rootpath = os.path.join(self.root, imagetime)
if not os.path.exists(rootpath):
os.mkdir(rootpath)
if not os.path.exists(rootpath + "/" + "Detection" + "/"):
os.mkdir(rootpath + "/" + "Detection" + "/")
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)
#cichuxuyaoxiugai ruguo meiyoujiance daomubiao jiuhuibaocuo
if len(labels) > 0:
indices_label_left = labels < 4
indices_label_right = labels > 2
indices = []
for i in range(len(indices_label_left)):
indices.append(indices_label_left[i] and indices_label_right[i])
boxes = boxes[indices]
labels = labels[indices]
scores = scores[indices] # 因为莫名奇妙多了一个list维度,所以加了这个0
boxes = boxes[0]
labels = labels[0]
scores = scores[0]
ratio_x = (3840.0 / 1024.0)
ratio_y = (2160.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] = 99999
boxes_fine, labels_fine, scores_fine = fine_grained.inference(image_or, boxes, labels, scores)
t4 = time.time()
if gp.DebugPrint_flag:
print("Detection final is ", (t4 - t3) * 1000)
else:
boxes_fine, labels_fine, scores_fine = [],[],[]
if gp.DebugPrint_flag:
print("Detection is nothong")
#numb = 2
#fine_grained.inference(image_or,boxes,labels,scores)
# for box in boxes:
# img1 = image_or.copy()[int(box[1]):int(box[3]), int(box[0]):int(box[2]), :]
# cv2.imwrite(rootpath + "/" + "Detection" + "/"+"{}.jpg".format(numb), img1)
# numb+=1
#drawn_image = draw_boxes(image_or, boxes_fine, labels_fine, scores_fine).astype(np.uint8)
#labels_fine = labels_fine.numpy()
t6 = time.time()
xml_outputpath = rootpath + "/" + "Detection"+"/" + index+".xml"
make_xml(str(index),image_or.shape[0],image_or.shape[1],boxes_fine,labels_fine,scores_fine,xml_outputpath)
#Image.fromarray(drawn_image).save(xml_outputpath.replace(".xml",".jpg"))
t5 = time.time()
if gp.DebugPrint_flag:
print("Detection save is ", (t5 - t6) * 1000)
def inference_Red(self,image,ImagePath,fine_grained):
with torch.no_grad():
image_or = image
height, width = image.shape[:2]
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:
indices_label_left = labels < 4
indices_label_right = labels > 2
indices = []
for i in range(len(indices_label_left)):
indices.append(indices_label_left[i] and indices_label_right[i])
boxes = boxes[indices]
labels = labels[indices]
scores = scores[indices] # 因为莫名奇妙多了一个list维度,所以加了这个0
boxes = boxes[0]
labels = labels[0]
scores = scores[0]
ratio_x = (width / 1024.0)
ratio_y = (height / 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][0] > 3840:
boxes[i][0] = 3839
if boxes[i][1] < 0:
boxes[i][1] = 0
if boxes[i][1] > 2160:
boxes[i][1] = 2159
if boxes[i][2] < 0:
boxes[i][2] = 0
if boxes[i][2]> 3840:
boxes[i][2] = 3839
if boxes[i][3] < 0:
boxes[i][3] = 0
if boxes[i][3] > 2160:
boxes[i][3] = 2159
for i in range(len(labels)):
labels[i] = 99999
boxes_fine, labels_fine, scores_fine = fine_grained.inference(image_or, boxes, labels, scores)
t4 = time.time()
if gp.DebugPrint_flag:
print("Detection final is ", (t4 - t3) * 1000)
t6 = time.time()
xml_outputpath = ImagePath.replace("/ftp/", "/ftp_Result/").replace(".JPG", ".xml").replace(".jpg",
".xml")
make_xml(str(ImagePath), image_or.shape[0], image_or.shape[1], boxes_fine, labels_fine, scores_fine,
xml_outputpath)
# Image.fromarray(drawn_image).save(xml_outputpath.replace(".xml",".jpg"))
t5 = time.time()
if gp.DebugPrint_flag:
print("Detection save is ", (t5 - t6) * 1000)
else:
boxes_fine, labels_fine, scores_fine = [], [], []
if gp.DebugPrint_flag:
print("Detection is nothong")
def check_object(self,img_o):
flag = False
with torch.no_grad():
# path = '/data/Hatcher/ouput1/0001_20210414151147/RawImage/0099.jpg'
# img = cv2.imread(path)
# img = cv2.resize()
height, width = img_o.shape[:2]
img = cv2.resize(img_o,(1024,768))
t1 = time.time()
image_scale, image = self.preprocess(img)
t2 = time.time()
if gp.DebugPrint_flag:
print("Check 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("Check 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("Check get result is ", (t5 - t3) * 1000)
if len(labels) > 0:
indices_label_left = labels < 4
indices_label_right = labels > 2
indices = []
for i in range(len(indices_label_left)):
indices.append(indices_label_left[i] and indices_label_right[i])
boxes = boxes[indices]
labels = labels[indices]
scores = scores[indices] # 因为莫名奇妙多了一个list维度,所以加了这个0
boxes = boxes[0]
labels = labels[0]
scores = scores[0]
ratio_x = (width / 1024.0)
ratio_y = (height / 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][0] > 3840:
boxes[i][0] = 3839
if boxes[i][1] < 0:
boxes[i][1] = 0
if boxes[i][1] > 2160:
boxes[i][1] = 2159
if boxes[i][2] < 0:
boxes[i][2] = 0
if boxes[i][2] > 3840:
boxes[i][2] = 3839
if boxes[i][3] < 0:
boxes[i][3] = 0
if boxes[i][3] > 2160:
boxes[i][3] = 2159
else:
pass
if 3 in labels:
flag = True
if gp.DebugPrint_flag:
print("boxes = ",boxes)
print("check flag is ",flag)
return flag,labels,boxes,scores
"""
细粒度识别
"""
class args_temp():
dataset = 'HCData'
backbone = 'resnet50'
batch_size = 1
num_workers = 8
version = 'val'
resize_resolution = 512
crop_resolution = 448
save_suffix = None
swap_num = [7,7]
class Fine_grained:
def __init__(self,device,root):
self.device = device
self.root = root
self.DclPreWeightPath = '../data_MPKFNet/weigth/FinedGrainedRecognition/Test/weights_19_499_0.9998_1.0000.pth'
# parser = argparse.ArgumentParser(description='dcl parameters')
# parser.add_argument('--data', dest='dataset', default='HCData', type=str)
# parser.add_argument('--backbone', dest='backbone', default='resnet50', type=str)
# parser.add_argument('--b', dest='batch_size', default=1, type=int)
# parser.add_argument('--nw', dest='num_workers', default=8, type=int)
# parser.add_argument('--ver', dest='version', default='val', type=str)
# parser.add_argument('--size', dest='resize_resolution', default=512, type=int)
# parser.add_argument('--crop', dest='crop_resolution', default=448, type=int)
# parser.add_argument('--ss', dest='save_suffix', default=None, type=str)
# parser.add_argument('--acc_report', dest='acc_report', action='store_true')
# parser.add_argument('--swap_num', default=[7, 7], nargs=2, metavar=('swap1', 'swap2'), type=int,
# help='specify a range')
self.args = args_temp()
self.Config = LoadConfig(self.args, 's')
self.transformers = load_data_transformers(self.args.resize_resolution, self.args.crop_resolution, self.args.swap_num)
self.net = MainModel(self.Config).to(device)
model_dict = self.net.state_dict()
pretrained_dict = torch.load(self.DclPreWeightPath)
model_dict.update(pretrained_dict)
self.net.load_state_dict(model_dict)
if gp.DebugPrint_flag:
print("load model weight is ok")
self.net.train(False)
def inference(self,img,imagelist,labellist,scorelist):
with torch.no_grad():
for i,box in enumerate(imagelist):
Img = img.copy()[int(box[1]):int(box[3]), int(box[0]):int(box[2]), :]
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()
if gp.DebugPrint_flag:
print("fine data deal is ", (t2 - t1) * 1000)
outputs = self.net(inputs)
t3 = time.time()
if gp.DebugPrint_flag:
print("fine inference is ", (t3 - t2) * 1000)
top3_val, top3_pos = torch.topk(outputs[0], 3) # topk返回的是一个元组,第一个元素是每一行最大的三个值(默认是按行取最大值),第二个元素是每一行最大的值的排序
Species = top3_pos.cpu().numpy()[0][0]
ConfidenceLevel = top3_val.cpu().numpy()[0][0]
ConfidenceLevel = ConfidenceLevel/26.0
if ConfidenceLevel >1:
ConfidenceLevel = 1.0* random.uniform(0, 1)
if gp.DebugPrint_flag:
print("hhhhhhhhhhhhhhhhhhhhhhh is ",ConfidenceLevel)
labellist[i] = Species
scorelist[i] = ConfidenceLevel
t4 = time.time()
if gp.DebugPrint_flag:
print("fine data final is ", (t4 - t3) * 1000)
return imagelist, labellist, scorelist
class Check_start (threading.Thread):
def __init__(self, threadID, name, img,DetectionClass):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.img = img
self.DetectionClass = DetectionClass
def run(self):
if gp.DebugPrint_flag:
print(self.name + " is start")
gp.is_ex_bird ,labels, boxes ,scores= self.DetectionClass.check_object(self.img)
if gp.is_ex_bird :
if gp.DebugPrint_flag:
print(gp.is_ex_bird)
gp.image_time = gp.Camera_device +time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
if gp.DebugPrint_flag:
print(self.name + " is finish")
class Check_start_fordataselect (threading.Thread):
def __init__(self, threadID, name, img,DetectionClass,fine_grained):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.img = img
self.DetectionClass = DetectionClass
self.fine_grained = fine_grained
def run(self):
if gp.DebugPrint_flag:
print(self.name + " is start")
is_ex_bird ,labels, boxes ,scores= self.DetectionClass.check_object(self.img)
image_or = self.img.copy()
if is_ex_bird :
for i in range(len(boxes)):
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 = self.fine_grained.inference(self.img, boxes, labels, scores)
for i in range(len(scores)):
xyxy = boxes_fine[i]
conf = scores_fine[i]
cls = labels_fine[i]
label = f'{int(cls)} {conf:.2f}'
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, self.img, label=label, color=colors, line_thickness=1)
cv2.imwrite("../data_MPKFNet/RTSP/" + "{}.jpg".format(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))),
image_or)
cv2.imwrite(
"../data_MPKFNet/RTSP/result/" + "{}.jpg".format(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))),
self.img)
if gp.DebugPrint_flag:
print(self.name + " is finish")
class Density_mul (threading.Thread):
def __init__(self, threadID, name,imagetime,DensityClass):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.imagetime = imagetime
self.DensityClass = DensityClass
def run(self):
gp.Density_thread_flag = True
if gp.DebugPrint_flag:
print(self.name +self.imagetime," is starting")
i = 0
while (i<=29):
# if(len(gp.ImgDealQueue)<=i):
# gp.event.wait()
# if ((i+1)%25==0): #说明是第25*n帧,间隔1s处理一次
if gp.DebugPrint_flag:
print("Density is1 ", len(gp.ImgDealQueue), " ", i)
print("Density is waiting")
if (len(gp.ImgDealQueue) <= i):
gp.eventDensity.wait()
if gp.DebugPrint_flag:
print("Density is2 ", len(gp.ImgDealQueue), " ", i)
print("Density is not waiting")
self.DensityClass.inference(gp.ImgDealQueue[i],self.imagetime,str(i).zfill(4))
gp.eventDensity.clear()
i += 1
gp.Density_thread_flag = False
gp.is_ex_bird = False
gp.destory_image = True
if gp.DebugPrint_flag:
print(self.name + self.imagetime," is over")
class Detecntion_mul (threading.Thread):
def __init__(self, threadID, name,imagetime,DetectionClass,Fine_grainedClass):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.imagetime = imagetime
self.DetectionClass = DetectionClass
self.Fine_grainedClass = Fine_grainedClass
def run(self):
gp.Detection_thread_flag = True
if gp.DebugPrint_flag:
print(self.name ,self.imagetime," is starting")
i = 0
while (i<=29):
if gp.DebugPrint_flag:
print("Detection is1 ",len(gp.ImgDealQueue)," ",i)
# if (len(gp.ImgDealQueue) <= i):
# gp.event.wait()
# if ((i+1)%25==0): #说明是第25*n帧,间隔1s处理一次
print("Detection is waiting")
if (len(gp.ImgDealQueue) <= i):
gp.eventDetection.wait()
if gp.DebugPrint_flag:
print("Detection is2 ", len(gp.ImgDealQueue), " ", i)
print("Detection is not waiting")
self.DetectionClass.inference(gp.ImgDealQueue[i],self.imagetime,str(i).zfill(4),self.Fine_grainedClass)
gp.eventDetection.clear()
i+=1
gp.Detection_thread_flag = False
gp.is_ex_bird = False
gp.destory_image = True
if gp.DebugPrint_flag:
print(self.name ,self.imagetime," is over")
class SaveImage_mul (threading.Thread):
def __init__(self, threadID, name,imagetime,root):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.imagetime = imagetime
self.root = root
self.rootpath = os.path.join(self.root, self.imagetime)
def run(self):
gp.SaveImage_thread_flag = True
if not os.path.exists(self.rootpath):
os.mkdir(self.rootpath)
if not os.path.exists(self.rootpath + "/" + "RawImage" + "/"):
os.mkdir(self.rootpath + "/" + "RawImage" + "/")
if gp.DebugPrint_flag:
print(self.name ,self.imagetime," is starting")
i = 0
while (i<=29):
# if(len(gp.ImgDealQueue)<=i):
# gp.event.wait()
# if ((i+1)%25==0): #说明是第25*n帧,间隔1s处理一次
if gp.DebugPrint_flag:
print("Saveimage is1 ", len(gp.ImgDealQueue), " ", i)
print("Saveimage is waiting")
if (len(gp.ImgDealQueue) <= i):
gp.eventSaveimage.wait()
if gp.DebugPrint_flag:
print("Saveimage is2 ", len(gp.ImgDealQueue), " ", i)
print("Saveimage is not waiting")
cv2.imwrite(os.path.join(self.rootpath,"RawImage","{}.jpg".format(str(i).zfill(4))),gp.ImgDealQueue[i])
gp.eventSaveimage.clear()
if gp.DebugPrint_flag:
print("Saveimage finish once")
i+=1
gp.SaveImage_thread_flag = False
gp.is_ex_bird = False
gp.destory_image = True
if gp.DebugPrint_flag:
print(self.name ,self.imagetime," is over")
class ThreadMonitor_mul (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
if gp.DebugPrint_flag:
print(self.name," is starting")
while 1:
# print(gp.Density_thread_flag,gp.Detection_thread_flag,gp.SaveImage_thread_flag)
if (gp.Density_thread_flag) == False and (gp.Detection_thread_flag == False) and (gp.SaveImage_thread_flag == False): #说明三个进程都已经结束了
gp.ImgDealQueue = []
class isopenmul (threading.Thread):
def __init__(self, threadID, name,DetectionClass):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.DetectionClass = DetectionClass
def run(self):
while 1:
# print(gp.Density_thread_flag,gp.Detection_thread_flag,gp.SaveImage_thread_flag)
if (gp.Vedio_thread_flag == False) and (gp.Density_thread_flag == False) and (
gp.Detection_thread_flag == False) and (gp.SaveImage_thread_flag == False) and (
gp.is_ex_bird == False) and gp.num_detection > 24:
gp.num_detection = 0
if gp.DebugPrint_flag:
print( "check is start")
gp.is_ex_bird, labels, boxes, scores = self.DetectionClass.check_object(gp.frame_openmul)
if gp.is_ex_bird:
if gp.DebugPrint_flag:
print(gp.is_ex_bird)
gp.image_time = gp.Camera_device + time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
if gp.DebugPrint_flag:
print("check is finish")
# Check_start1 = Check_start("0002", "check_start", gp.frame_openmul, self.DetectionClass)
# Check_start1.start()
if __name__ == '__main__':
t1 = time.time()
image = cv2.imread("/home/wanghc/output1/0001_20210413155842/RawImage/0174.jpg")
t2 = time.time()
camera_num = "0001"
image_time = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
image_time = camera_num + image_time
root = "/home/wanghc/output"
Density1 = Density(gp.devices,root)
Detection1 = Detection_Y5(gp.devices,root)
Fine_grained1 = Fine_grained(gp.devices,root)
os.environ['CUDA_VISIBLE_DEVICES'] = '4'
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
path = '/home/wanghc/output1/001020210509163621/RawImage/0000.jpg'
img = cv2.imread(path) # BGR
Detection1.inference(img, image_time, str(1), Fine_grained1)
# root = "/home/wanghc/output"
# detector = Detection_Y5(gp.devices, root)
# print("ok")
# path = '/home/wanghc/output1/001020210509163621/RawImage/0000.jpg'
# img = cv2.imread(path) # BGR
# img_show = img#cv2.resize(img, (1024, 768))
# names = detector.model.module.names if hasattr(detector.model, 'module') else detector.model.names
# colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
# flag, labels,pred_boxes, scores = detector.check_object(img_show)
# print(len(scores))
# #img_show = cv2.resize(img, (768, 1024))
# for i in range(len(scores)):
# xyxy = pred_boxes[i]
# conf = scores[i]
# cls = labels[i]
# label = f'{names[int(cls)]} {conf:.2f}'
# plot_one_box(xyxy, img_show, label=label, color=colors[int(cls)], line_thickness=1)
# cv2.imshow(str(path), img_show)
# t1 = time.time()
# cv2.imwrite("./a.jpg", img_show)
# t2 = time.time()
# print((t2 - t1) * 1000)
# cv2.waitKey(-1)
# while(1):
# pass