Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
class CustomFaceDetectionComponent(FaceDetectionComponent):
    def __init__(self, *args, **kwargs):
        super(CustomFaceDetectionComponent, self).__init__(*args, **kwargs)
        self.scaleFactor = 1.2
        self.minNeighbors = 3

    def detect(self, image):
        # Override the detect function with custom behavior
        img = array(image).astype(np.uint8)

        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

        faces = self.faceCascade.detectMultiScale(
            gray,
            scaleFactor=self.scaleFactor,  # Example of different scale factornow these variables belong to the class
            minNeighbors=self.minNeighbors,    # Example of different minNeighbors they can be accessed and changed outside this function
            minSize=(int(self.params.minW), int(self.params.minH)),
        )

        faces = [BoundingBox(x, y, w, h) for (x, y, w, h) in faces]

        return BoundingBoxesMessage(faces)

...

The work on the new custom component is done. Now, you must change the scripts that you want to use the new component. Here the demo_desktop_camera_facedetection.py is used as an example.

First, make sure your the script is able to find your new component. Here is one way of doing so:

Code Block
import sys
from pathlib import Path
# Get the absolute path to the custom_servicescomponents folder
custom_servicescomponents_path = Path(__file__).resolve().parent.parent.parent / "custom_components"
sys.path.append(str(custom_servicescomponents_path))

Alternatively, you can append the ‘custom_components' folder path to the PYTHONPATH environment variable within the ‘activate’ script of your virtual environment. For example:

...