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

from maix import image

from maix import nn
from maix.nn import decoder

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))

ScreenOrientation = False
image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")

model = {
    "param": "/root/preset/model/yolo2_20class_awnn.param",
    "bin": "/root/preset/model/yolo2_20class_awnn.bin"
}
options = {
    "model_type":  "awnn",
    "inputs": {
        "input0": (224, 224, 3)
    },
    "outputs": {
        "output0": (7, 7, (1+4+20)*5)
    },
    "mean": [127.5, 127.5, 127.5],
    "norm": [0.0078125, 0.0078125, 0.0078125],
}
labels = ['飞机', '自行车', '鸟', '船', '瓶子', '公共汽车', '汽车', '猫', '椅子', '牛', '餐桌', '狗', '马', '摩托车', '人', '盆栽', '羊', '沙发', '火车', '电视监视器']
#labels = ['plane', 'bicycle', 'bird', 'ship', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'table', 'dog', 'horse', 'motobike', 'human', 'Pot plant', 'sheep', 'sofa', 'train', 'tv']
anchors = [5.4, 5.38, 1.65, 2.09, 0.8, 1.83, 2.45, 4.14, 0.46, 0.8]
m = nn.load(model, opt=options)
yolo2_decoder = decoder.Yolo2(len(labels), anchors, net_in_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]), net_out_size=(7, 7))



canvasImg = image.new(size = (240, 320))
while True:
    canvasImg.clear()
    img_objectrecognition = getLcdRotation(camera.capture())
    img_objectrecognition = img_objectrecognition.crop(0, 0,224, 224)
    out = m.forward(img_objectrecognition.tobytes(), quantize=True, layout="hwc")
    boxes, probs = yolo2_decoder.run(out, nms=0.3, threshold=0.3, img_size=(options["inputs"]["input0"][0], options["inputs"]["input0"][1]))
    if len(boxes):
        for boxesi, box in enumerate(boxes):
            boxes[boxesi].append(probs[boxesi])
    if len(boxes):
        for i in (boxes):
            img_objectrecognition.draw_string(10,0, (str(str(labels[i[4][0]])) + str(round(i[4][1][i[4][0]]*100, 2))), scale = 1, color = (255,0,0) , thickness = 1)
            img_objectrecognition.draw_rectangle(i[0],i[1], int(i[0]+i[2]),int(i[1]+i[3]), color=(255,0,0), thickness=1)
    else:
        img_objectrecognition.draw_string(10,0, "No Result!", scale = 1, color = (255,0,0) , thickness = 1)
    if ScreenOrientation:
        img_objectrecognitionVER = img_objectrecognition.crop(0,0,240,320)
        img_objectrecognitionVER = img_objectrecognitionVER.rotate(-90, adjust=1)
        display.show(img_objectrecognitionVER)
    else:
        canvasImg.draw_image(img_objectrecognition,48,8)
        canvasImg.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
        display.show(canvasImg)
