Intel OpenVINO on Raspberry Pi: 2018 harvest

Published on December 26, 2018

Intel OpenVINO on Raspberry Pi: 2018 harvest


    You can write about programming not only with prose, but also with verses. The latter, of course, does not happen often - say, on an Intel blog, this happened a little less than never. However, as an experiment today, we decided to allow ourselves; how it happened is up to you. So...


    Last quarter of the year, to
    be precise, last week,
    Intel developers presented a new
    OpenVINO toolkit release on software .


    What's new is done - refer to changelog'am,
    I can’t be given exact details,
    I’ll tell you a little
    about this post: About vision, Intel and raspberry wine.


    ***


    OpenVINO is essentially a set of tools,
    libraries and examples of solutions.
    With a special acceleration on Intel's hardware,
    popular computer vision problems.


    Who has not heard about computer vision -
    This is a field of computer science,
    Forcing to understand the machine,
    Like man, the world around.


    The source of information is mainly images,
    and the problem is to create an algorithm
    for extracting useful signs
    and making decisions on them.


    ***


    The first in our mini-review is the OpenCV library,
    How often it is called, the traditional
    Machine Vision and Learning,
    Reading from the camera and on the drawing screen,
    Cross-platform and living on GitHub,
    Got 30502 stars at the time of writing.


    In the package for Raspberry, we will also find
    OpenCV Public, and with it
    NEON optimization, wrappers in Python,
    GStreamer for cameras and for GTK windows.



    ***


    Already seven years in the world.
    To solve our problems.
    Deep neural networks are used.
    Those that need to be trained with a teacher.


    Caffe, PyTorch - it's all about training,
    sometimes taking a couple of weeks.
    OpenVINO solves the second problem:
    Run the trained grids as quickly as possible.


    From the best solutions for many platforms,
    For known scenarios, needs and resources,
    In OpenVINO deep learning - a separate engine,
    With one interface on different devices.


    ***


    To work with your toolkit.
    Install Raspbian 9 on Raspberry Pi,
    Connect Movidius stick, power supply,
    Check that the CPU version is at least seven.


    Read more in the corresponding guide ,
    And as a demo, I propose a code that
    works with the camera and turns off the
    LED in the direction of view.


    Download two networks: for persons finding
    and predicting the position of the head ,
    Use a USB camera and note,
    What is in the pin code is number 2 by default.


    ***


    Dedicated to all those who are enthusiastic.
    We have been working on our project all year.
    Let everything accelerate in the upcoming,
    OpenVINO on Raspberry - the same IoT!


    import cv2 as cv
    from gpiozero import LED
    from math import cos, sin, pi
    winName = 'OpenVINO on Raspberry Pi'
    cv.namedWindow(winName, cv.WINDOW_NORMAL)
    faceDetectionNet = cv.dnn.readNet('face-detection-retail-0004.xml',
                                      'face-detection-retail-0004.bin')
    headPoseNet = cv.dnn.readNet('head-pose-estimation-adas-0001.xml',
                                 'head-pose-estimation-adas-0001.bin')
    faceDetectionNet.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
    headPoseNet.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)
    cap = cv.VideoCapture(0)
    led = LED(2)
    led.on()
    while cv.waitKey(1) != 27:
        hasFrame, frame = cap.read()
        if not hasFrame:
            break
        frameHeight, frameWidth = frame.shape[0], frame.shape[1]
        # Detect faces on the image.
        blob = cv.dnn.blobFromImage(frame, size=(300, 300), ddepth=cv.CV_8U)
        faceDetectionNet.setInput(blob)
        detections = faceDetectionNet.forward()
        for detection in detections.reshape(-1, 7):
            confidence = float(detection[2])
            if confidence > 0.5:
                xmin = int(detection[3] * frameWidth)
                ymin = int(detection[4] * frameHeight)
                xmax = int(detection[5] * frameWidth)
                ymax = int(detection[6] * frameHeight)
                xmax = max(1, min(xmax, frameWidth - 1))
                ymax = max(1, min(ymax, frameHeight - 1))
                xmin = max(0, min(xmin, xmax - 1))
                ymin = max(0, min(ymin, ymax - 1))
                # Run head pose estimation network.
                face = frame[ymin:ymax+1, xmin:xmax+1]
                blob = cv.dnn.blobFromImage(face, size=(60, 60), ddepth=cv.CV_8U)
                headPoseNet.setInput(blob)
                headPose = headPoseNet.forward(['angle_p_fc', 'angle_r_fc', 'angle_y_fc'])
                p, r, y = headPose[0][0], headPose[1][0], headPose[2][0]
                cos_r = cos(r * pi / 180)
                sin_r = sin(r * pi / 180)
                sin_y = sin(y * pi / 180)
                cos_y = cos(y * pi / 180)
                sin_p = sin(p * pi / 180)
                cos_p = cos(p * pi / 180)
                x = int((xmin + xmax) / 2)
                y = int((ymin + ymax) / 2)
                # center to right
                cv.line(frame, (x,y), (x+int(50*(cos_r*cos_y+sin_y*sin_p*sin_r)), y+int(50*cos_p*sin_r)), (0, 0, 255), thickness=3)
                # center to top
                cv.line(frame, (x, y), (x+int(50*(cos_r*sin_y*sin_p+cos_y*sin_r)), y-int(50*cos_p*cos_r)), (0, 255, 0), thickness=3)
                # center to forward
                cv.line(frame, (x, y), (x + int(50*sin_y*cos_p), y + int(50*sin_p)), (255, 0, 0), thickness=3)
                cv.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 255))
                if abs(cos_y * cos_p) > 0.9:
                    cv.putText(frame, 'FORWARD', (0, 30), cv.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), thickness=2)
                    led.off()
                else:
                    led.on()
        cv.imshow(winName, frame)