Search code examples
pythonopencvarduinopyserial

How to send whether a face is detected via open cv to serial?


I am trying to make a system where a python program sends a number (1 or 0) based on whether a face is detected or not using open cv. A arduino is then to recieve a signal and turn on or off a relay to turn on or off a light. I have done the part where the face is detected and the arduino but i want to know how i can make the python code send a 0 or 1. Here is the code:

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')

while True:
    # Read the frame
    _, img = cap.read()
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
    # Display
    cv2.imshow('img', img)
    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k==27:
        break
# Release the VideoCapture object
cap.release()

I tried some things but they all failed miserably. Any help is appreciated.


Solution

  • The cv::CascadeClassifier detectMultiScale: method returns all the faces detected by OpenCV, so if there are no faces, the result faces will be empty.

    You can test it, and do what you want accordingly :

    # ...
        # Detect the faces
        faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    
        if len(face) == 0:
            # No face : turn on the light !
        else:
            # Something detected, lets do something...
    
    

    For how to write from Python to the Arduino, you can use pyserial :

    import time
    import serial
    
    ser = serial.Serial('/dev/serial0', 115200, timeout=0.050)
    
    # ...
    
    if len(face) == 0:
        # No face
        ser.open()
        ser.write('0')
        time.sleep(1)
        ser.close()
    else:
        # A least one face detected
        ser.open()
        ser.write('1')
        time.sleep(1)
        ser.close()
    

    As you see, the if and else part can be factorized, etc. :-)