Monica Houston
Published © Apache-2.0

Running Machine Learning on MaaXBoard's Yocto Image: Part 1

The ML Yocto image includes an eIQ layer so you can run machine learning inference efficiently on the MaaXBoard.

AdvancedProtip2 hours1,480

Things used in this project

Hardware components

MaaxBoard
Avnet MaaxBoard
×1
32GB SD card
×1

Software apps and online services

Yocto Project
Yocto Project

Story

Read more

Code

face_detect.py

Python
Usage:
python3 face_detect.py
import cv2
# Load our classifiers
haarcascades = "/usr/share/opencv4/haarcascades/"
face_classifier = cv2.CascadeClassifier(haarcascades + "haarcascade_frontalface_default.xml") 
l_eye_classifier = cv2.CascadeClassifier(haarcascades + "haarcascade_lefteye_2splits.xml")
r_eye_classifier = cv2.CascadeClassifier(haarcascades + "haarcascade_righteye_2splits.xml")
smile_classifier = cv2.CascadeClassifier(haarcascades + "haarcascade_smile.xml")

def detect(gray, frame):
    faces = face_classifier.detectMultiScale(gray, 1.1, 6)
    for (x, y, w, h) in faces:
        # Draw a rectangle around the face
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        # Detect the left eye and draw a circle around it 
        l_eye = l_eye_classifier.detectMultiScale(roi_gray, 1.1, 10)
        for (ex, ey, ew, eh) in l_eye:
            cv2.circle(roi_color, ((ex + ex+ew)//2,  (ey + ey+eh)//2), 10, (255, 255, 0), 1)
        # Detect the right eye and draw a cricle around it also
        r_eye = r_eye_classifier.detectMultiScale(roi_gray, 1.1, 10)
        for (ex, ey, ew, eh) in r_eye:
            cv2.circle(roi_color, ((ex + ex+ew)//2,  (ey + ey+eh)//2), 10, (255, 0, 255), 1)
            # Detect smile and draw rectangle around it
        smile = smile_classifier.detectMultiScale(roi_gray, 1.8, 20)
        for (sx, sy, sw, sh) in smile:
            cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (255, 0, 0), 2)
    return frame  
video_src = 1 # select 0 for MIPI camera or 1 for USB camera
video_capture = cv2.VideoCapture(video_src) 

# Grab the width and height of the input video
frame_width = int(video_capture.get(3))
frame_height = int(video_capture.get(4))

# Create the video writer for MP4 format
out = cv2.VideoWriter('face_detect.mp4',cv2.VideoWriter_fourcc(*'MP4V'), 20.0, (frame_width,frame_height))

while True: 
    _, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
    canvas = detect(gray, frame) 
    cv2.imshow('Video', canvas) # Comment this line if you'd prefer to only record a video
    out.write(frame)
    if cv2.waitKey(1) & 0xFF == ord('q'): # If we type on the keyboard:
        break # We stop the loop.
out.release()
video_capture.release() # We turn the webcam off.
cv2.destroyAllWindows() # We destroy all the windows inside which the images were displayed.

Credits

Monica Houston

Monica Houston

75 projects • 446 followers
I don't live on a boat anymore.

Comments