AI智能识别人脸情绪项目

1.本项目需要用到CocoBlocky进行编辑,使用CocoMod电子模块(显示屏,摄像头等)。

2.支持识别4种情绪:快乐(happy),伤心(sad),愤怒(angry),平静(peace)。
CocoBlocky
源码:

from maix import camera
from maix import display

from maix import image

from maix import nn
from maix.nn import decoder

camera.camera.config(size=(240,320))

ScreenOrientation = False
image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")

def lcdRotation(inputImg,rotationAngle):
    from maix import image
    imageRotationBuffer = inputImg.crop(0, 0, 240, 320)
    if ScreenOrientation:
        imgRotationAim = image.new(size = (240, 320))
    else:
        imgRotationAim = image.new(size = (320, 240))
    return imgRotationAim.draw_image(imageRotationBuffer.rotate(rotationAngle, adjust=1),0,0,alpha=1)

if ScreenOrientation:
    CAMERAROTATE = +180
else:
    CAMERAROTATE = +90



class Yolo:
    labels = ['sad', 'angry', 'peace', 'happy']
    anchors = [1.19, 1.98, 2.79, 4.59, 4.53, 8.92, 8.06, 5.29, 10.32, 10.65]
    m = {
        "param": "/root/user/model/emotion-20260204142932.param",
        "bin": "/root/user/model/emotion-20260204142932.bin"
    }
    options = {
        "model_type":  "awnn",
        "inputs": {
            "input0": (224, 224, 3)
        },
        "outputs": {
            "output0": (7, 7, (1+4+len(labels))*5)
        },
        "mean": [127.5, 127.5, 127.5],
        "norm": [0.0078125, 0.0078125, 0.0078125],
    }
    def __init__(self):
        from maix import nn
        from maix.nn import decoder
        self.model = nn.load(self.m, opt=self.options)
        self.decoder = decoder.Yolo2(len(self.labels), self.anchors, net_in_size=(224, 224), net_out_size=(7, 7))
    def __del__(self):
        del self.model
        del self.decoder
Yolo = Yolo()
while True:
    img_detection = lcdRotation(camera.capture(),CAMERAROTATE)
    img_detection = img_detection.crop(0, 0,224, 224)
    out = Yolo.model.forward(img_detection, quantize=True, layout="hwc")
    boxes, probs = Yolo.decoder.run(out, nms=0.3, threshold=0.5, img_size=(224, 224))
    if len(boxes):
        for boxesi, box in enumerate(boxes):
            boxes[boxesi].append(probs[boxesi])
    if len(boxes):
        for i in (boxes):
            img_detection.draw_string((i[0]),(i[1]), (''.join([str(x) for x in [Yolo.labels[i[4][0]], ";", str(round(((i[4][1][i[4][0]]) * 100), 2)) + str("%")]])), scale = 1, color = (255,0,0) , thickness = 1)
            img_detection.draw_rectangle((i[0]),(i[1]), (i[0]+i[2]),(i[1]+i[3]), color=(255,0,0), thickness=1)
    if ScreenOrientation:
        img_detectionVER = img_detection.crop(0,0,240,320)
        img_detectionVER = img_detectionVER.rotate(-90, adjust=1)
        display.show(img_detectionVER)
    else:
        display.show(img_detection)
posted @ 2026-02-05 16:17  Tobaa  阅读(20)  评论(0)    收藏  举报