import subprocess

import freenect
import numpy as np
from cv2 import cv2
from time import sleep


# init kinect
mdev = freenect.open_device(freenect.init(), 0)
freenect.set_depth_mode(mdev, freenect.RESOLUTION_MEDIUM, freenect.DEPTH_11BIT)
# freenect.set_video_mode(mdev, freenect.RESOLUTION_MEDIUM, freenect.VIDEO_IR_8BIT)
freenect.set_video_mode(mdev, freenect.RESOLUTION_HIGH, freenect.VIDEO_IR_8BIT)

keep_running = True


def save_ir(dev, data, timestamp):
    data = np.dstack((data, data, data)).astype(np.uint8)
    diff = (512 - 480) // 2
    downsampled = cv2.pyrDown(data)
    data = downsampled[diff:downsampled.shape[0] - diff, 0:downsampled.shape[1]]

    cv2.imwrite('kinect_ir.png', data)

    print('reading pid')
    with open('frontend.pid', 'r') as f:
        subprocess.run(['kill', '-USR1', f.read()])
        print('sending signal')
    sleep(1)


def body(*args):
    if not keep_running:
        raise freenect.Kill


freenect.runloop(
    # depth=display_depth,
    depth=None,
    video=save_ir,
    body=body,
    dev=mdev,
)
