#!/usr/bin/env python
#version    :       2023.12.31
#language   :       ch
from maix import camera
from maix import display

from maix import image

import numpy as np

import os
ScreenOrientation = False
try:
    if os.path.exists("/etc/cameraSize.cfg"):
        cameraSize = True
    else:
        cameraSize = False
except:
    cameraSize = False
def getLcdRotation(cameraCapture):
    global cameraSize
    if cameraSize:
        return lcdRotationNew(cameraCapture)
    else:
        return lcdRotation(cameraCapture)

def lcdRotationNew(inputImg):
    global cameraSize,ScreenOrientation
    imageRotationBuffer = inputImg.crop(0, 0, 320, 240)
    if ScreenOrientation:
        imgRotationAim = image.new(size = (240, 320))
        rotationAngle = 90
        GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
    else:
        imgRotationAim = image.new(size = (320, 240))
        GETROTATION = imageRotationBuffer

    GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
    return GETROTATION

def lcdRotation(inputImg):
    global cameraSize,ScreenOrientation
    imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
    if ScreenOrientation:
        imgRotationAim = image.new(size = (240, 320))
        rotationAngle = 180
    else:
        imgRotationAim = image.new(size = (320, 240))
        rotationAngle = 90
    GETROTATION = imageRotationBuffer.rotate(+rotationAngle, adjust=1)
    GETROTATION = imgRotationAim.draw_image(GETROTATION,0,0,alpha=1)
    return GETROTATION
   
if cameraSize==True:
    camera.camera.config(size=(320,240))
else:
    camera.camera.config(size=(240,320))
    
image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")

class Edge:
    model = {
        "param": "/root/preset/model/sobel_int8.param",
        "bin": "/root/preset/model/sobel_int8.bin"
    }
    input_size = (224, 224, 3)
    output_size = (222, 222, 3)
    options = {
        "model_type":  "awnn",
        "inputs": {
            "input0": input_size
        },
        "outputs": {
            "output0": output_size
        },
        "mean": [127.5, 127.5, 127.5],
        "norm": [0.0078125, 0.0078125, 0.0078125],
    }
    def __init__(self):
        from maix import nn
        print("-- load model:", self.model)
        self.model = nn.load(self.model, opt=self.options)
        print("-- load ok")
    def __del__(self):
        del self.model

m = Edge()


canvas = image.new(size = (320, 240))
while True:
    canvas.clear()
    img_edgedetection = getLcdRotation(camera.capture())
    img_edgedetection = img_edgedetection.resize(224, 224, padding = 0)
    out = m.model.forward(img_edgedetection, quantize=True, layout="hwc")
    out = out.astype(np.float32).reshape(m.output_size)
    out = (np.ndarray.__abs__(out) * 255 / out.max()).astype(np.uint8)
    data = out.tobytes()
    edgeModel = img_edgedetection.load(data,(222, 222), mode="RGB")
    # canvas = edgeModel
    if ScreenOrientation:
        canvasVER = canvas.crop(0,0,240,320)
        canvasVER = canvasVER.rotate(-90, adjust=1)
        display.show(canvasVER)
    else:
        canvas.draw_image(edgeModel,48,8)
        canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
        display.show(canvas)
