class PersonDetector: def __init__(self, gpu_id=0, fastface=False): self.fastface = fastface caffe.set_mode_gpu() caffe.set_device(gpu_id) det_net = caffe.Net(cfg_priv.PersonDet.DEPLOY, cfg_priv.PersonDet.WEIGHTS, caffe.TEST) self.D = Detector(det_net, mean=cfg_priv.PersonDet.PIXEL_MEANS, std=cfg_priv.PersonDet.PIXEL_STDS, scales=(480,), max_sizes=(800,), preN=500, postN=50, conf_thresh=0.7, color_map={0: [192, 0, 192], 1: [64, 64, 255]}, class_map=V.PERSON_CLASS) if self.fastface: face_net = caffe.Net(cfg_priv.FastFace.DEPLOY, cfg_priv.FastFace.WEIGHTS, caffe.TEST) self.FastFace = Detector(face_net, mean=cfg_priv.FastFace.PIXEL_MEANS, std=cfg_priv.FastFace.PIXEL_STDS, scales=(480,), max_sizes=(800,), preN=500, postN=50, conf_thresh=0.5, color_map={0: [192, 0, 192], 1: [255, 64, 64]}, class_map=V.FACE_CLASS) def __call__(self, img): objs = self.D.det_im(img) if self.fastface: face_objs = self.FastFace.det_im(img) objs.extend(face_objs) if objs is None: return None else: return objs_sort_by_center(objs) def __del__(self): print self.__class__.__name__
class TrafficDetector: def __init__(self, gpu_id=0): caffe.set_mode_gpu() caffe.set_device(gpu_id) det_net = caffe.Net(cfg.Traffic.DEPLOY, cfg.Traffic.WEIGHTS, caffe.TEST) self.D = Detector(det_net, mean=cfg.Traffic.PIXEL_MEANS, std=cfg.Traffic.PIXEL_STDS, scales=(540, ), max_sizes=(960, ), preN=6000, postN=300, conf_thresh=0.6, color_map=V.COLORMAP21, class_map=V.SHANXI7_CLASS) def __call__(self, img): objs = self.D.det_im(img) if objs is None: return None else: return objs_sort_by_center(objs) def __del__(self): print self.__class__.__name__
class ObjDetector: def __init__(self, gpu_id=0): caffe.set_mode_gpu() caffe.set_device(gpu_id) det_net = caffe.Net(cfg_priv.ObjDet.DEPLOY, cfg_priv.ObjDet.WEIGHTS, caffe.TEST) self.D = Detector(det_net, mean=cfg_priv.ObjDet.PIXEL_MEANS, std=cfg_priv.ObjDet.PIXEL_STDS, scales=(480, ), max_sizes=(960, ), preN=1000, postN=100, conf_thresh=0.7, color_map=V.COLORMAP81, class_map=V.COCO_CLASS81) def __call__(self, img): objs = self.D.det_im(img) if objs is None: return None else: return objs_sort_by_center(objs) def __del__(self): print self.__class__.__name__
class FaceDetector: def __init__(self, gpu_id=0): caffe.set_mode_gpu() caffe.set_device(gpu_id) det_net = caffe.Net(cfg.FaceDet.DEPLOY, cfg.FaceDet.WEIGHTS, caffe.TEST) self.D = Detector(det_net, mean=cfg.FaceDet.PIXEL_MEANS, std=cfg.FaceDet.PIXEL_STDS, scales=(480, ), max_sizes=(800, ), preN=500, postN=50, conf_thresh=0.7, color_map={ 0: [192, 0, 192], 1: [255, 64, 64] }, class_map=V.FACE_CLASS) def __call__(self, img): objs = self.D.det_im(img) if objs is None: return None else: return objs_sort_by_center(objs) def __del__(self): print self.__class__.__name__
class FaceRecog: def __init__(self, gpu_id=0): caffe.set_mode_gpu() caffe.set_device(gpu_id) det_net = caffe.Net(cfg.FastFace.DEPLOY, cfg.FastFace.WEIGHTS, caffe.TEST) self.FastFace = Detector(det_net, mean=cfg.FastFace.PIXEL_MEANS, std=cfg.FastFace.PIXEL_STDS, scales=(300, ), max_sizes=(400, ), preN=500, postN=50, conf_thresh=0.5, color_map={ 0: [192, 0, 192], 1: [255, 64, 64] }, class_map=V.FACE_CLASS) id_net = caffe.Net(cfg.FaceID.DEPLOY, cfg.FaceID.WEIGHTS, caffe.TEST) self.I = Identity(id_net, mean=cfg.FaceID.PIXEL_MEANS, std=cfg.FaceID.PIXEL_STDS, base_size=256, crop_size=224, crop_type='center', prob_layer='classifier', gallery=np.load(cfg.FaceID.GALLERY)) self.name_list = open(cfg.FaceID.GALLERY_NAMES).readlines() print self.name_list def __call__(self, img): img_det = img.copy() objs = self.FastFace.det_im(img_det) if objs is None: return None else: faces = [] for i in objs: face_box = T.extend_bbox(img, i['bbox']) crop_face = T.bbox_crop(img, face_box) faces.append(crop_face) if len(faces) == 0: return None ids = self.I.cls_batch(faces) for _ in xrange(len(ids)): sims = self.I.one2n_identity(ids[_]) idx = np.asarray(sims).argsort()[-1] score = sims[idx] if score >= 0.5: objs[_]['attri'] = dict(name=self.name_list[idx].strip()) # score='{:.3f}'.format(score), # age='unknown') else: objs[_]['attri'] = dict( name='--------') # , score=str(0.0), age='unknown') return objs def __del__(self): print self.__class__.__name__