Пример #1
0
 def __init__(self, name):
     threading.Thread.__init__(self)
     self.name = name
     self.stopped = False
     self.succ_interval = SYS_CONF.get(self.name + "_interval", 30)
     self.fail_interval = SYS_CONF.get(self.name + "_failed_interval", 30)
     logging.info("task(%s): initiated", self.name)
Пример #2
0
    def __init__(self):
        conf = SYS_CONF["mongodb"]
        self.db_adapter = pymongo.MongoClient(conf["addr"])[conf["db"]]
        self.db_adapter.counters.update({"_id": "role-viewer"},
                                        {"$setOnInsert": {
                                            "next_id": 1000001
                                        }},
                                        upsert=True)
        self.db_adapter.counters.update({"_id": "role-operator"},
                                        {"$setOnInsert": {
                                            "next_id": 2000001
                                        }},
                                        upsert=True)
        self.db_adapter.counters.update({"_id": "role-developer"},
                                        {"$setOnInsert": {
                                            "next_id": 3000001
                                        }},
                                        upsert=True)

        # self.db_adapter.alerts.create_index(
        #     [("ttl", pymongo.ASCENDING)], expireAfterSeconds=2419200)

        function_table = SYS_CONF.get("function", {})
        self.db_adapter.function.update({}, {"$setOnInsert": function_table},
                                        upsert=True)

        face_table = SYS_CONF.get("facedata", {})
        self.db_adapter.facedata.update({}, {"$setOnInsert": face_table},
                                        upsert=True)

        # if SYS_CONF["device"] != "TD201":
        # pointer = SYS_CONF.get("chip_pointer", {})
        # self.db_adapter.pointer.update({}, {"$setOnInsert": pointer}, upsert=True)
        # tmp_pointer = self.db_adapter.pointer.find_one()
        # tmp_pointer.pop("_id")
        # memory.CHIP_POINTER_TABLE = tmp_pointer

        tmp_function = self.db_adapter.function.find_one()
        tmp_function.pop("_id")
        memory.FUNCTION_TABLE = tmp_function
        self.db_adapter.images.create_index([('source', 1), ('timestamp', -1)])

        wanrning_count = {}
        tmp_device = self.db_adapter.device.find()
        tmp_devices = [mongo_id_to_str(r) for r in tmp_device]
        for device in tmp_devices:
            wanrning_count.update({
                device['uuid']:
                {func: []
                 for func in device["function"].keys()}
            })
        memory.WARNING_COUNT = wanrning_count
Пример #3
0
def _request_cluster(path, model_list):
    results = {}

    image_extra_command = SYS_CONF.get("image_extra_command", {})

    with open(path, 'rb') as f:
        img = str(base64.b64encode(f.read()), encoding='utf-8')

    for model, model_info in SYS_CONF['chip_pointer'].items():
        if model not in model_list:
            continue
        address_list = model_info.get('address', [])
        if not address_list:
            raise RuntimeError("image {} owns none chip!".format(model))
        extra_command = image_extra_command[model]
        schedule_index = random.randint(0, len(address_list) - 1)
        node_address = address_list[schedule_index]

        logging.info("image %s @ recogn requesting: %s" %
                     (model, node_address))
        requests_command = {'image': img}
        requests_command.update(extra_command)
        response = requests.post("http://{}/api/detect".format(node_address),
                                 data=json.dumps(requests_command),
                                 timeout=3)
        response = response.json()['result']
        if type(response).__name__ == 'dict':
            temp = list(response.values())
            part_results = []
            for i in temp:
                t = json.loads(i)
                part_results += t
        else:
            part_results = json.loads(response)
        results[model] = part_results
    return results
Пример #4
0
 def upload(self):
     return self._data.get("upload", SYS_CONF.get("upload", {}))
Пример #5
0
 def alert(self):
     return self._data.get("alert", SYS_CONF.get("alert", {}))
Пример #6
0
 def history(self):
     return self._data.get("history", SYS_CONF.get("history", {}))
Пример #7
0
 def detection_cycle(self):
     return self._data.get("detection_cycle",
                           SYS_CONF.get("detection_cycle", {}))
Пример #8
0
 def interval(self):
     return self._data.get("interval",
                           SYS_CONF.get("default_capture_interval", 10))
Пример #9
0
def export(parse_results, pic_path, cam):
    export_url = SYS_CONF["exporter"].get("url")
    if export_url is None:
        raise RuntimeError("export url is None!")

    alert_allowed, _ = alctl.allowed_alert_type()
    img = cv2.imread(pic_path)
    cst_dt, utc_dt, ts = clock.now()

    cor = [20, 20]
    for func_name in alert_allowed:
        color = FUNC_BBOX_COLOR[func_name]
        cv2.putText(img, func_name, tuple(cor), cv2.FONT_HERSHEY_COMPLEX, 0.5,
                    color, 1)
        cor[1] = cor[1] + 15

    export_data = {
        "device_id": cam.uuid,
        "device_name": cam.name,
        "host_id": SYS_CONF.get("host_id"),
        "host_ip": SYS_CONF.get("host_ip"),
        "timestamp": clock.format_dt_standard(utc_dt),
        "location": cam.location,
        "alert": [],
        "image": '',
    }
    for func_name, alert_info in parse_results.items():
        if func_name not in alert_allowed:
            logging.info("alert(%s) raise, but will not export", func_name)
            continue
        aggregation_time = functl.get(func_name).get("aggreagate", 600)
        result = mongodb.alerts.find_one({
            "create_time": {
                '$gte': (ts - aggregation_time)
            },
            "device_id": cam.uuid,
            "func_name": func_name
        })

        if not result:
            for position in alert_info:
                probability = str(position["probability"])
                xmin = int(position["xmin"])
                ymin = int(position["ymin"])
                xmax = int(position["xmax"])
                ymax = int(position["ymax"])
                export_data["alert"].append([
                    func_name, probability,
                    str(xmin),
                    str(ymin),
                    str(xmax),
                    str(ymax)
                ])
                img = cv2.rectangle(img, (xmin, ymin), (xmax, ymax),
                                    FUNC_BBOX_COLOR[func_name], 4)
    b64_str = cv2.imencode('.jpg', img)[1].tostring()
    b64_img = base64.b64encode(b64_str)
    export_data["image"] = str(b64_img, encoding='utf-8')
    requests_data = json.dumps(export_data)
    try:
        if export_data["alert"] != []:
            response = requests.post(export_url, data=requests_data)
            logging.info(response)
    except Exception as err:
        logging.error("export data failed!")
Пример #10
0
 def __init__(self):
     super().__init__("sweeper")
     self.clean_quota = SYS_CONF.get("sweep_by_quota", 80)
Пример #11
0
def create(cam, func_name, result_info, image_path):
    alert_allowed, _ = allowed_alert_type()

    if func_name not in alert_allowed:
        logging.info("alert(%s) raise, but will not prompt", func_name)
        return False

    _, _, ts = clock.now()

    alert = {
        "sub_type": "camera",
        "device_id": cam.uuid,
        "device_name": cam.name,
        "host_id": SYS_CONF.get("host_id"),
        "host_ip": SYS_CONF.get("host_ip"),
        "func_name": func_name,
        "title": functl.get(func_name).get("title", ""),
        # TODO: alert_position should be renamed
        "alert_position": result_info,
        "create_time": ts,
        "location": cam.location,
        "status": "opening",
        "affirmed": False,
        "level": cam.function.get(func_name, {}).get("alert_level", functl.get(func_name).get("alert_level", "")),
    }

    aggreagate = cam.function[func_name].get("aggreagate", 60)
    warning_count = memory.WARNING_COUNT[cam.uuid].get(func_name, [])
    logging.info("driver.alert:: warning_count of func@{} is {}".format(func_name, warning_count))
    aggreagate_times = cam.function[func_name].get("aggreagate_times", 1)

    # aggreagate_times=1, 超过告警间隔才发送告警
    if aggreagate_times == 1:
        result = mongodb.alerts.find_one(
            {"create_time": {'$gte': (ts - aggreagate)},
             "device_id": cam.uuid, "func_name": func_name})
        if result:
            return False
        _save_alert_in_db(alert, image_path, cam)
        alert_data = copy.deepcopy(alert)
        alert_data.pop("_id", None)
        alert_data["create_time"] = ts * 1000
        Middle.CldT.report_alert(alert_data, cam, os.path.join(IMG_BASE_PATH, image_path))
        return True

    # aggreagate_times>1,aggreagate内连续告警超过aggreagate_tims才告警
    if len(warning_count) < aggreagate_times:
        memory.WARNING_COUNT[cam.uuid][func_name].append(ts)
        if len(memory.WARNING_COUNT[cam.uuid][func_name]) == aggreagate_times:
            if (ts - warning_count[0]) <= aggreagate:
                memory.WARNING_COUNT[cam.uuid][func_name].pop(0)
                result = mongodb.alerts.find_one(
                    {"create_time": {'$gte': (ts - aggreagate + 1)},
                     "device_id": cam.uuid, "func_name": func_name})
                if result:
                    return False
                _save_alert_in_db(alert, image_path, cam)
                alert_data = copy.deepcopy(alert)
                alert_data.pop("_id", None)
                alert_data["create_time"] = ts * 1000
                Middle.CldT.report_alert(alert_data, cam, os.path.join(IMG_BASE_PATH, image_path))
                return True
        return False

    # 当aggreagate_times改小时,只截取一部分ts
    if len(warning_count) >= aggreagate_times:
        memory.WARNING_COUNT[cam.uuid][func_name].append(ts)
        memory.WARNING_COUNT[cam.uuid][func_name] = memory.WARNING_COUNT[cam.uuid][func_name][-aggreagate_times:]
        if (ts - memory.WARNING_COUNT[cam.uuid][func_name][0]) <= aggreagate:
            memory.WARNING_COUNT[cam.uuid][func_name].pop(0)
            result = mongodb.alerts.find_one(
                {"create_time": {'$gte': (ts - aggreagate + 1)},
                 "device_id": cam.uuid, "func_name": func_name})
            if result:
                return False
            _save_alert_in_db(alert, image_path, cam)
            alert_data = copy.deepcopy(alert)
            alert_data.pop("_id", None)
            alert_data["create_time"] = ts * 1000
            Middle.CldT.report_alert(alert_data, cam, os.path.join(IMG_BASE_PATH, image_path))
            return True
        return False

    """
    aggregation_time = cam.function.get(func_name, {}).get("aggreagate", functl.get(func_name).get("aggreagate", 600))
    result = mongodb.alerts.find_one(
        {"create_time": {'$gte': (ts - aggregation_time)},
         "device_id": cam.uuid, "func_name": func_name})
    if result:
        return False
    mongodb.alerts.insert_one(alert)
    return True
    """

    """