import argparse
import cv2

import common
# from .utils.cropAtCenter import cropImageCenter
# from cropAtCenter import cropImageCenter

from gabriel_client.websocket_client import WebsocketClient
from gabriel_client.opencv_adapter import OpencvAdapter

DEFAULT_SERVER_HOST = '128.2.212.50'
DEFAULT_ZOOM_FACTOR = 10

def preprocess(frame):
    # return frame

    print(type(frame), frame.shape)

    width, height = frame.shape[1], frame.shape[0]

    left = int(width/2 * (1 - 1/DEFAULT_ZOOM_FACTOR))
    top = int(height/2 * (1 - 1/DEFAULT_ZOOM_FACTOR))
    right = int(width/2 * (1 + 1/DEFAULT_ZOOM_FACTOR))
    bottom = int(height/2 * (1 + 1/DEFAULT_ZOOM_FACTOR))

    cropped_frame = frame[top:bottom, left:right]
    return cropped_frame


def produce_extras():
    return None


def consume_frame(frame, _):
    cv2.imshow('Image from server', frame)
    cv2.waitKey(1)


def main():
    common.configure_logging()
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'source_name', nargs='?', default=common.DEFAULT_SOURCE_NAME)
    parser.add_argument('server_host', nargs='?', default=DEFAULT_SERVER_HOST)
    args = parser.parse_args()

    capture = cv2.VideoCapture(0)
    opencv_adapter = OpencvAdapter(
        preprocess, produce_extras, consume_frame, capture, args.source_name)

    client = WebsocketClient(
        args.server_host, common.WEBSOCKET_PORT,
        opencv_adapter.get_producer_wrappers(), opencv_adapter.consumer)
    client.launch()


if __name__ == '__main__':
    main()

[((35, 4, 35, 42), 'cv2.imshow', 'cv2.imshow', ({(35, 15, 35, 34): '"""Image from server"""', (35, 36, 35, 41): 'frame'}, {}), "('Image from server', frame)", False, 'import cv2\n'), ((36, 4, 36, 18), 'cv2.waitKey', 'cv2.waitKey', ({(36, 16, 36, 17): '(1)'}, {}), '(1)', False, 'import cv2\n'), ((40, 4, 40, 30), 'common.configure_logging', 'common.configure_logging', ({}, {}), '()', False, 'import common\n'), ((41, 13, 41, 38), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ({}, {}), '()', False, 'import argparse\n'), ((47, 14, 47, 33), 'cv2.VideoCapture', 'cv2.VideoCapture', ({(47, 31, 47, 32): '0'}, {}), '(0)', False, 'import cv2\n'), ((48, 21, 49, 77), 'gabriel_client.opencv_adapter.OpencvAdapter', 'OpencvAdapter', ({(49, 8, 49, 18): 'preprocess', (49, 20, 49, 34): 'produce_extras', (49, 36, 49, 49): 'consume_frame', (49, 51, 49, 58): 'capture', (49, 60, 49, 76): 'args.source_name'}, {}), '(preprocess, produce_extras, consume_frame, capture, args.\n    source_name)', False, 'from gabriel_client.opencv_adapter import OpencvAdapter\n')]