Beispiel #1
0
    def update_cam(self, cam_type, cam_source, has_aoi, aoi_info):
        print("[INFO] Updating Cam ...")
        # print('  cam_type', cam_type)
        # print('  cam_source', cam_source)

        if cam_source == "0":
            cam_source = 0
        elif cam_source == "1":
            cam_source = 1
        elif cam_source == "2":
            cam_source = 2
        elif cam_source == "3":
            cam_source = 3

        if self.cam_type == cam_type and self.cam_source == cam_source:
            return

        self.cam_source = cam_source
        self.has_aoi = has_aoi
        self.aoi_info = aoi_info
        cam = cv2.VideoCapture(normalize_rtsp(cam_source))

        # Protected by Mutex
        self.lock.acquire()
        self.cam.release()
        self.cam = cam
        self.lock.release()
        update_instance(normalize_rtsp(cam_source))
Beispiel #2
0
    def __init__(self,
                 model_dir,
                 cam_type="video_file",
                 cam_source=CAM_SOURCE):
        # def __init__(self, model_dir, cam_type="video_file", cam_source="./mov_bbb.mp4"):
        # def __init__(self, model_dir, cam_type="video_file", cam_source="./sample_video/video_1min.mp4"):
        # def __init__(self, model_dir, cam_type="rtsp", cam_source="rtsp://52.229.36.89:554/media/catvideo.mkv"):
        # Default system params
        self.render = False

        self.lock = threading.Lock()

        self.cam_type = cam_type
        self.cam_source = cam_source
        self.cam = cv2.VideoCapture(normalize_rtsp(cam_source))
        self.cam_is_alive = False

        self.model = self.load_model(model_dir, is_default_model=True)
        self.model_uri = None

        self.last_img = None
        self.last_edge_img = None
        self.last_drawn_img = None
        self.last_prediction = []
        self.last_prediction_count = {}

        self.part_detection_id = None
        self.confidence_min = 30 * 0.01
        self.confidence_max = 30 * 0.01
        self.max_images = 10
        self.last_upload_time = 0
        self.is_upload_image = False
        self.current_uploaded_images = {}

        self.detection_success_num = 0
        self.detection_unidentified_num = 0
        self.detection_total = 0
        self.detections = []

        self.threshold = 0.3

        self.has_aoi = False
        self.aoi_info = None
        # Part that we want to detect
        self.parts = []

        self.is_gpu = (onnxruntime.get_device() == 'GPU')
        self.average_inference_time = 0

        # IoT Hub
        self.iothub_is_send = False
        self.iothub_threshold = 0.5
        self.iothub_fpm = 0
        self.iothub_last_send_time = time.time()
        self.iothub_interval = 99999999
Beispiel #3
0
    def restart_cam(self):

        print("[INFO] Restarting Cam")

        cam = cv2.VideoCapture(normalize_rtsp(self.cam_source))

        # Protected by Mutex
        self.lock.acquire()
        self.cam.release()
        self.cam = cam
        self.lock.release()
Beispiel #4
0
    def update_cam(self, cam_type, cam_source, frameRate, cam_id, has_aoi, aoi_info, scenario_type=None, line_info=None, zone_info=None):
        print('[INFO] Updating Cam ...', flush=True)

        # if self.cam_type == cam_type and self.cam_source == cam_source:
        #    return
        if self.cam_source != cam_source or round(self.frameRate) != round(frameRate):
            self.cam_source = cam_source
            self.frameRate = frameRate
            self._update_instance(normalize_rtsp(cam_source), str(frameRate))

        self.has_aoi = has_aoi
        self.aoi_info = aoi_info

        detection_mode = self.model.get_detection_mode()
        if detection_mode == 'PC':
            print('[INFO] Line INFO', line_info, flush=True)
            self.scenario = PartCounter()
            self.scenario_type = self.model.detection_mode
            try:
                line_info = json.loads(line_info)
                self.use_line = line_info['useCountingLine']
                lines = line_info['countingLines']
                if len(lines) > 0:
                    x1 = int(lines[0]['label'][0]['x'])
                    y1 = int(lines[0]['label'][0]['y'])
                    x2 = int(lines[0]['label'][1]['x'])
                    y2 = int(lines[0]['label'][1]['y'])
                    self.scenario.set_line(x1, y1, x2, y2)
                    print('Upading Line:', flush=True)
                    print('    use_line:', self.use_line, flush=True)
                    print('        line:', x1, y1, x2, y2, flush=True)
                else:
                    print('Upading Line:', flush=True)
                    print('    use_line:', self.use_line, flush=True)

            except:
                self.use_line = False
                print('Upading Line[*]:', flush=True)
                print('    use_line   :', False, flush=True)

        elif detection_mode == 'ES':
            print('[INFO] Zone INFO', zone_info, flush=True)
            self.scenario = DangerZone()
            self.scenario_type = self.model.detection_mode
            # FIXME
            self.scenario.set_targets(['Person'])
            try:
                zone_info = json.loads(zone_info)
                self.use_zone = zone_info['useDangerZone']
                zones = zone_info['dangerZones']
                _zones = []
                print('Upading Line:', flush=True)
                print('    use_zone:', self.use_zone, flush=True)
                for zone in zones:
                    x1 = int(zone['label']['x1'])
                    y1 = int(zone['label']['y1'])
                    x2 = int(zone['label']['x2'])
                    y2 = int(zone['label']['y2'])
                    _zones.append([x1, y1, x2, y2])
                    print('     zone:', x1, y1, x2, y2, flush=True)
                self.scenario.set_zones(_zones)

            except:
                self.use_zone = False
                print('Upading Zone[*]:', flush=True)
                print('    use_zone   :', False, flush=True)

        elif detection_mode == 'DD':
            print('[INFO] Line INFO', line_info, flush=True)
            self.scenario = DefeatDetection()
            self.scenario_type = self.model.detection_mode
            # FIXME
            self.scenario.set_ok('Bottle - OK')
            self.scenario.set_ng('Bottle - NG')
            try:
                line_info = json.loads(line_info)
                self.use_line = line_info['useCountingLine']
                lines = line_info['countingLines']
                if len(lines) > 0:
                    x1 = int(lines[0]['label'][0]['x'])
                    y1 = int(lines[0]['label'][0]['y'])
                    x2 = int(lines[0]['label'][1]['x'])
                    y2 = int(lines[0]['label'][1]['y'])
                    self.scenario.set_line(x1, y1, x2, y2)
                    print('Upading Line:', flush=True)
                    print('    use_line:', self.use_line, flush=True)
                    print('        line:', x1, y1, x2, y2, flush=True)
                else:
                    print('Upading Line:', flush=True)
                    print('    use_line:', self.use_line, flush=True)

            except:
                self.use_line = False
                print('Upading Line[*]:', flush=True)
                print('    use_line   :', False, flush=True)
        else:
            self.scenario = None
            self.scenario_type = self.model.detection_mode
    def update_cam(
        self,
        cam_type,
        cam_source,
        frameRate,
        lva_mode,
        cam_id,
        has_aoi,
        aoi_info,
        scenario_type=None,
        line_info=None,
        zone_info=None,
    ):
        logger.info("Updating Cam ...")

        # if self.cam_type == cam_type and self.cam_source == cam_source:
        #    return
        if (self.cam_source != cam_source
                or round(self.frameRate) != round(frameRate)
                or self.lva_mode != lva_mode):
            self.cam_source = cam_source
            self.frameRate = frameRate
            self.lva_mode = lva_mode
            if IS_OPENCV == "true":
                logger.info("post to CVModule")
                data = {
                    "stream_id": self.cam_id,
                    "rtsp": self.cam_source,
                    "fps": self.frameRate,
                    "endpoint": "http://InferenceModule:5000",
                }
                res = requests.post("http://CVCaptureModule:9000/streams",
                                    json=data)
            else:
                self._update_instance(normalize_rtsp(cam_source),
                                      str(frameRate))

        self.has_aoi = has_aoi
        self.aoi_info = aoi_info

        detection_mode = self.model.get_detection_mode()
        if detection_mode == "PC":
            print("[INFO] Line INFO", line_info, flush=True)
            self.scenario = PartCounter()
            self.scenario_type = self.model.detection_mode
            try:
                line_info = json.loads(line_info)
                self.use_line = line_info["useCountingLine"]
                lines = line_info["countingLines"]
                if len(lines) > 0:
                    x1 = int(lines[0]["label"][0]["x"])
                    y1 = int(lines[0]["label"][0]["y"])
                    x2 = int(lines[0]["label"][1]["x"])
                    y2 = int(lines[0]["label"][1]["y"])
                    self.scenario.set_line(x1, y1, x2, y2)
                    print("Upading Line:", flush=True)
                    print("    use_line:", self.use_line, flush=True)
                    print("        line:", x1, y1, x2, y2, flush=True)
                else:
                    print("Upading Line:", flush=True)
                    print("    use_line:", self.use_line, flush=True)

            except:
                self.use_line = False
                print("Upading Line[*]:", flush=True)
                print("    use_line   :", False, flush=True)

        elif detection_mode == "ES":
            print("[INFO] Zone INFO", zone_info, flush=True)
            self.scenario = DangerZone()
            self.scenario_type = self.model.detection_mode
            # FIXME
            self.scenario.set_targets(["Person"])
            try:
                zone_info = json.loads(zone_info)
                self.use_zone = zone_info["useDangerZone"]
                zones = zone_info["dangerZones"]
                _zones = []
                print("Upading Line:", flush=True)
                print("    use_zone:", self.use_zone, flush=True)
                for zone in zones:
                    x1 = int(zone["label"]["x1"])
                    y1 = int(zone["label"]["y1"])
                    x2 = int(zone["label"]["x2"])
                    y2 = int(zone["label"]["y2"])
                    _zones.append([x1, y1, x2, y2])
                    print("     zone:", x1, y1, x2, y2, flush=True)
                self.scenario.set_zones(_zones)

            except:
                self.use_zone = False
                print("Upading Zone[*]:", flush=True)
                print("    use_zone   :", False, flush=True)

        elif detection_mode == "DD":
            print("[INFO] Line INFO", line_info, flush=True)
            self.scenario = DefeatDetection()
            self.scenario_type = self.model.detection_mode
            # FIXME
            self.scenario.set_ok("Bottle - OK")
            self.scenario.set_ng("Bottle - NG")
            try:
                line_info = json.loads(line_info)
                self.use_line = line_info["useCountingLine"]
                lines = line_info["countingLines"]
                if len(lines) > 0:
                    x1 = int(lines[0]["label"][0]["x"])
                    y1 = int(lines[0]["label"][0]["y"])
                    x2 = int(lines[0]["label"][1]["x"])
                    y2 = int(lines[0]["label"][1]["y"])
                    self.scenario.set_line(x1, y1, x2, y2)
                    print("Upading Line:", flush=True)
                    print("    use_line:", self.use_line, flush=True)
                    print("        line:", x1, y1, x2, y2, flush=True)
                else:
                    print("Upading Line:", flush=True)
                    print("    use_line:", self.use_line, flush=True)

            except:
                self.use_line = False
                print("Upading Line[*]:", flush=True)
                print("    use_line   :", False, flush=True)
        else:
            self.scenario = None
            self.scenario_type = self.model.detection_mode
    def update_cam(
        self,
        cam_type,
        cam_source,
        frameRate,
        recording_duration,
        lva_mode,
        cam_id,
        cam_name,
        has_aoi,
        aoi_info,
        scenario_type=None,
        line_info=None,
        zone_info=None,
    ):
        logger.info("Updating Cam ...")

        # if self.cam_type == cam_type and self.cam_source == cam_source:
        #    return
        if (self.cam_source != cam_source
                or round(self.frameRate) != round(frameRate)
                or self.lva_mode != lva_mode
                or self.recording_duration != recording_duration):
            self.cam_source = cam_source
            self.frameRate = frameRate
            self.lva_mode = lva_mode
            self.recording_duration = recording_duration
            if IS_OPENCV == "true":
                logger.info("post to CVModule")
                data = {
                    "stream_id": self.cam_id,
                    "rtsp": self.cam_source,
                    "fps": self.frameRate,
                    "endpoint": "http://inferencemodule:5000",
                }
                res = requests.post("http://cvcapturemodule:9000/streams",
                                    json=data)
            else:
                self._update_instance(normalize_rtsp(cam_source),
                                      str(frameRate), str(recording_duration))

        self.name = cam_name
        self.has_aoi = has_aoi
        self.aoi_info = aoi_info

        detection_mode = self.model.get_detection_mode()
        if detection_mode == "PD":
            self.scenario = PartDetection()
            self.scenario.set_parts(self.model.parts)
            self.scenario_type = self.model.detection_mode

        elif detection_mode == "PC":
            print("[INFO] Line INFO", line_info, flush=True)
            self.scenario = PartCounter()
            self.scenario_type = self.model.detection_mode
            try:
                line_info = json.loads(line_info)
                self.use_line = line_info["useCountingLine"]
                lines = line_info["countingLines"]
                lines_to_set = []
                if len(lines) > 0:
                    for i in range(len(lines)):
                        x1 = int(lines[i]["label"][0]["x"])
                        y1 = int(lines[i]["label"][0]["y"])
                        x2 = int(lines[i]["label"][1]["x"])
                        y2 = int(lines[i]["label"][1]["y"])
                        line_id = str(lines[i]['order'])
                        print("        line:",
                              x1,
                              y1,
                              x2,
                              y2,
                              line_id,
                              flush=True)
                        lines_to_set.append([x1, y1, x2, y2, line_id])
                    self.scenario.set_line(lines_to_set)
                    print("Upading Line:", flush=True)
                    print("    use_line:", self.use_line, flush=True)
                else:
                    print("Upading Line:", flush=True)
                    print("    use_line:", self.use_line, flush=True)

            except:
                self.use_line = False
                print("Upading Line[*]:", flush=True)
                print("    use_line   :", False, flush=True)

        elif detection_mode in ["ES", "ESA", "TCC", "CQA"]:
            class_obj = [DangerZone, ShelfZone, CountingZone, QueueZone]
            print("[INFO] Zone INFO", zone_info, flush=True)
            self.scenario = class_obj[["ES", "ESA", "TCC",
                                       "CQA"].index(detection_mode)]()
            self.scenario_type = self.model.detection_mode
            # FIXME
            self.scenario.set_targets(self.model.parts)
            try:
                zone_info = json.loads(zone_info)
                self.use_zone = zone_info["useDangerZone"]
                zones = zone_info["dangerZones"]
                _zones = []
                print("Upading Line:", flush=True)
                print("    use_zone:", self.use_zone, flush=True)
                # for zone in zones:
                #     x1 = int(zone["label"]["x1"])
                #     y1 = int(zone["label"]["y1"])
                #     x2 = int(zone["label"]["x2"])
                #     y2 = int(zone["label"]["y2"])
                #     zone_id = str(zone['order'])
                #     _zones.append([x1, y1, x2, y2, zone_id])
                #     print("     zone:", x1, y1, x2, y2, flush=True)
                # self.scenario.set_zones(_zones)
                self.scenario.set_zones(zones)

            except:
                self.use_zone = False
                print("Upading Zone[*]:", flush=True)
                print("    use_zone   :", False, flush=True)

        elif detection_mode == "DD":
            print("[INFO] Line INFO", line_info, flush=True)
            self.scenario = DefeatDetection()
            self.scenario_type = self.model.detection_mode
            # FIXME
            self.scenario.set_ok("Bottle - OK")
            self.scenario.set_ng("Bottle - NG")
            try:
                line_info = json.loads(line_info)
                self.use_line = line_info["useCountingLine"]
                lines = line_info["countingLines"]
                if len(lines) > 0:
                    x1 = int(lines[0]["label"][0]["x"])
                    y1 = int(lines[0]["label"][0]["y"])
                    x2 = int(lines[0]["label"][1]["x"])
                    y2 = int(lines[0]["label"][1]["y"])
                    self.scenario.set_line(x1, y1, x2, y2)
                    print("Upading Line:", flush=True)
                    print("    use_line:", self.use_line, flush=True)
                    print("        line:", x1, y1, x2, y2, flush=True)
                else:
                    print("Upading Line:", flush=True)
                    print("    use_line:", self.use_line, flush=True)

            except:
                self.use_line = False
                print("Upading Line[*]:", flush=True)
                print("    use_line   :", False, flush=True)

        else:
            self.scenario = None
            self.scenario_type = self.model.detection_mode