def parse_cptv(cptv_file, config, thermal_config_file, preview_type):
    with open(cptv_file, "rb") as f:
        reader = CPTVReader(f)

        headers = HeaderInfo(
            res_x=reader.x_resolution,
            res_y=reader.y_resolution,
            fps=9,
            brand=reader.brand.decode() if reader.brand else None,
            model=reader.model.decode() if reader.model else None,
            frame_size=reader.x_resolution * reader.y_resolution * 2,
            pixel_bits=16,
            serial="",
            firmware="",
        )
        thermal_config = ThermalConfig.load_from_file(thermal_config_file,
                                                      headers.model)
        pi_classifier = PiClassifier(
            config,
            thermal_config,
            headers,
            thermal_config.motion.run_classifier,
            0,
            preview_type,
        )
        for frame in reader:
            if frame.background_frame:
                pi_classifier.motion_detector.background = frame.pix
                continue
            frame.received_at = time.time()
            pi_classifier.process_frame(frame)
        pi_classifier.disconnected()
Beispiel #2
0
def main():
    logging.root.removeHandler(absl.logging._absl_handler)
    absl.logging._warn_preinit_stderr = False
    init_logging()
    args = parse_args()

    config = Config.load_from_file(args.config_file)
    thermal_config = ThermalConfig.load_from_file(args.thermal_config_file)

    if args.cptv:
        return parse_cptv(args.cptv, config, thermal_config)

    try:
        os.unlink(SOCKET_NAME)
    except OSError:
        if os.path.exists(SOCKET_NAME):
            raise

    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.bind(SOCKET_NAME)
    sock.listen(1)
    while True:
        logging.info("waiting for a connection")
        connection, client_address = sock.accept()
        logging.info("connection from %s", client_address)
        try:
            handle_connection(connection, config, thermal_config)
        finally:
            # Clean up the connection
            connection.close()
Beispiel #3
0
def main():
    args = parse_args()
    init_logging()

    config = Config.load_from_file(args.config_file)
    thermal_config = ThermalConfig.load_from_file(args.thermal_config_file)
    print("detecting on  " + args.cptv)
    with open(args.cptv, "rb") as f:
        reader = CPTVReader(f)

        headers = HeaderInfo(
            res_x=reader.x_resolution,
            res_y=reader.y_resolution,
            fps=9,
            brand="",
            model="",
            frame_size=reader.x_resolution * reader.y_resolution * 2,
            pixel_bits=16,
        )

        motion_detector = MotionDetector(
            thermal_config, config.tracking.motion_config.dynamic_thresh, None,
            headers)
        for i, frame in enumerate(reader):
            motion_detector.process_frame(frame)
def handle_connection(connection, config, thermal_config_file):
    headers, extra_b = handle_headers(connection)
    thermal_config = ThermalConfig.load_from_file(thermal_config_file,
                                                  headers.model)
    logging.info("parsed camera headers %s running with config %s", headers,
                 thermal_config)

    process_queue = multiprocessing.Queue()

    processor = get_processor(process_queue, config, thermal_config, headers)
    processor.start()

    edge = config.tracking.edge_pixels
    crop_rectangle = tools.Rectangle(edge, edge, headers.res_x - 2 * edge,
                                     headers.res_y - 2 * edge)
    raw_frame = lepton3.Lepton3(headers)
    read = 0
    try:
        while True:
            if extra_b is not None:
                data = extra_b + connection.recv(
                    headers.frame_size - len(extra_b), socket.MSG_WAITALL)
                extra_b = None
            else:
                data = connection.recv(headers.frame_size, socket.MSG_WAITALL)

            if not data:
                logging.info("disconnected from camera")
                process_queue.put(STOP_SIGNAL)
                break
            try:
                message = data[:5].decode("utf-8")
                if message == "clear":
                    logging.info("processing error from camera")
                    process_queue.put(STOP_SIGNAL)
                    break
            except:
                pass
            read += 1
            frame = raw_frame.parse(data)
            frame.received_at = time.time()
            cropped_frame = crop_rectangle.subimage(frame.pix)
            t_max = np.amax(cropped_frame)
            t_min = np.amin(cropped_frame)
            # seems to happen if pi is working hard
            if t_min == 0:
                logging.warning(
                    "received frame has odd values skipping thermal frame max {} thermal frame min {} cpu % {} memory % {}"
                    .format(t_max, t_min, psutil.cpu_percent(),
                            psutil.virtual_memory()[2]))
                process_queue.put(SKIP_SIGNAL)
            elif read < 100:
                process_queue.put(SKIP_SIGNAL)
            else:
                process_queue.put(frame)
    finally:
        time.sleep(5)
        # give it a moment to close down properly
        processor.terminate()
def main():
    logging.root.removeHandler(absl.logging._absl_handler)
    absl.logging._warn_preinit_stderr = False
    init_logging()
    args = parse_args()

    config = Config.load_from_file()
    thermal_config = ThermalConfig.load_from_file()
    proccesor = None
    if thermal_config.motion.run_classifier:
        classifier = get_classifier(config)
        proccesor = PiClassifier(config, thermal_config, classifier)
    else:
        proccesor = MotionDetector(
            config.res_x,
            config.res_y,
            thermal_config,
            config.tracking.dynamic_thresh,
            CPTVRecorder(thermal_config),
        )
    if args.cptv:
        with open(args.cptv, "rb") as f:
            reader = CPTVReader(f)
            for frame in reader:
                proccesor.process_frame(frame)

        proccesor.disconnected()
        return

    service = SnapshotService(proccesor)
    try:
        os.unlink(SOCKET_NAME)
    except OSError:
        if os.path.exists(SOCKET_NAME):
            raise

    sock = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
    sock.bind(SOCKET_NAME)
    sock.listen(1)
    while True:
        logging.info("waiting for a connection")
        connection, client_address = sock.accept()
        logging.info("connection from %s", client_address)
        try:
            handle_connection(connection, proccesor)
        finally:
            # Clean up the connection
            connection.close()