def __init__(self, *args, **kwargs):
     super(TestPipeline, self).__init__(*args, **kwargs)
     self.testHelper = TestHelper()
     self.log = self.testHelper.CreateLog(TestPipeline.__name__)
     TestPipeline.log = self.log
     self.archiver = CameraArchiveHelper(self.log)
     self.helper = CommonHelper()
Esempio n. 2
0
    def CreateLog(self, name: str):

        file_error_handler = logging.FileHandler(filename=name +
                                                 '-test-error.log',
                                                 encoding='utf-8')
        file_error_handler.setLevel(logging.ERROR)
        file_handler = logging.FileHandler(filename=name + '-test.log',
                                           mode='w',
                                           encoding='utf-8')
        stdout_handler = logging.StreamHandler(sys.stdout)
        handlers = [file_handler, stdout_handler, file_error_handler]

        logging.basicConfig(
            format=
            '%(asctime)s|%(levelname)-.3s|%(name)s: %(message)s',  # \t####=> %(filename)s:%(lineno)d 
            level=logging.DEBUG,
            datefmt='%H:%M:%S',
            handlers=handlers)
        log = logging.getLogger("TEST")

        self.helper = CommonHelper()

        self.helper.installColoredLog(log)
        log.info(f'start {__name__}: ⏱️ {datetime.datetime.now()}')
        return log
Esempio n. 3
0
 def __init__(self):
     # self.Result = ProcessingResult()
     # self.Result.Summary = {}
     self.log = logging.getLogger(
         "PROC:DIFF")  # :{shot.filenameWithoutExtension}
     # self.shot = ctx.Shot
     # self.originalShot = ctx.OriginalShot
     # self.originalShots = ctx.OriginalShots
     # self.shots = ctx.Shots
     self.helper = CommonHelper()
Esempio n. 4
0
 def __init__(self, camera: str, datetime: datetime, isSimulation=False):
     super().__init__("ELSE")
     self.camera = camera
     self.datetime = datetime
     self.helper = CommonHelper()
     self.isSimulation = isSimulation
     (self.elasticsearch_host, self.elasticsearch_port) = (None, None)
     if AppSettings.ELASTICSEARCH_HOST:
         (self.elasticsearch_host, self.elasticsearch_port
          ) = AppSettings.ELASTICSEARCH_HOST.split(':')
    def move_files(self, files, config):
        common = CommonHelper()
        elastic = ElasticSearchHelper()
        exts = {}

        last_file_dir_relative_to = ''
        bytes_moved = 0
        files_moved = 0
        for file in files:
            dir_relative_to = file.to.get_dir_relative(config.path_to)
            dir_relative_frm = file.frm.get_dir_relative(config.pathFrom())
            if last_file_dir_relative_to != dir_relative_to:
                if last_file_dir_relative_to != '':
                    ext_stat = ' '.join(
                        ["{}:{}".format(k.upper(), exts[k]) for k in exts])
                    self.log.info('\t{} files moved: {}. Total {}'.format(
                        files_moved, ext_stat, common.size_human(bytes_moved)))
                    files_moved = 0
                    bytes_moved = 0
                    exts = {}
                self.log.info('{} => {}'.format(dir_relative_frm,
                                                dir_relative_to))

            files_moved += 1
            bytes_moved += file.frm.size()
            #print('\t - {} ({})'.format(file.frm.filename, file.frm.size_human()))
            # print(os.path.dirname(file.path_to))
            #pprint(file.__dict__)
            os.makedirs(file.to.dir, exist_ok=True)
            ## os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
            ##shutil.copy2(file.frm.path, file.to.path)
            if self.isSimulation:
                self.log.info(f'{file.frm.path} => {file.to.path}')
            else:
                shutil.move(file.frm.path, file.to.path)
                elastic.report_to_elastic(file)

            ext = file.to.get_extension()
            if ext not in exts:
                exts[ext] = 0
            exts[ext] += 1

            last_file_dir_relative_to = dir_relative_to
            #break

        ext_stat = ' '.join(["{}:{}".format(k.upper(), exts[k]) for k in exts])
        self.log.info('\t{} files moved: {}. Total {}'.format(
            files_moved, ext_stat, common.size_human(bytes_moved)))
        files_moved = 0
        bytes_moved = 0
        self.log.info(
            '########################################################')
Esempio n. 6
0
    def __init__(self, filename=None):
        if not filename:
            return
        self.filename = filename
        self.helper = CommonHelper()
        self.log = logging.getLogger(f"META")

        self.log.info(f'========== {filename} ==========')
        self.ProcessFileName()

        if self.mediatype == 'image':
            self.ProcessImageMeta()
        elif self.mediatype == 'video':
            self.ProcessVideoMeta()

        self.ProcessFileAttributes()
Esempio n. 7
0
 def __init__(self, yolo_data):
     self.log = logging.getLogger(f"PROC:TRAC:TBox")
     self.helper = CommonHelper()
     self.image = []
     self.object_id = None
     (self.w, self.h) = yolo_data['size']
     self.center = yolo_data['center_coordinate']
     self.yolo_label = yolo_data['label']
     self.id = f"{self.center[0]}x{self.center[1]}"
     (self.x, self.y) = self.center
     (self.point_left_top,
      self.point_right_bottom) = self.TransformCenterToLimits(
          self.x, self.y, self.point_size, self.point_size)
     (self.box_left_top,
      self.box_right_bottom) = self.TransformCenterToLimits(
          self.x, self.y, self.h, self.w)
     self.pos_text = (self.x, self.y - 5)
Esempio n. 8
0
class CameraArchiveConfig:
    sensor: {}
    position: {}
    camera: str
    imap_folder: str
    path_from: str
    path_to: str
    ignore_dir: []
    camera_triggered_interval_sec: int

    helper = CommonHelper()

    def __init__(self):
        self.ignore_dir = []
        self.secretConfig = SecretConfig()
        self.secretConfig.fromJsonFile()

    def fromJsonFile(self, filename: str):
        with open(filename, "r") as read_file:
            self.__dict__ = json.load(read_file)
        if not hasattr(self, 'ignore_dir'):
            self.ignore_dir = []

    def fromJson(self, json_dump):
        self.__dict__ = json.loads(json_dump)
        if not hasattr(self, 'ignore_dir'):
            self.ignore_dir = []

    def toJson(self):
        return json.dumps(self.__dict__, indent=4)

    def __repr__(self):
        return 'CONFIG: {}: = {}'.format(self.camera, self.path_from)

    def pathFrom(self):
        return os.path.join(AppSettings.CAMERA_LIVE_PATH, self.path_from)

    def pathTo(self):
        return self.path_to
 def __init__(self, tempFolder='temp'):
     super().__init__("IMAP")
     self.secretConfig = SecretConfig()
     self.secretConfig.fromJsonFile()
     self.tempFolder = tempFolder
     self.helper = CommonHelper()
Esempio n. 10
0
'''
run locally :
> set FLASK_APP=app.py
> flask run
    -OR-
> run.cmd
'''

temp = 'temp'
imap_folder = 'camera/foscam'
camera = 'Foscam'
isSimulation = False
secretConfig = SecretConfig()
secretConfig.fromJsonFile()
helper = CommonHelper()

file_error_handler = logging.FileHandler(filename='camera-tools-error.log')
file_error_handler.setLevel(logging.ERROR)
file_handler = logging.handlers.TimedRotatingFileHandler('camera-tools.log',
                                                         when='midnight',
                                                         backupCount=7)
file_handler.suffix = '_%Y-%m-%d.log'
stdout_handler = logging.StreamHandler(sys.stdout)
handlers = [file_handler, stdout_handler, file_error_handler]

logging.basicConfig(
    format=
    '%(asctime)s|%(levelname)-.3s|%(name)s: %(message)s',  # \t####=> %(filename)s:%(lineno)d 
    level=logging.DEBUG,
    datefmt='%H:%M:%S',
Esempio n. 11
0
    def __init__(self, full_filename):
        self.helper = CommonHelper()

        self.path = full_filename
        self.filename = os.path.basename(full_filename)
        self.dir = os.path.dirname(full_filename)
Esempio n. 12
0
 def __init__(self, name, isSimulation=False):
     super().__init__(name, isSimulation)
     self.helper = CommonHelper()
Esempio n. 13
0
class ApiContext:
    # class ApiContextInstance:
    #     def __init__(self, arg):
    #         self.val = arg
    # Instance = None
    IsInitialized = False
    Log = None
    Helper = CommonHelper()
    CameraArchive: CameraArchiveHelper
    ElasticSearch: ElasticSearchHelper
    ShotsPipeline: ShotsPipeline

    def __init__(self, arg):
        if not ApiContext.IsInitialized:
            self.InitPrivate()
            ApiContext.IsInitialized = True
        # if not ApiContext.Instance:
        #     ApiContext.Instance = ApiContext.ApiContextInstance("TODO")
        # else:
        #     ApiContext.Instance.val = arg

    def InitPrivate(self):

        file_error_handler = logging.FileHandler(
            filename='logs/camera-tools-error.log', encoding='utf-8')
        file_error_handler.setLevel(logging.ERROR)
        file_handler = logging.handlers.TimedRotatingFileHandler(
            'logs/camera-tools.log',
            when='midnight',
            backupCount=7,
            encoding='utf-8')
        file_handler.suffix = '_%Y-%m-%d.log'
        # html_handler = HtmlLogger.HTMLRotatingFileHandler('camera-tools.html')
        # html_handler.suffix = '_%Y-%m-%d_%H.html'
        # html_handler.setFormatter(HtmlLogger.HTMLFormatter())
        locale.getpreferredencoding()  #need for display emoji in terminal
        stdout_handler = logging.StreamHandler(sys.stdout)
        handlers = [file_handler, stdout_handler, file_error_handler]

        logging.basicConfig(
            format=
            '%(asctime)s|%(levelname)-.3s|%(name)s: %(message)s',  # \t####=> %(filename)s:%(lineno)d 
            level=logging.DEBUG,
            datefmt='%H:%M:%S',
            handlers=handlers)

        self.log = logging.getLogger("API")
        ApiContext.Log = self.log
        self.log.info(
            'ℹ️ |############################################################|'
        )
        self.log.info(
            f'ℹ️ |####### start API @ {str(datetime.datetime.now()) + " ":#<40}|'
        )
        self.log.info(
            'ℹ️ |############################################################|'
        )
        self.log.info("USED_SETTINGS: " + AppSettings.USED_SETTINGS)
        self.Helper.installColoredLog(self.log)

        # Some examples.
        # self.log.debug("this is a debugging message")
        # self.log.info("this is an informational message")
        # self.log.warning("this is a warning message")
        # self.log.error("this is an error message")
        # self.log.critical("this is a critical message")

        self.isSimulation = False
        self.secretConfig = SecretConfig()
        self.secretConfig.fromJsonFile()

        ApiContext.CameraArchive = CameraArchiveHelper(self.log)
        ApiContext.ElasticSearch = ElasticSearchHelper()

        self.camera = 'Foscam'
        self.shotsPipeline = ShotsPipeline(self.camera, self.log)
        self.InitShotsPipeline()
        ApiContext.ShotsPipeline = self.shotsPipeline

        self.InitDnsPipeline()
        ApiContext.DnsPipeline = self.dnsPipeline

        self.InitPhotosPipeline()
        ApiContext.PhotosSergeyPipeline = self.photosSergeyPipeline
        ApiContext.PhotosLiliaPipeline = self.photosLiliaPipeline

        self.log.info(
            f'initialization API finished @ {datetime.datetime.now()}')

    def InitPhotosPipeline(self):
        self.photosSergeyPipeline = Pipeline()
        self.photosSergeyPipeline.providers.append(
            FilesWalkerProvider("../camera-OpenCV-data/Mobile", ['Lilia']))
        self.photosSergeyPipeline.processors.append(
            MediaCreationDateProcessor())
        self.photosSergeyPipeline.processors.append(
            PhotosArrangeProcessor('Mobile Sergey'))

        self.photosLiliaPipeline = Pipeline()
        self.photosLiliaPipeline.providers.append(
            FilesWalkerProvider("../camera-OpenCV-data/Mobile/Lilia"))
        self.photosLiliaPipeline.processors.append(
            MediaCreationDateProcessor())
        self.photosLiliaPipeline.processors.append(
            PhotosArrangeProcessor('Mobile Lilia'))

    def InitShotsPipeline(self):
        self.shotsPipeline.providers.clear()
        self.shotsPipeline.processors.clear()
        self.shotsPipeline.providers.append(ImapShotsProvider())
        self.shotsPipeline.providers.append(DirectoryShotsProvider())
        self.shotsPipeline.processors.append(DiffContoursProcessor())
        self.shotsPipeline.processors.append(YoloObjDetectionProcessor())
        self.shotsPipeline.processors.append(TrackingProcessor())
        self.shotsPipeline.processors.append(SaveToTempProcessor())
        self.shotsPipeline.processors.append(MailSenderProcessor())
        self.shotsPipeline.processors.append(
            HassioProcessor('temp' if self.isSimulation else None))
        self.shotsPipeline.processors.append(
            ArchiveProcessor(self.isSimulation))
        self.shotsPipeline.processors.append(
            ElasticSearchProcessor(self.isSimulation))
        self.shotsPipeline.PreLoad()

    def InitDnsPipeline(self):
        self.dnsPipeline = Pipeline(self.log)
        self.dnsPipeline.providers.append(DnsAdGuardProvider())
        self.dnsPipeline.processors.append(ElasticSearchDnsProcessor())
 def __init__(self, *args, **kwargs):
     super(TestArchPhotos, self).__init__(*args, **kwargs)
     self.testHelper = TestHelper()
     self.helper = CommonHelper()
     self.log = self.testHelper.CreateLog(TestArchPhotos.__name__)
 def __init__(self, pShot: PipelineShot, yolo: YoloContext):
     self.pShot = pShot
     self.log = logging.getLogger(f"PROC:YOLO")
     self.boxes = []
     self.yolo = yolo
     self.helper = CommonHelper()
Esempio n. 16
0
 def __init__(self):
     self.log = logging.getLogger('IMAP')
     self.config = SecretConfig()
     self.config.fromJsonFile()
     self.helper = CommonHelper()
 def __init__(self, isDebug=False):
     super().__init__("TRAC")
     self.boxes_last = []
     self.helper = CommonHelper()
     self.isDebug = isDebug
Esempio n. 18
0
class Shot:
    filename = ''
    contourArea: int
    image: any
    image_timestamp: any
    Contours = []
    YoloResult = []
    datetime: datetime
    helper = CommonHelper()
    imageAnalyseResult = ImageAnalyseResult()
    magnifiedRegion = []

    def FromFile(self, path: str):
        self = Shot()
        self.log = logging.getLogger("SHOT")
        self.filename = path
        self.log.info('Open file: {}'.format(self.filename))
        self.image = cv2.imread(self.filename, cv2.IMREAD_GRAYSCALE)
        self.image_color = cv2.imread(self.filename, cv2.IMREAD_COLOR)
        self.image_contours = self.image_color.copy()
        if len(self.image) == 0:
            raise ValueError('cant load: {}'.format(self.filename))
        self.image_timestamp = self.image[:22, :230]
        self.image[:22, :230] = 0  # remove timestamp

        self.datetime = self.helper.get_datetime(self.filename)

        return self

    def show_cv(self):
        cv2.imshow('shot.show_cv (press any key for close)', self.image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    def show_plt(self):
        plt.axis("off")
        #plt.title('shot.show_plt')
        img = cv2.cvtColor(self.image_contours, cv2.COLOR_BGR2RGB)
        plt.imshow(img, interpolation="bilinear")

    def DrawContours(self):
        cv2.drawContours(self.image_contours, self.Contours, -1, (0, 255, 255),
                         1)
        for c in self.Contours[0:2]:
            area = int(cv2.contourArea(c) / 100)
            #print('Contour: {}'.format(area))

            (x, y, w, h) = cv2.boundingRect(c)
            cv2.rectangle(self.image_contours, (x, y), (x + w, y + h),
                          (0, 255, 0), 1, 8)
            cv2.putText(self.image_contours, str(area), (x, y - 3),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2,
                        cv2.LINE_AA)

    def MagnifyMotion(self):
        if len(self.Contours) == 0:
            return

        counts = 1
        margin = 5
        zoom = 2

        for c in self.Contours[0:counts]:
            (x, y, w, h) = cv2.boundingRect(c)
            if math.sqrt(w * w + h * h) > 200:
                return

            self.magnifiedRegion = [x, y, w, h]
            img = self.image_color
            motion_area = img[y:(y + h), x:(x + w)]
            img_xmax = len(img[0])
            img_ymax = len(img)

            motion_area = cv2.resize(motion_area,
                                     None,
                                     fx=zoom,
                                     fy=zoom,
                                     interpolation=cv2.INTER_CUBIC)
            y_max = img_ymax - margin
            y_min = y_max - zoom * h
            x_max = img_xmax - margin
            x_min = x_max - zoom * w

            if y_min < margin or x_min < margin:
                continue  # can fit, too large

            self.image_contours[y_min:y_max, x_min:x_max] = motion_area
            cv2.rectangle(self.image_contours, (x_min, y_min), (x_max, y_max),
                          (127, 127, 127), 2)

    def CalcHaarBody(self):
        cascadePath = "cascade\\haarcascade_frontalface_alt.xml"
        cascade = cv2.CascadeClassifier(cascadePath)
        objects = cascade.detectMultiScale(self.image,
                                           scaleFactor=1.1,
                                           minNeighbors=1,
                                           minSize=(30, 30))
        #print('=HAAR=: Detected bodies : {}'.format(len(objects)))
        for (x, y, w, h) in objects:
            #crop = image[y: y + h, x: x + w]
            cv2.rectangle(self.image_contours, (x, y), (x + w, y + h),
                          (255, 0, 0), 2)

    def CalcContoursAnalyseResult(self):
        self.imageAnalyseResult.contours = []

        for c in self.Contours:

            area = int(cv2.contourArea(c))
            #print('Contour: {}'.format(area))

            (x, y, w, h) = cv2.boundingRect(c)
            (center_x, center_y) = (x + w // 2, y + h // 2)

            result = ContourAnalyseResult()
            result.area = area
            result.profile_proportion = round(h / w, 2)
            result.center_coordinate = [center_x, center_y]
            self.imageAnalyseResult.contours.append(result)
Esempio n. 19
0
 def __init__(self, label: str, isSimulation: bool = False):
     super().__init__("PHOT", isSimulation)
     self.label = label
     self.helper = CommonHelper()
 def __init__(self, root: str = None, ignoreDirs=[], condition=None):
     super().__init__("WALK")
     self.helper = CommonHelper()
     self.root = root
     self.ignoreDirs = ignoreDirs
     self.condition = condition if condition != None else (lambda f: True)
Esempio n. 21
0
 def __init__(self, folder: str = None):
     super().__init__("DIRC")
     self.helper = CommonHelper()
     self.folder = folder
     self.SourceImagePattern = re.compile(AppSettings.SOURCE_IMAGE_PATTARN)
 def __init__(self, isSimulation: bool = False):
     super().__init__("SMTP")
     self.secretConfig = SecretConfig()
     self.secretConfig.fromJsonFile()
     self.isSimulation = isSimulation
     self.helper = CommonHelper()