def __init__(self): # load weights and set defaults self.config_path = 'config/yolov3.cfg' self.weights_path = 'config/yolov3.weights' self.class_path = 'config/coco.names' self.img_size = 416 self.conf_thres = 0.8 self.nms_thres = 0.4 self.horizontalDividingLine = 800 self.personIn = 0 self.personOut = 0 # load model and put into eval mode self.model = Darknet(self.config_path, img_size=self.img_size) self.model.load_weights(self.weights_path) self.model.cuda() self.model.eval() self.classes = utils.load_classes(self.class_path) self.Tensor = torch.cuda.FloatTensor # self.Tensor = torch.FloatTensor self.mot_tracker = Sort() self.classes = utils.load_classes(self.class_path) self.persons = dict() self.heads = dict() config_file = './config/head_detect_1gpu_e2e_faster_rcnn_R-50-FPN_2x.yaml' weights_file = './config/model_iter99999.aug.pkl' self.m_det = det_model(config_file, weights_file, 1) print('init finished')
def create_dataset(opt, model_cfg): data_config = parse_data_config(opt.data) train_path = data_config["train"] valid_path = data_config["valid"] class_names = load_classes(data_config["names"]) train_loader, valid_loader, test_loader = None, None, None train_dataset = ListDataset(train_path, img_size=int(model_cfg[0]['width']), augment=True, multiscale=opt.multiscale_training) train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=opt.batch_size, shuffle=True, num_workers=4, pin_memory=True, collate_fn=train_dataset.collate_fn, ) #import pdb;pdb.set_trace() valid_dataset = ListDataset(train_path, img_size=int(model_cfg[0]['width']), augment=False, multiscale=False) valid_loader = torch.utils.data.DataLoader( valid_dataset, batch_size=opt.batch_size, shuffle=False, num_workers=4, collate_fn=valid_dataset.collate_fn) test_loader = valid_loader return class_names, train_loader, valid_loader, test_loader
def init(): global model, cuda, classes, img_size global use_cuda, conf_thres, nms_thres try: model_path = Model.get_model_path('YOLOv3') except: model_path = 'model' config_path = os.path.join(model_path, 'yolov3.cfg') class_path = os.path.join(model_path, 'coco.names') weights_path = os.path.join(model_path, 'yolov3.weights') params_path = os.path.join(model_path, 'params.json') # get paramters and model params = load_params(params_path) img_size = params['ImageSize'] conf_thres = params['Confidence'] nms_thres = params['NonMaxSuppression'] use_cuda = params['cuda'] classes = load_classes(class_path) model = Darknet(config_path, img_size=img_size) model.load_weights(weights_path) cuda = torch.cuda.is_available() and use_cuda if cuda: model.cuda() # Set in evaluation mode model.eval()
def extract_samples(src, dest, targets=[]): train_fd = open(os.path.join(dest, "train.txt"), "w") val_fd = open(os.path.join(dest, "val.txt"), "w") classes = load_classes("./data/coco.names") if len(targets) == 0: targets = [str(i) for i in range(80)] else: targets = [str(classes.index(_)) for _ in targets] for dataset, record_f in zip(["train2014", "val2014"], [train_fd, val_fd]): os.makedirs(os.path.join(dest, dataset, "labels"), exist_ok=True) os.makedirs(os.path.join(dest, dataset, "images"), exist_ok=True) for f in os.listdir("%s/%s" % (src, dataset)): label_file = os.path.join(src, dataset, f) with open(label_file, "r") as fd: lines = fd.readlines() annos = [] for line in lines: cls, x, y, w, h = line.strip().split(" ") if cls in targets: annos.append(line) if len(annos) > 0: dest_label = os.path.join(dest, dataset, "labels", f) with open(dest_label, "w") as fw: for line in annos: fw.write(line) src_img = label_file.replace("labels", "images").replace(".txt", ".jpg") dest_img = dest_label.replace("labels", "images").replace( ".txt", ".jpg") shutil.copy(src_img, dest_img) record_f.write(dest_label)
def test(img_path: str = 'data/custom/images/', anno_path: str = 'data/custom/annos/') -> None: # transform something initialize(img_path=img_path, anno_path=anno_path, split_ratio=1.0) # some common config iou_thres = 0.5 conf_thres = 0.01 nms_thres = 0.5 img_size = 416 batch_size = 16 device = tc.device('cuda' if tc.cuda.is_available() else 'cpu') # other paths weights_path = 'my/yolov3_ckpt_1.pth' model_def = 'config/custom.cfg' test_path = 'data/custom/train.txt' class_path = 'data/custom/classes.names' # load model class_names = load_classes(class_path) model = Darknet(model_def).to(device) model.load_state_dict(tc.load(weights_path)) imgs, img_detections = detect( model=model, path=img_path, conf_thres=conf_thres, nms_thres=nms_thres, img_size=img_size, batch_size=batch_size, n_cpu=8, device=device, ) os.makedirs('predicted_file', exist_ok=True) class1 = open('predicted_file/det_test_core.txt', 'w') class2 = open('predicted_file/det_test_coreless.txt', 'w') for path, boxes in zip(imgs, img_detections): w, h = Image.open(path).size boxes = rescale_boxes(boxes, img_size, (h, w)) for box in boxes: line = [ # os.path.split(path)[1].split('_')[1].split('.')[0], # no prefix os.path.split(path)[1].split('.')[0], # with prefix 'core_' or 'coreless_' f'{box[4].tolist():.3f}', # conf f'{box[0].tolist():.1f}', f'{box[1].tolist():.1f}', f'{box[2].tolist():.1f}', f'{box[3].tolist():.1f}', ] if box[-1] == 0.0: class1.write(' '.join(line) + '\n') elif box[-1] == 1.0: class2.write(' '.join(line) + '\n') class1.close() class2.close() print('Output file saved.\n')
def main(): device = torch.device("cuda" if OPT.use_cuda else "cpu") cudnn.benchmark = True classes = tools.load_classes(OPT.class_path) # Extracts class labels from file model = Darknet(OPT.config_path, img_size=OPT.img_size) model.load_weights(OPT.weights_path) if OPT.source == 'image': detect_image(model, device, classes) elif OPT.source == 'video': detect_video(model, device, classes)
def __init__(self, model_path, img_size=416, non_maximum_suppression=None): self.img_size = img_size self.config_path = os.path.join(model_path, 'yolov3.cfg') self.weights_path = os.path.join(model_path, 'yolov3.weights') self.class_path = os.path.join(model_path, 'coco.names') self.non_maximum_suppression = non_maximum_suppression self.model = Darknet(self.config_path, img_size=img_size) self.model.load_weights(self.weights_path) self.model.eval() self.classes = utils.load_classes(self.class_path)
def run_single_detect(model, images, img_size, conf_thres=0.3, nms_thres=0.45): device = torch_utils.select_device() dataloader = LoadImages(images, img_size=img_size) classes = load_classes(parse_data_cfg('cfg/coco.data')['names']) for i, (path, img, im0) in enumerate(dataloader): img = torch.from_numpy(img).unsqueeze(0).to(device) pred = model(img) pred = pred[pred[:, :, 4] > conf_thres] # remove boxes < threshold if len(pred) > 0: # Run NMS on predictions detections = non_max_suppression(pred.unsqueeze(0), conf_thres, nms_thres)[0] # Print results to screen unique_classes = detections[:, -1].cpu().unique() for c in unique_classes: n = (detections[:, -1].cpu() == c).sum() print('%g %ss' % (n, classes[int(c)]), end=', ')
def __init__(self, data_config, net_config, weights, image_size): """Class init function.""" QtCore.QThread.__init__(self) self.image_list = [] self.threshold = 0.9 self.image_directory = '' self.data = None self.image_size = image_size if torch.cuda.is_available(): self.device = torch.device('cuda:0') else: self.device = torch.device('cpu') self.data_config = parse_data_config(data_config) # Extracts class labels from file self.classes = load_classes(self.data_config['names']) self.model = Darknet(net_config, image_size) checkpoint = torch.load(weights, map_location='cpu') self.model.load_state_dict(checkpoint['model'])
def cloth_train_data(train_path=None, valid_path = None, test_path=None): ### class2indice={} names = load_classes(class_name_path) for i, name in enumerate(names): class2indice[name] = i ### if train_path is not None and os.path.exists(train_path): xml2txt(train_path, train_save_path, class2indice) else: print("train path %s not exists !" %(train_path)) if valid_path is not None and os.path.exists(valid_path): xml2txt(valid_path, valid_save_path, class2indice) else: print("valid path %s not exists !" % (valid_path)) if test_path is not None and os.path.exists(valid_path): xml2txt(test_path, test_save_path, class2indice) else: print("test path %s not exists !" % (test_path)) print('done')
def kitti(setname): kitti2coco = { 'Car': 'car', 'Van': 'car', 'Truck': 'truck', 'Pedestrian': 'person', 'Person_sitting': 'person', 'Cyclist': 'person', 'Tram': 'train' } # frames = glob.glob(os.path.join('/home/fremont/ford/kitti/training/yolo/kitti_ood/labels', '*.txt')) # IMAGE_PATH = '/home/fremont/ford/kitti/training/yolo/kitti_ood/images' frames = glob.glob( os.path.join('/home/fremont/ford/kitti/training/yolo/labels', '*.txt')) IMAGE_PATH = '/home/fremont/ford/kitti/training/yolo/images' # frames = glob.glob(os.path.join('/home/fremont/ford/yolov3/data/coco/labels/val2017ood', '*.txt')) # IMAGE_PATH = '/home/fremont/ford/yolov3/data/coco/images/val2017ood' column_names = ['image', 'class', 'x1', 'y1', 'x2', 'y2'] class_names = set(load_classes('data/coco.names')) classes = os.listdir('kitti_out_cam'.format(setname)) classes.sort() for c in classes: column_names.append(c + '_cam_max') column_names.append(c + '_diff_max') column_names.append(c + '_grad_max') column_names.append(c + '_temp_max') column_names.append(c + '_cam_median') column_names.append(c + '_diff_median') column_names.append(c + '_grad_median') column_names.append(c + '_temp_median') column_names.append(c + '_cam_mean') column_names.append(c + '_diff_mean') column_names.append(c + '_grad_mean') column_names.append(c + '_temp_mean') data = [] counter = 0 for frame in tqdm.tqdm(frames): labels = np.genfromtxt(frame, delimiter=" ", dtype=np.float) if labels.ndim == 1: labels = np.expand_dims(labels, axis=0) image_name = os.path.basename(frame).replace('txt', 'png') img = cv2.imread(os.path.join(IMAGE_PATH, image_name)) H, W, C = img.shape matname = image_name.replace('png', 'mat') for label in labels: class_num = int(label[0]) if class_num >= 0: class_name = coco_names[class_num] else: class_name = 'ood' print('ood') bbox = label[1:].astype(float) bbox[[0, 2]] *= W bbox[[1, 3]] *= H x1, x2 = int(bbox[0] - bbox[2] / 2), int(bbox[0] + bbox[2] / 2) y1, y2 = int(bbox[1] - bbox[3] / 2), int(bbox[1] + bbox[3] / 2) row = [image_name, class_name, x1, y1, x2, y2] for name in classes: cam_mat = loadmat( os.path.join('kitti_out_cam'.format(setname), name, matname))['cam'] diff_mat = loadmat( os.path.join('kitti_out_diff'.format(setname), name, matname))['cam'] grad_mat = loadmat( os.path.join('kitti_out_gradient'.format(setname), name, matname))['cam'] temp_mat = loadmat( os.path.join('kitti_out_tempcam'.format(setname), name, matname))['cam'] cam_bbox = cam_mat[y1:y2, x1:x2] diff_bbox = diff_mat[y1:y2, x1:x2] grad_bbox = grad_mat[y1:y2, x1:x2] temp_bbox = temp_mat[y1:y2, x1:x2] cam_max = np.max(cam_bbox) diff_max = np.max(diff_bbox) grad_max = np.max(grad_bbox) temp_max = np.max(temp_bbox) row.append(cam_max) row.append(diff_max) row.append(grad_max) row.append(temp_max) cam_median = np.percentile(cam_bbox, 50) diff_median = np.percentile(diff_bbox, 50) grad_median = np.percentile(grad_bbox, 50) temp_median = np.percentile(temp_bbox, 50) row.append(cam_median) row.append(diff_median) row.append(grad_median) row.append(temp_median) cam_mean = np.mean(cam_bbox) diff_mean = np.mean(diff_bbox) grad_mean = np.mean(grad_bbox) temp_mean = np.mean(temp_bbox) row.append(cam_mean) row.append(diff_mean) row.append(grad_mean) row.append(temp_mean) data.append(row) df = pd.DataFrame(data, columns=column_names, dtype=float) df.to_csv('kitti_cams.csv'.format(setname)) print(counter / len(data))
"--save_video", type=bool, default=False, help="Set this flag to True if you want to record video") parser.add_argument("--split", type=str, default="test", help="text file having image lists in dataset") parser.add_argument("--folder", type=str, default="training", help="directory name that you downloaded all dataset") opt = parser.parse_args() print(opt) classes = utils.load_classes(opt.class_path) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Set up model model = Darknet(opt.model_def, img_size=opt.img_size).to(device) # Load checkpoint weights model.load_state_dict(torch.load(opt.weights_path)) # Eval mode model.eval() dataset = KittiYOLO2WayDataset(cnf.root_dir, split=opt.split, folder=opt.folder) data_loader = torch_data.DataLoader(dataset, 1, shuffle=False) Tensor = torch.cuda.FloatTensor if torch.cuda.is_available(
def __next__(self): self.count += 1 if self.count == self.nB: raise StopIteration ia = self.count * self.batch_size ib = min((self.count + 1) * self.batch_size, self.nF) multi_scale = False if multi_scale and self.augment: # Multi-Scale YOLO Training height = random.choice(range(10, 20)) * 32 # 320 - 608 pixels else: # Fixed-Scale YOLO Training height = self.height img_all = [] labels_all = [] for index, files_index in enumerate(range(ia, ib)): img_path = self.img_files[self.shuffled_vector[files_index]] label_path = self.label_files[self.shuffled_vector[files_index]] img = cv2.imread(img_path) # BGR if img is None: print('nooooooooooimages') continue augment_hsv = True if self.augment and augment_hsv: # SV augmentation by 50% fraction = 0.50 img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) S = img_hsv[:, :, 1].astype(np.float32) V = img_hsv[:, :, 2].astype(np.float32) a = (random.random() * 2 - 1) * fraction + 1 S *= a if a > 1: np.clip(S, a_min=0, a_max=255, out=S) a = (random.random() * 2 - 1) * fraction + 1 V *= a if a > 1: np.clip(V, a_min=0, a_max=255, out=V) img_hsv[:, :, 1] = S.astype(np.uint8) img_hsv[:, :, 2] = V.astype(np.uint8) cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) h, w, _ = img.shape img, ratio, padw, padh = resize_square(img, height=height, color=(127.5, 127.5, 127.5)) # Load labels name_classes = load_classes('/home/jiaxiang/myproject/yolov3/cfg/duration.names') if os.path.isfile(label_path): labels0 = np.loadtxt(label_path, dtype=np.float32).reshape(-1, 6) # Normalized xywh to pixel xyxy format labels = labels0.copy() labels[:, 1] = ratio * w * (labels0[:, 1] - labels0[:, 3] / 2) + padw labels[:, 2] = ratio * h * (labels0[:, 2] - labels0[:, 4] / 2) + padh labels[:, 3] = ratio * w * (labels0[:, 1] + labels0[:, 3] / 2) + padw labels[:, 4] = ratio * h * (labels0[:, 2] + labels0[:, 4] / 2) + padh durations = [] for i in labels0[:,5]: if float(i)==float(1): durations.append(9) elif str(i) not in name_classes and str(i)!='0.0': durations.append(6) else: for idx,j in enumerate(name_classes): if float(i)== float(j): durations.append(idx) labels[:, 5] = durations else: labels = np.array([]) # Augment image and labels if self.augment: img, labels, M = random_affine(img, labels, degrees=(-3, 3), translate=(0.1, 0.1), scale=(0.9, 1.1)) plotFlag = False if plotFlag: import matplotlib.pyplot as plt plt.figure(figsize=(10, 10)) if index == 0 else None plt.subplot(4, 4, index + 1).imshow(img[:, :, ::-1]) plt.plot(labels[:, [1, 3, 3, 1, 1]].T, labels[:, [2, 2, 4, 4, 2]].T, '.-') plt.axis('off') nL = len(labels) if nL > 0: # convert xyxy to xywh labels[:, 1:5] = xyxy2xywh(labels[:, 1:5].copy()) / height if self.augment: # random left-right flip lr_flip = True if lr_flip & (random.random() > 0.5): img = np.fliplr(img) if nL > 0: labels[:, 1] = 1 - labels[:, 1] # random up-down flip ud_flip = False if ud_flip & (random.random() > 0.5): img = np.flipud(img) if nL > 0: labels[:, 2] = 1 - labels[:, 2] img_all.append(img) labels_all.append(torch.from_numpy(labels)) # Normalize assert len(img_all)!=0 img_all = np.stack(img_all)[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB and cv2 to pytorch img_all = np.ascontiguousarray(img_all, dtype=np.float32) # img_all -= self.rgb_mean # img_all /= self.rgb_std img_all /= 255.0 return torch.from_numpy(img_all), labels_all
def bdd(): JSON_FILE = os.path.join('data', 'bdd100k', 'labels', 'bdd100k_labels_images_val.json') frames = json.load(open(JSON_FILE, 'r')) column_names = [ 'image', 'class', 'x1', 'y1', 'x2', 'y2', 'height', 'width', 'area', 'occluded', 'truncated', 'weather', 'timeofday' ] class_names = set(load_classes('data/coco.names')) classes = os.listdir('bdd_out_cam') classes.sort() for c in classes: column_names.append(c + '_cam_max') column_names.append(c + '_diff_max') column_names.append(c + '_cam_mean') column_names.append(c + '_diff_mean') data = [] for frame in tqdm.tqdm(frames): image_name = frame['name'] weather = frame['attributes']['weather'] timeofday = frame['attributes']['timeofday'] labels = frame['labels'] matname = image_name.replace('jpg', 'mat') for label in labels: class_name = label['category'] if class_name == 'rider': class_name = 'person' if class_name not in class_names: continue xy = label['box2d'] bbox = [xy['x1'], xy['y1'], xy['x2'], xy['y2']] x1, y1, x2, y2 = map(int, bbox) occluded = label['attributes']['occluded'] truncated = label['attributes']['truncated'] height = abs(bbox[1] - bbox[3]) width = abs(bbox[0] - bbox[2]) area = width * height row = [ image_name, class_name, *bbox, height, width, area, occluded, truncated, weather, timeofday ] if abs(x2 - x1) <= 0 or abs(y2 - y1) <= 0: print('skip') continue assert ((x2 - x1) * (y2 - y1) > 0) for name in classes: cam_mat = loadmat(os.path.join('bdd_out_cam', name, matname))['cam'] diff_mat = loadmat(os.path.join('bdd_out_diff', name, matname))['cam'] cam_bbox = cam_mat[y1:y2, x1:x2] diff_bbox = diff_mat[y1:y2, x1:x2] cam_max = np.max(cam_bbox) diff_max = np.max(diff_bbox) row.append(cam_max) row.append(diff_max) cam_mean = np.mean(cam_bbox) diff_mean = np.mean(diff_bbox) row.append(cam_mean) row.append(diff_mean) data.append(row) df = pd.DataFrame(data, columns=column_names, dtype=float) df.to_csv('bdd100k_val_difficulty.csv') print(df[['height', 'width', 'area', 'occluded', 'truncated', 'timeofday']])
def kitti(setname): kitti2coco = { 'Car': 'car', 'Van': 'car', 'Truck': 'truck', 'Pedestrian': 'person', 'Person_sitting': 'person', 'Cyclist': 'person', 'Tram': 'train' } DEPTH_DATA_PATH = '/media/hdd2/ford/kitti_object_depth/vec/data_object_image_2/training/image_2' frames = glob.glob( os.path.join('/home/fremont/ford/kitti/training/label_2', '*.txt')) gradient_PATH = 'kitti_{}out_gradient'.format(setname) KITTI_IMAGE_PATH = '/home/fremont/ford/kitti/training/yolo/images' column_names = [ 'image', 'class', 'x1', 'y1', 'x2', 'y2', 'height', 'width', 'area', 'truncated', 'occluded', 'alpha', 'difficulty', 'depth_max', 'depth_min', 'depth_mean' ] class_names = set(load_classes('data/coco.names')) classes = os.listdir('kitti_{}out_cam'.format(setname)) classes.sort() for c in classes: column_names.append(c + '_cam_max') column_names.append(c + '_diff_max') column_names.append(c + '_grad_max') column_names.append(c + '_cam_mean') column_names.append(c + '_diff_mean') column_names.append(c + '_grad_mean') data = [] counter = 0 for frame in tqdm.tqdm(frames): frame = '/home/fremont/ford/kitti/training/label_2/000002.txt' labels = np.genfromtxt(frame, delimiter=" ", dtype='str') image_name = os.path.basename(frame).replace('txt', 'png') img = cv2.imread(os.path.join(KITTI_IMAGE_PATH, image_name)) # cv2.imshow('imig', img) # cv2.waitKey(0) depth_image_path = os.path.join(DEPTH_DATA_PATH, image_name) zimg = 99505.81485 / np.array(Image.open(depth_image_path)) # zimg = gaussian_filter(zimg, sigma=2) import matplotlib.pyplot as plt zimg[zimg > np.percentile(zimg, 99.5)] = np.percentile(zimg, 99.5) plt.imshow(zimg) plt.show() exit(0) matname = image_name.replace('png', 'mat') for label in labels: kitti_name = label[0] if kitti_name not in kitti2coco: continue class_name = kitti2coco[kitti_name] truncated = float(label[1]) occluded = float(label[2]) alpha = float(label[3]) bbox = label[4:8].astype(float) x1, y1, x2, y2 = map(int, bbox) height = abs(bbox[1] - bbox[3]) width = abs(bbox[0] - bbox[2]) depth = zimg[y1:y2, x1:x2] depth_max, depth_min, depth_mean = np.max(depth), np.min( depth), np.mean(depth) area = width * height if height >= 40 and (occluded in [0]) and truncated <= 0.15: difficulty = 'easy' elif height >= 25 and (occluded in [0, 1]) and truncated <= 0.3: difficulty = 'medium' elif height >= 25 and (occluded in [0, 1, 2]) and truncated <= 0.5: difficulty = 'hard' else: difficulty = 'extreme/unkown' counter += 1 row = [ image_name, class_name, *bbox, height, width, area, truncated, occluded, alpha, difficulty, depth_max, depth_min, depth_mean ] if abs(x2 - x1) <= 0 or abs(y2 - y1) <= 0: print('skip') continue assert ((x2 - x1) * (y2 - y1) > 0) for name in classes: cam_mat = loadmat( os.path.join('kitti_{}out_cam'.format(setname), name, matname))['cam'] diff_mat = loadmat( os.path.join('kitti_{}out_diff'.format(setname), name, matname))['cam'] temp_mat = loadmat( os.path.join('kitti_{}out_gradient'.format(setname), name, matname))['cam'] cam_bbox = cam_mat[y1:y2, x1:x2] diff_bbox = diff_mat[y1:y2, x1:x2] temp_bbox = temp_mat[y1:y2, x1:x2] cam_max = np.max(cam_bbox) diff_max = np.max(diff_bbox) temp_max = np.max(temp_bbox) row.append(cam_max) row.append(diff_max) row.append(temp_max) cam_mean = np.mean(cam_bbox) diff_mean = np.mean(diff_bbox) temp_mean = np.mean(temp_bbox) row.append(cam_mean) row.append(diff_mean) row.append(temp_mean) data.append(row) df = pd.DataFrame(data, columns=column_names, dtype=float) # df.to_csv('kitti_{}val_difficulty_v2.csv'.format(setname)) print(df[[ 'height', 'width', 'area', 'occluded', 'truncated', 'alpha', 'difficulty' ]]) print(counter / len(data))
def detect_main(qthread): qthread.status_update.emit('模型加载') device = torch.device(device_name) # 获取检测模型 model = get_model(config_path, img_size, weights_path, device) logging.info('Model initialized') qthread.status_update.emit('连接OPC服务') if open_opc: opc_client = OpcClient(opc_url, nodes_dict) strftime = time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) qthread.text_append.emit(strftime + ' OPC 服务器已连接') logging.info('OPC Client created') else: opc_client = None strftime = time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) qthread.text_append.emit(strftime + ' OPC 服务器未连接') logging.warning('OPC Client does not create') if open_email_warning: warning_email = Email() else: warning_email = None qthread.status_update.emit('初始化异常处理程序') # 创建可视化类对象,画掩模、文本信息、检测框、FPS visualize = Visualize(masks_paths_dict) # 创建异常处理类对象,判断是否进入掩模区域,给OPC发信号停机,保存记录 handling = IntrusionHandling(masks_paths_dict, opc_client) qthread.status_update.emit('读取视频流') # 创建加载视频的类,多线程读帧,生产者-消费者 video_loader = VideoLoader(video_stream_paths_dict) logging.info('Video streams create: ' + ', '.join(n for n in video_stream_paths_dict.keys())) qthread.status_update.emit('准备就绪') classes = load_classes(class_path) # Extracts class labels from file # 计时器开始的变量 since = patrol_opc_nodes_clock_start = update_detection_flag_clock_start = time.time( ) # 用于后续计算检测FPS accum_time, curr_fps = 0, 0 show_fps = 'FPS: ??' prevs_frames_dict = None logging.info('Enter detection main loop process') # 进入检测的主循环 exception_flag = False while not exception_flag: # 当前次循环的时间戳 curr_time = time.time() # 隔一段时间更新一次 detection_flag,这个标志是和守护进程共享内存的, # 以此让守护进程确认检测主循环在正常运行 if curr_time - update_detection_flag_clock_start > update_detection_flag_interval: update_detection_flag_clock_start = curr_time qthread.detection_flag.value = 1 # 更新 detection_flag # time.sleep(10) # 模拟检测程序卡住 10s # 隔一段时间轮巡读取一遍OPC状态信息,确认OPC服务正常与否 if curr_time - patrol_opc_nodes_clock_start > patrol_opc_nodes_interval: patrol_opc_nodes_clock_start = curr_time if open_opc: try: opc_client.patrol_nodes() except RuntimeError as er: strftime = time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) msg = strftime + ' OPC服务失效,无法获取节点数据!!' qthread.text_append.emit(msg) print(msg) logging.error('OPC服务失效,无法获取节点数据!!') if open_popup_message_box: qthread.popup_message_box.emit( "OPC服务异常,无法获取机器人节点数据, 系统失效!!\n请排查OPC软件异常后,重启系统\n\n联系电话: 13429129739" ) if open_email_warning and warning_email is not None: warning_email.subthread_email_warning( "OPC服务失效,无法获取节点数据", "检查时间:" + strftime) except Exception as ex: strftime = time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) msg = strftime + ' OPC服务失效,无法获取节点数据!!未知错误' qthread.text_append.emit(msg) print(msg + str(ex)) logging.error('OPC服务无法获取节点数据!!未知错误' + str(ex)) if open_popup_message_box: qthread.popup_message_box.emit( "无法获取OPC服务节点数据, 系统失效!!\n请排查OPC软件问题,重启系统\n\n联系电话: 13429129739" ) if open_email_warning and warning_email is not None: warning_email.subthread_email_warning( "OPC服务失效,无法获取节点数据", "检查时间:" + strftime) # prepare frame tensors before inference frames_dict = video_loader.getitem() # 如果有读不到,则该帧的值为None active_streams = [] input_tensor = [] for name in frames_dict.keys(): if frames_dict[name] is None: if prevs_frames_dict is not None: frames_dict[name] = prevs_frames_dict[name] # 将图片转换成 PyTorch Tensor tensor = transform(frames_dict[name], img_size) input_tensor.append(tensor) else: active_streams.append(stations_name_dict[name]) tensor = transform(frames_dict[name], img_size) input_tensor.append(tensor) if len(input_tensor) == len(frames_dict): prevs_frames_dict = frames_dict elif len(input_tensor) == 0: print("未读到任何视频帧") time.sleep(0.5) continue # 将多张图片的Tensor堆叠一起,相当于batch size input_tensor = stack_tensors(input_tensor) # model inference and postprocess preds = inference(model, input_tensor, device, 80, conf_thres, nms_thres) if prevs_frames_dict is None: not_none_streams = [ x for x in frames_dict.keys() if frames_dict[x] is not None ] else: not_none_streams = list(frames_dict.keys()) # 返回值只有非None视频流的预测结果 preds_dict = preds_postprocess(preds, not_none_streams, frame_shape, img_size, classes) # judge whether someone breaks into # 只判断和返回preds_dict中有预测结果的视频帧 judgements_dict = handling.judge_intrusion(preds_dict) # calculate inference fps since, accum_time, curr_fps, show_fps = calc_fps( since, accum_time, curr_fps, show_fps) # print(show_fps) # visualize detection results # 只绘制和返回preds_dict中有预测结果的视频帧 vis_imgs_dict = visualize.draw(frames_dict, preds_dict, judgements_dict, show_fps) # handle judgement results handling.handle_judgement(judgements_dict, vis_imgs_dict) # emit the information to the front end if vis_name in vis_imgs_dict: img = vis_imgs_dict[vis_name] qsize = qthread.main_window.videoLabel_1.size() qimage = array_to_QImage(img, qsize) qthread.video_1_change_pixmap.emit(qimage) for name in judgements_dict.keys(): if judgements_dict[name]: timestr = time.strftime('%Y-%m-%d %H:%M:%S ', time.localtime()) qthread.text_append.emit(timestr + name + ' 启动联锁保护') # show intrusion record img = vis_imgs_dict[name] qsize = qthread.main_window.recordLabel.size() qimage = array_to_QImage(img, qsize) qthread.record_change_pixmap.emit(qimage) if prevs_vis_name in vis_imgs_dict: prevs_img = vis_imgs_dict[prevs_vis_name] vis_imgs_dict[vis_name] = prevs_img vis_imgs_dict.pop(prevs_vis_name) for i, img in enumerate(vis_imgs_dict.values()): qsize = qthread.main_window.videoLabel_2.size() qimage = array_to_QImage(img, qsize) if i == 0: qthread.video_2_change_pixmap.emit(qimage) elif i == 1: qthread.video_3_change_pixmap.emit(qimage) elif i == 2: qthread.video_4_change_pixmap.emit(qimage) elif i == 3: qthread.video_5_change_pixmap.emit(qimage) else: raise RuntimeError("No so many QLabel!") statusbar_width = qthread.main_window.statusbar.size().width() qthread.main_window.statusbar.showMessage('正在检测' + ' ' * int(0.09 * statusbar_width) + '生产线: ' + ', '.join(active_streams))
parser.add_argument('--n_cpu', type=int, default=8) parser.add_argument('--img_size', type=int, default=416) parser.add_argument('--checkpoint_interval', type=int, default=10) parser.add_argument('--evaluation_interval', type=int, default=10) parser.add_argument('--multiscale_training', type=bool, default=True) opt = parser.parse_args() print(opt) device = tc.device('cuda' if tc.cuda.is_available() else 'cpu') os.makedirs('checkpoints', exist_ok=True) # Get data configuration data_config = parse_data_config(opt.data_config) train_path = data_config['train'] valid_path = data_config['valid'] class_names = load_classes(data_config['names']) # Initiate model model = Darknet(opt.model_def).to(device) model.apply(weights_init_normal) # If specified we start from checkpoint if opt.pretrained_weights: if opt.pretrained_weights.endswith('.pth'): model.load_state_dict(tc.load(opt.pretrained_weights)) else: model.load_darknet_weights(opt.pretrained_weights) # Get dataloader dataset = ListDataset(train_path, augment=True,
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Set up model model = Darknet(opt.model_def, img_size=opt.frame_size).to(device) # loading weights if opt.weights_path.endswith(".weights"): model.load_darknet_weights(opt.weights_path) # Load weights else: model.load_state_dict(torch.load(opt.weights_path)) # Load checkpoints # Set in evaluation mode model.eval() # Extracts class labels from file classes = load_classes(opt.class_path) # ckecking for GPU for Tensor Tensor = torch.cuda.FloatTensor if torch.cuda.is_available( ) else torch.FloatTensor # camara capture cap = cv2.VideoCapture(opt.input_file_path) assert cap.isOpened(), 'Cannot capture source' # Video feed dimensions _, frame = cap.read() v_height, v_width = frame.shape[:2] # print(v_height,v_width)
def run(): print_environment_info() parser = argparse.ArgumentParser(description="Evaluate validation data.") parser.add_argument("-m", "--model", type=str, default="config/yolov3.cfg", help="Path to model definition file (.cfg)") parser.add_argument( "-w", "--weights", type=str, default="weights/yolov3.weights", help="Path to weights or checkpoint file (.weights or .pth)") parser.add_argument("-d", "--data", type=str, default="config/coco.data", help="Path to data config file (.data)") parser.add_argument("-b", "--batch_size", type=int, default=8, help="Size of each image batch") parser.add_argument("-v", "--verbose", action='store_true', help="Makes the validation more verbose") parser.add_argument("--img_size", type=int, default=416, help="Size of each image dimension for yolo") parser.add_argument( "--n_cpu", type=int, default=8, help="Number of cpu threads to use during batch generation") parser.add_argument("--iou_thres", type=float, default=0.5, help="IOU threshold required to qualify as detected") parser.add_argument("--conf_thres", type=float, default=0.01, help="Object confidence threshold") parser.add_argument("--nms_thres", type=float, default=0.4, help="IOU threshold for non-maximum suppression") args = parser.parse_args() print("Command line arguments: {}".format(args)) # Load configuration from data file data_config = parse_data_config(args.data) # Path to file containing all images for validation valid_path = data_config["valid"] class_names = load_classes(data_config["names"]) # List of class names precision, recall, AP, f1, ap_class = evaluate_model_file( args.model, args.weights, valid_path, class_names, batch_size=args.batch_size, img_size=args.img_size, n_cpu=args.n_cpu, iou_thres=args.iou_thres, conf_thres=args.conf_thres, nms_thres=args.nms_thres, verbose=True)
"""Load darknet weights""" model.load_darknet_weights(opt.weights_path) else: """Load checkpoint weights""" model.load_state_dict(torch.load(opt.weights_path)) model.eval() dataloader = DataLoader( ImageFolder(opt.image_folder, img_size=opt.img_size), batch_size=opt.batch_size, shuffle=False, num_workers=opt.n_cpu, ) classes = load_classes(opt.class_path) # Extracts class labels from file imgs = [] # Stores image paths img_detections = [] # Stores detections for each image index print("\nPerforming object detection:") prev_time = time.time() for batch_i, (img_paths, input_imgs) in enumerate(dataloader): input_imgs = input_imgs.requires_grad_(False).to(device) with torch.no_grad(): detections = model(input_imgs) detections = non_max_suppression(detections, opt.conf_thres, opt.nms_thres) current_time = time.time()
def test(cfg, data, batch_size, img_size, conf_thres, iou_thres, nms_thres, src_txt_path, weights, log_file_path=None, model=None): # 0、初始化一些参数 data = parse_data_cfg(data) nc = int(data['classes']) # number of classes names = load_classes(data['names']) # 1、加载网络 if model is None: device = select_device('0') model = Darknet(cfg) if weights.endswith('.pt'): # TODO: .weights权重格式 model.load_state_dict( torch.load(weights, map_location=device)['model'] ) # 20200704_50epoch_modify_noobj # TODO:map_location=device ? if torch.cuda.device_count() > 1: model = nn.DataParallel(model) # clw note: 多卡 else: device = next(model.parameters()).device # get model device model.to(device).eval() # 2、加载数据集 test_dataset = VocDataset(src_txt_path, img_size, with_label=True, is_training=False) dataloader = DataLoader( test_dataset, batch_size=batch_size, shuffle=False, num_workers=8, # TODO collate_fn=test_dataset.test_collate_fn, # TODO pin_memory=True) # 3、预测,前向传播 image_nums = 0 s = ('%20s' + '%10s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', 'mAP@{}'.format(iou_thres), 'F1') #s = ('%20s' + '%10s' * 6) % ('Class', 'ImgNum', 'Target', 'P', 'R', '[email protected]', 'F1') p, r, f1, mp, mr, map, mf1 = 0., 0., 0., 0., 0., 0., 0. jdict, stats, ap, ap_class = [], [], [], [] pbar = tqdm(dataloader) for i, (img_tensor, target_tensor, _, _) in enumerate(pbar): img_tensor = img_tensor.to(device) # (bs, 3, 416, 416) target_tensor = target_tensor.to(device) height, width = img_tensor.shape[2:] start = time.time() # Disable gradients with torch.no_grad(): # (1) Run model output = model( img_tensor ) # (x1, y1, x2, y2, obj_conf, class_conf, class_pred) # (2) NMS nms_output = non_max_suppression(output, conf_thres, nms_thres) s = 'time use per batch: %.3fs' % (time.time() - start) pbar.set_description(s) for batch_idx, pred in enumerate(nms_output): # pred: (bs, 7) labels = target_tensor[target_tensor[:, 0] == batch_idx, 1:] nl = len(labels) # len of label tcls = labels[:, 0].tolist() if nl else [] # target class image_nums += 1 # 考虑一个预测 box 都没有的情况,比如 conf 太高 if pred is None: if nl: stats.append(([], torch.Tensor(), torch.Tensor(), tcls)) continue # Clip boxes to image bounds TODO:有必要,因为 label 都是经过clip的,所以如果去掉clip,mAP应该会有所降低 clip_coords(pred, (height, width)) # mAP is the same # Assign all predictions as incorrect correct = [0] * len(pred) if nl: detected = [] tcls_tensor = labels[:, 0] # target boxes tbox = xywh2xyxy(labels[:, 1:5]) tbox[:, [0, 2]] *= img_tensor[batch_idx].size()[2] # w tbox[:, [1, 3]] *= img_tensor[batch_idx].size()[1] # h # Search for correct predictions for i, (*pbox, pconf, pcls_conf, pcls) in enumerate(pred): # Break if all targets already located in image if len(detected) == nl: break # Continue if predicted class not among image classes if pcls.item() not in tcls: continue # Best iou, index between pred and targets m = (pcls == tcls_tensor).nonzero().view(-1) iou, bi = bbox_iou(pbox, tbox[m]).max(0) # If iou > threshold and class is correct mark as correct if iou > iou_thres and m[ bi] not in detected: # and pcls == tcls[bi]: correct[i] = 1 detected.append(m[bi]) # print('stats.append: ', (correct, pred[:, 4].cpu(), pred[:, 6].cpu(), tcls)) ''' pred flag ( [1, 0, 1, 0, 0, 1, 0, 0, 1], pred conf tensor([0.17245, 0.14642, 0.07215, 0.07138, 0.07069, 0.06449, 0.06222, 0.05580, 0.05452]), pred cls tensor([2., 2., 2., 2., 2., 2., 2., 2., 2.]), lb_cls [2.0, 2.0, 2.0, 2.0, 2.0]) stats is a [] ''' stats.append( (correct, pred[:, 4].cpu(), pred[:, 6].cpu(), tcls)) # Append statistics (correct, conf, pcls, tcls) # after get stats for all images , ... # Compute statistics stats = [np.concatenate(x, 0) for x in list(zip(*stats))] # to numpy if len(stats): p, r, ap, f1, ap_class = ap_per_class(*stats) mp, mr, map, mf1 = p.mean(), r.mean(), ap.mean(), f1.mean() nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class else: nt = torch.zeros(1) # Print results # time.sleep(0.01) # clw note: 防止前面 tqdm 还没输出,但是这里已经打印了 #pf = '%20s' + '%10.3g' * 6 # print format pf = '%20s' + '%10s' + '%10.3g' * 5 pf_value = pf % ('all', str(image_nums), nt.sum(), mp, mr, map, mf1) print(pf_value) if __name__ != '__main__': write_to_file(s, log_file_path) write_to_file(pf_value, log_file_path) results = [] results.append({"all": (mp, mr, map, mf1)}) # Print results per class #if verbose and nc > 1 and len(stats): if nc > 1 and len(stats): for i, c in enumerate(ap_class): #print(pf % (names[c], seen, nt[c], p[i], r[i], ap[i], f1[i])) print(pf % (names[c], '', nt[c], p[i], r[i], ap[i], f1[i])) if __name__ != '__main__': write_to_file( pf % (names[c], '', nt[c], p[i], r[i], ap[i], f1[i]), log_file_path) results.append({names[c]: (p[i], r[i], ap[i], f1[i])}) # Return results maps = np.zeros(nc) + map for i, c in enumerate(ap_class): maps[c] = ap[i] return (mp, mr, map, mf1), maps
# make_folder(SAVE_IMAGE_DIR) # IMAGE_DIR = 'data/bdd100k/images/100k/val' # PREDICTION_PATH = 'predlabeled' # classes = os.listdir('bdd_out_cam') # IMAGE_DIR = '/home/fremont/ford/kitti/training/yolo/images' IMAGE_DIR = '/home/fremont/ford/kitti/training/yolo/{}'.format(num_name) PREDICTION_PATH = 'kitti_{}predlabeled'.format(setname) classes = os.listdir('kitti_{}out_cam'.format(setname)) classes.sort() pred_files = glob.glob(os.path.join(PREDICTION_PATH, '*')) class_names = load_classes('data/coco.names') class_names[79] = 'ood' type_bbox = ['FN', 'FP', 'TP'] data = [] column_names = [ 'image_id', 'x1', 'y1', 'x2', 'y2', 'confidence', 'class', 'type' ] for c in classes: column_names.append(c + '_cam_max') column_names.append(c + '_grad_max') column_names.append(c + '_cam_mean') column_names.append(c + '_grad_mean') for it, obj_inst in tqdm.tqdm(enumerate(pred_files), total=len(pred_files)):
def main(): """Create a TensorRT engine for ONNX-based YOLOv3-608 and run inference.""" # Try to load a previously generated YOLOv3-608 network graph in ONNX format: onnx_file_path = './yolov3.onnx' engine_file_path = "yolov3.trt" data_path = "./data/unrel.data" data = parse_data_cfg(data_path) nc = int(data['classes']) # number of classes path = data['valid'] # path to test images names = load_classes(data['names']) # class names iouv = torch.linspace(0.5, 0.95, 1, dtype=torch.float32) # iou vector for [email protected]:0.95 niou = 1 conf_thres = 0.001 iou_thres = 0.6 verbose = True # Genearte custom dataloader img_size = 448 # copy form pytorch src batch_size = 16 dataset = LoadImagesAndLabels(path, img_size, batch_size, rect=True) batch_size = min(batch_size, len(dataset)) dataloader = data_loader(dataset, batch_size, img_size) # Output shapes expected by the post-processor output_shapes = [(16, 126, 14, 14), (16, 126, 28, 28), (16, 126, 56, 56)] # Do inference with TensorRT trt_outputs = [] with get_engine(onnx_file_path, engine_file_path ) as engine, engine.create_execution_context() as context: inputs, outputs, bindings, stream = common.allocate_buffers(engine) s = ('%20s' + '%10s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', '[email protected]', 'F1') p, r, f1, mp, mr, map, mf1, t0, t1 = 0., 0., 0., 0., 0., 0., 0., 0., 0. pbar = tqdm.tqdm(dataloader, desc=s) stats, ap, ap_class = [], [], [] seen = 0 for batch_i, (imgs, targets, paths, shapes) in enumerate(pbar): imgs = imgs.astype(np.float32) / 255.0 nb, _, height, width = imgs.shape # batch size, channels, height, width whwh = np.array([width, height, width, height]) inputs[0].host = imgs postprocessor_args = { "yolo_masks": [ (6, 7, 8), (3, 4, 5), (0, 1, 2) ], # A list of 3 three-dimensional tuples for the YOLO masks "yolo_anchors": [ (10, 13), (16, 30), (33, 23), (30, 61), ( 62, 45 ), # A list of 9 two-dimensional tuples for the YOLO anchors (59, 119), (116, 90), (156, 198), (373, 326) ], "num_classes": 37, "stride": [32, 16, 8] } postprocessor = PostprocessYOLO(**postprocessor_args) # Do layers before yolo t = time.time() trt_outputs = common.do_inference_v2(context, bindings=bindings, inputs=inputs, outputs=outputs, stream=stream) trt_outputs = [ output.reshape(shape) for output, shape in zip(trt_outputs, output_shapes) ] trt_outputs = [ np.ascontiguousarray( otpt[:, :, :int(imgs.shape[2] * (2**i) / 32), :int(imgs.shape[3] * (2**i) / 32)], dtype=np.float32) for i, otpt in enumerate(trt_outputs) ] output_list = postprocessor.process(trt_outputs) t0 += time.time() - t inf_out = torch.cat(output_list, 1) t = time.time() output = non_max_suppression(inf_out, conf_thres=conf_thres, iou_thres=iou_thres) # nms t1 += time.time() - t # Statistics per image for si, pred in enumerate(output): labels = targets[targets[:, 0] == si, 1:] nl = len(labels) tcls = labels[:, 0].tolist() if nl else [] # target class seen += 1 if pred is None: if nl: stats.append((torch.zeros(0, niou, dtype=torch.bool), torch.Tensor(), torch.Tensor(), tcls)) continue # Assign all predictions as incorrect correct = torch.zeros(pred.shape[0], niou, dtype=torch.bool) if nl: detected = [] # target indices tcls_tensor = labels[:, 0] # target boxes tbox = xywh2xyxy(labels[:, 1:5]) * whwh tbox = tbox.type(torch.float32) # Per target class for cls in torch.unique(tcls_tensor): ti = (cls == tcls_tensor).nonzero().view( -1) # prediction indices pi = (cls == pred[:, 5]).nonzero().view( -1) # target indices # Search for detections if pi.shape[0]: # Prediction to target ious ious, i = box_iou(pred[pi, :4], tbox[ti]).max( 1) # best ious, indices # Append detections for j in (ious > iouv[0]).nonzero(): d = ti[i[j]] # detected target if d not in detected: detected.append(d) correct[pi[j]] = ious[ j] > iouv # iou_thres is 1xn if len( detected ) == nl: # all targets already located in image break # Append statistics (correct, conf, pcls, tcls) stats.append( (correct.cpu(), pred[:, 4].cpu(), pred[:, 5].cpu(), tcls)) # Plot images if batch_i < 1: f = 'test_batch%g_gt.jpg' % batch_i # filename plot_images(imgs, targets, paths=paths, names=names, fname=f) # ground truth f = 'test_batch%g_pred.jpg' % batch_i plot_images(imgs, output_to_target(output, width, height), paths=paths, names=names, fname=f) # predictions # Compute statistics stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy if len(stats): p, r, ap, f1, ap_class = ap_per_class(*stats) if niou > 1: p, r, ap, f1 = p[:, 0], r[:, 0], ap.mean( 1), ap[:, 0] # [P, R, [email protected]:0.95, [email protected]] mp, mr, map, mf1 = p.mean(), r.mean(), ap.mean(), f1.mean() nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class else: nt = torch.zeros(1) # Print results pf = '%20s' + '%10.3g' * 6 # print format print(pf % ('all', seen, nt.sum(), mp, mr, map, mf1)) # Print results per class if verbose and nc > 1 and len(stats): for i, c in enumerate(ap_class): print(pf % (names[c], seen, nt[c], p[i], r[i], ap[i], f1[i])) # Print speeds if verbose: t = tuple(x / seen * 1E3 for x in (t0, t1, t0 + t1)) + ( img_size, img_size, batch_size) # tuple print( 'Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t)
ai_configuration_chemin_daccess = 'config/yolov3.cfg' ai_poids_chemin_daccess = 'config/yolov3.weights' nom_des_objets_chemin_daccess = 'config/coco_fr.names' image_size = 416 # Ont définis la résolution de l'image qu'ont vas donner à notre intelligence conf_thres = 0.8 nms_thres = 0.4 # Ont charge notre model d'intelligence artificielle model = Darknet(ai_configuration_chemin_daccess, img_size=image_size) # Ont charge les poids (ce que notre intelligence artificielle à appris) model.load_weights(ai_poids_chemin_daccess) model.eval() # Ont charger les noms de ce que notre intelligence a appris à reconnaitre classes = utils.load_classes(nom_des_objets_chemin_daccess) def detect_image(image: Image): # Modifie la taille de l'image, et ses marges pour qu'ont puisse envoyer n'importe quel type d'image à notre intelligence ratio = min(image_size / image.size[0], image_size / image.size[1]) imw = round(image.size[0] * ratio) imh = round(image.size[1] * ratio) img_transforms = transforms.Compose([ transforms.Resize((imh, imw)), transforms.Pad((max(int( (imh - imw) / 2), 0), max(int( (imw - imh) / 2), 0), max(int( (imh - imw) / 2), 0), max(int((imw - imh) / 2), 0)), (128, 128, 128)), transforms.ToTensor(),
class_path='config/coco.names' config_path='config/yolov3.cfg' weights_path='config/yolov3.weights' img_size=416 nms_thres=0.4 conf_thres=0.8 # Load model and weights model = Darknet(config_path, img_size=img_size) model.load_weights(weights_path) model.cuda() model.eval() classes = utils.load_classes(class_path) Tensor = torch.cuda.FloatTensor # In[59]: session = Session() connection = Connection("http://127.0.0.1:8088/signalr", session) conn = connection.register_hub('step5') connection.start() # In[54]:
def mask_catch(input, output): parser = argparse.ArgumentParser() parser.add_argument(u"--input_file_path", type=unicode, default=input, help=u"path to images directory") parser.add_argument(u"--output_path", type=unicode, default=output, help=u"output image directory") parser.add_argument(u"--model_def", type=unicode, default=u"data/yolov3_mask.cfg", help=u"path to model definition file") parser.add_argument(u"--weights_path", type=unicode, default=u"checkpoints/yolov3_ckpt_499.pth", help=u"path to weights file") parser.add_argument(u"--class_path", type=unicode, default=u"data/mask_dataset.names", help=u"path to class label file") parser.add_argument(u"--conf_thres", type=float, default=0.8, help=u"object confidence threshold") parser.add_argument(u"--nms_thres", type=float, default=0.3, help=u"iou thresshold for non-maximum suppression") parser.add_argument(u"--frame_size", type=int, default=416, help=u"size of each image dimension") opt = parser.parse_args() # Output directory os.makedirs(opt.output_path, exist_ok=True) # checking for GPU device = torch.device(u"cuda" if torch.cuda.is_available() else u"cpu") # Set up model model = Darknet(opt.model_def, img_size=opt.frame_size).to(device) # loading weights if opt.weights_path.endswith(u".weights"): model.load_darknet_weights(opt.weights_path) # Load weights else: model.load_state_dict(torch.load(opt.weights_path)) # Load checkpoints # Set in evaluation mode model.eval() # Extracts class labels from file classes = load_classes(opt.class_path) # ckecking for GPU for Tensor Tensor = torch.cuda.FloatTensor if torch.cuda.is_available( ) else torch.FloatTensor print u"\nPerforming object detection:" # for text in output t_size = cv2.getTextSize(u" ", cv2.FONT_HERSHEY_PLAIN, 1, 1)[0] for imagename in os.listdir(opt.input_file_path): print u"\n" + imagename + u"_______" image_path = os.path.join(opt.input_file_path, imagename) print image_path # frame extraction org_img = cv2.imread(image_path) # Original image width and height i_height, i_width = org_img.shape[:2] # resizing => [BGR -> RGB] => [[0...255] -> [0...1]] => [[3, 416, 416] -> [416, 416, 3]] # => [[416, 416, 3] => [416, 416, 3, 1]] => [np_array -> tensor] => [tensor -> variable] # resizing to [416 x 416] # Create a black image x = y = i_height if i_height > i_width else i_width # Black image img = np.zeros((x, y, 3), np.uint8) # Putting original image into black image start_new_i_height = int((y - i_height) / 2) start_new_i_width = int((x - i_width) / 2) img[start_new_i_height:(start_new_i_height + i_height), start_new_i_width:(start_new_i_width + i_width)] = org_img #resizing to [416x 416] img = cv2.resize(img, (opt.frame_size, opt.frame_size)) # [BGR -> RGB] img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # [[0...255] -> [0...1]] img = np.asarray(img) / 255 # [[3, 416, 416] -> [416, 416, 3]] img = np.transpose(img, [2, 0, 1]) # [[416, 416, 3] => [416, 416, 3, 1]] img = np.expand_dims(img, axis=0) # [np_array -> tensor] img = torch.Tensor(img) # plt.imshow(img[0].permute(1, 2, 0)) # plt.show() # [tensor -> variable] img = Variable(img.type(Tensor)) # Get detections with torch.no_grad(): detections = model(img) detections = non_max_suppression_output(detections, opt.conf_thres, opt.nms_thres) # print(detections) # For accommodate results in original frame mul_constant = x / opt.frame_size #We should set a variable for the number of nomask people. i is the variable i = 0 # For each detection in detections for detection in detections: if detection is not None: print u"{0} Detection found".format(len(detection)) for x1, y1, x2, y2, conf, cls_conf, cls_pred in detection: # Accommodate bounding box in original frame x1 = int(x1 * mul_constant - start_new_i_width) y1 = int(y1 * mul_constant - start_new_i_height) x2 = int(x2 * mul_constant - start_new_i_width) y2 = int(y2 * mul_constant - start_new_i_height) # Bounding box making and setting Bounding box title if (int(cls_pred) == 0): # WITH_MASK cv2.rectangle(org_img, (x1, y1), (x2, y2), (0, 255, 0), 2) else: #WITHOUT_MASK i += 1 cv2.rectangle(org_img, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.putText(org_img, classes[int(cls_pred)] + u": %.2f" % conf, (x1, y1 + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 1, [225, 255, 255], 2) u"""------------Ready to save!-----------------""" import time now = time.strftime(u"%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) #num is the number of people num = len(detection) #na=now + '-' + 'NUM:%d'%num +'-'+ 'Nom:%d'%i+'-'+'.jpg' u"""------------txt_save-----------------""" u"""------------image_save-----------------""" na = u'result.jpg' out_filepath = os.path.join(opt.output_path, na) cv2.imwrite(out_filepath, org_img) #org_img is final result with frames #naa = now + '-' + 'NUM:%d' % num + '-' + 'Nom:%d' % i #ssh_scp_put('172.21.39.222',22,'tensor','tensor',out_filepath,'/home/tensor/eden/%s.jpg'%naa) #upload_img(na) #os.remove(out_filepath) signal = 1 #we first set signal only 1 if i == 0: signal = 0 print u"Signal is ", signal print u"Finish to save!!!" msg = now + u'-' + u'NUM:%d' % num + u'-' + u'Nomask:%d' % i + u'-' nam = u'info.txt' full_path = os.path.join(opt.output_path, nam) print u"----------------" file = open(full_path, u'w') file.write(msg) cv2.destroyAllWindows() return signal
def stream(cfg, classes_file, weights, socket_ip, socket_port, image_size=128, confidence_threshold=0.6, nms_thres=0.5): print('+ Initializing model') model = Darknet(cfg, image_size) print('+ Loading model') load_darknet_weights(model, weights) print('+ Fusing model') model.fuse() print('+ Loading model to CPU') model.to('cpu').eval() print('+ Loading webcam') cap = LoadKinect(img_size=image_size) print('+ Loading classes') classes = load_classes(classes_file) colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(classes))] print('+ Connecting to remote socket') global sock sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((socket_ip, socket_port)) print('+ Enumerating cam') for counter, (path, img, im0, vid_cap) in enumerate(cap): t = time.time() print('+ Loading image to CPU') img = torch.from_numpy(img).unsqueeze(0).to('cpu') pred, _ = model(img) print('+ Detecting objects') det = non_max_suppression(pred, confidence_threshold, nms_thres)[0] if det is not None and len(det) > 0: detected_classes = [] print('+ Rescaling model') det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() print('+ Reading depth') depth = get_depth() depth_swap = np.swapaxes(depth, 0, 1) depth_strip1d = np.array([ np.sort(stripe)[100] for stripe in depth_swap ]).astype(np.uint8) depth_strip2d_swap = np.array([ np.ones(depth_swap.shape[1]) * depth for depth in depth_strip1d ]).astype(np.uint8) depth_strip2d = np.swapaxes(depth_strip2d_swap, 0, 1) depth_edge1d = np.zeros(depth_strip1d.shape) state = False for counter, _ in np.ndenumerate(depth_edge1d[:-1]): state = True if not state and depth_strip1d[ counter] < 230 else False depth_edge1d[counter[0]] = not state state = False state_cnt = 0 for counter, _ in np.ndenumerate(depth_edge1d[:-1]): counter = counter[0] if depth_edge1d[counter] == state: state_cnt += 1 else: if state_cnt < 10: for r in range(max(0, counter - 10), counter): depth_edge1d[counter] = state state_cnt = 0 state = depth_edge1d[counter] depth_edge1d = depth_edge1d * 255 depth_edge2d_swap = np.array([ np.ones(100) * awddawd for awddawd in depth_edge1d ]).astype(np.uint8) depth_edge2d = np.swapaxes(depth_edge2d_swap, 0, 1) for *coordinates, conf, cls_conf, cls in det: if classes[int(cls)] in RISKY_CLASSES: label = '%s %.2f' % (classes[int(cls)], conf) plot_one_box(coordinates, im0, label=label, color=colors[int(cls)]) print(f"+ Detected {classes[int(cls)]}") x_avg_depth = np.mean(depth[coordinates[0] - 5:coordinates[0] + 5]) y_avg_depth = np.mean(depth[coordinates[1] - 5:coordinates[1] + 5]) detected_classes.append({ classes[int(cls)]: { 'x': coordinates[0], 'y': coordinates[1], 'z': np.average(np.array([x_avg_depth, y_avg_depth])) } }) n = [] for counter in detected_classes: width = im0.shape[1] x, y, z = counter[list(counter.keys())[0]].values() phi = (x / width * 2 - 1) * (CAMERA_FOV / 2) n.append(f"{list(counter.keys())[0]};{phi};{z}|") sock.send(''.join(str(x) for x in n)[:-1].encode('utf-8')) print('+ Cycle took %.3fs' % (time.time() - t)) plt.imshow(bgr_to_rgb(im0)) plt.show(block=False) plt.pause(.001)
def main(): # load model device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu') model = Darknet(cfg.MODEL, img_size=cfg.SIZE).to(device) model.load_darknet_weights(cfg.WEIGHTS) model.eval() # coco classes classes = load_classes(cfg.CLASSES) # animals and person app_classes = [0, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23] # tensor type Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor # create video capture cap = cv2.VideoCapture('udp://127.0.0.1:5000', cv2.CAP_FFMPEG) if not cap.isOpened(): print('VideoCapture not opened') exit(-1) # preprocess pipeline t = transforms.Compose([ transforms.Resize((cfg.SIZE, cfg.SIZE)), transforms.ToTensor() ]) # tracker tracker = Sort() # bbox colors colors=[ (255,0,0), (0,255,0), (0,0,255), (255,0,255), (128,0,0), (0,128,0), (0,0,128), (128,0,128), (128,128,0), (0,128,128) ] # process stream while True: # read frame ret, frame = cap.read() # frame = cv2.flip(cv2.flip(frame, 0), 1) orig = frame frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img = Image.fromarray(frame) # process image img = t(img).unsqueeze(0).type(Tensor) with torch.no_grad(): detections = model(img) detections = non_max_suppression(detections, cfg.CONF, cfg.NMS) detections = detections[0] if detections is not None: # track objects tracked_objects = tracker.update(detections.cpu()) det = rescale_boxes(tracked_objects, cfg.SIZE, frame.shape[:2]) for x1, y1, x2, y2, obj_id, cls_pred in det: # ignore not necessary classes if int(cls_pred) not in app_classes: continue # draw bbox color = colors[int(obj_id) % len(colors)] cls = classes[int(cls_pred)] x1, x2, y1, y2 = int(x1), int(x2), int(y1), int(y2) cv2.rectangle(orig, (x1, y1), (x2, y2), color, 2) cv2.putText( orig, cls + '-' + str(int(obj_id)), (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 3 ) cv2.imshow('YoloV3', orig) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") os.makedirs("output", exist_ok=True) os.makedirs("checkpoints", exist_ok=True) # Get data configuration data_config = parse_data_config(opt.data_config) if platform == "linux" or platform == "linux2": train_path = data_config["train_Linux"] valid_path = data_config["valid_Linux"] else: train_path = data_config["train"] valid_path = data_config["valid"] class_names = load_classes(data_config["names"]) # Initiate model model = Darknet(opt.model_def).to(device) model.apply(weights_init_normal) # If specified we start from checkpoint if opt.pretrained_weights: if opt.pretrained_weights.endswith(".pth"): model.load_state_dict(torch.load(opt.pretrained_weights)) else: model.load_darknet_weights(opt.pretrained_weights) # Get dataloader dataset = ListDataset(train_path, augment=False, multiscale=opt.multiscale_training)
def detect(): # 0、初始化一些参数 cfg = opt.cfg weights = opt.weights src_txt_path = opt.src_txt_path img_size = opt.img_size batch_size = opt.batch_size dst_path = opt.dst_path if not os.path.exists(dst_path): os.mkdir(dst_path) device = select_device(opt.device) classes = load_classes(parse_data_cfg(opt.data)['names']) # 1、加载网络 model = Darknet(cfg) if weights.endswith('.pt'): # TODO: .weights权重格式 model.load_state_dict( torch.load(weights)['model']) # TODO:map_location=device ? model.to(device).eval() # 2、加载数据集 test_dataset = VocDataset(src_txt_path, img_size, with_label=False) dataloader = DataLoader( test_dataset, batch_size=batch_size, shuffle=False, num_workers=8, # TODO collate_fn=test_dataset.test_collate_fn) # TODO # 3、预测,前向传播 start = time.time() pbar = tqdm(dataloader) for i, (img_tensor, img0, img_name) in enumerate(pbar): pbar.set_description("Already Processed %d image: " % (i + 1)) # print('clw: Already Processed %d image' % (i+1)) img_tensor = img_tensor.to(device) # (bs, 3, 416, 416) output = model(img_tensor)[ 0] # (x1, y1, x2, y2, obj_conf, class_conf, class_pred) # NMS nms_output = non_max_suppression(output, opt.conf_thres, opt.nms_thres) # 可视化 for batch_idx, det in enumerate(nms_output): # detections per image if det is not None: # and len(det): # clw note: important ! #or box in det: for *box, conf, _, cls in det: # det: tensor.Size (bs, 7) box: list orig_h, orig_w = img0[batch_idx].shape[:2] # 坐标变换 new_h = new_w = img_tensor.size()[ 2] # 绘图,resize后的图的框 -> 原图的框,new -> orig ratio_h = orig_h / new_h ratio_w = orig_w / new_w x1 = int(ratio_w * box[0]) y1 = int(ratio_h * box[1]) x2 = int(ratio_w * (box[2])) y2 = int(ratio_h * (box[3])) label = '%s %.2f' % (classes[int(cls)], conf) # 预测结果可视化 plot_one_box([x1, y1, x2, y2], img0[batch_idx], label=label, color=(255, 0, 0)) #cv2.rectangle(img0[batch_idx], (x1, y1), (x2, y2), (0, 0, 255), 1) # 如果报错 TypeError: an integer is required (got type tuple),检查是不是传入了img_tensor if SAVE: # 保存结果 cv2.imwrite(os.path.join(dst_path, img_name[batch_idx]), img0[batch_idx]) if SHOW: cv2.imshow('aaa', img0[batch_idx]) cv2.waitKey(0) print('time use: %.3fs' % (time.time() - start))