Build a Face Detection App with Python OOP — From Zero to Pro(part-5)

Part 5: View Rendering & Main Application (FaceApp)

Overview

This part covers:

The View class (responsible only for drawing)

The FaceApp class (main loop + webcam + detection pipeline)

🟡 View Class — Everythi…


This content originally appeared on DEV Community and was authored by MohammadReza Mahdian

Part 5: View Rendering & Main Application (FaceApp)

Overview

This part covers:

  • The View class (responsible only for drawing)
  • The FaceApp class (main loop + webcam + detection pipeline)

🟡 View Class — Everything About Rendering

Responsibilities

  • Draw face ellipse
  • Draw eye and lip landmarks
  • Apply overlays
  • Show result using cv2.imshow

View Implementation (Your Logic — Cleaned, Structured)

class View:
    def draw_faces(self, img, faces):
        for (x, y, w, h) in faces:
            cx, cy = x + w//2, y + h//2
            cv2.ellipse(img, (cx, cy), (w//2, h//2), 0, 0, 360, (255, 255, 0), 2)
        return img

    def draw_features(self, img, features):
        if not features:
            return img

        for key in features:
            pts = features[key]
            for (x, y) in pts:
                px = int(x * img.shape[1])
                py = int(y * img.shape[0])
                cv2.circle(img, (px, py), 1, (0, 255, 0), -1)

        return img

    def show(self, img):
        cv2.imshow("FaceApp", img)

⚫ FaceApp — The Heart of the System

Responsibilities

  • Load YOLO model (via ConfigModel)
  • Initialize detectors
  • Open webcam (VideoCapture(0))
  • Run real‑time loop
  • Send frames to detectors
  • Render through View
  • Handle quitting (press q)
  • Cleanup webcam + windows

Full FaceApp Implementation

class FaceApp:
    def __init__(self):
        import cv2
        from app.config_model import ConfigModel
        from app.face_detector import FaceDetector
        from app.face_features_detector import FaceFeaturesDetector
        from app.view import View

        self.cv2 = cv2
        self.view = View()

        cfg = ConfigModel()
        self.model = cfg.load_model("yolo-face", "model/yolov9t-face-lindevs.pt")

        self.face_detector = FaceDetector(self.model)
        self.features_detector = FaceFeaturesDetector()

        self.cap = cv2.VideoCapture(0)

    def run(self):
        while True:
            ret, frame = self.cap.read()
            if not ret:
                break

            faces = self.face_detector.detect(frame)
            features = self.features_detector.extract(frame)

            frame = self.view.draw_faces(frame, faces)
            frame = self.view.draw_features(frame, features)
            self.view.show(frame)

            if self.cv2.waitKey(1) & 0xFF == ord('q'):
                break

        self.cap.release()
        self.cv2.destroyAllWindows()

Main Program

if __name__ == "__main__":
    app = FaceApp()
    app.run()

requirements.txt

-f https://download.pytorch.org/whl/cpu/torch_stable.html
torch==2.2.0+cpu
torchvision==0.17.0+cpu
torchaudio==2.2.0+cpu
opencv-python>=4.7.0
numpy>=1.26.0
ultralytics>=8.0.167
mediapipe>=0.10.21


This content originally appeared on DEV Community and was authored by MohammadReza Mahdian


Print Share Comment Cite Upload Translate Updates
APA

MohammadReza Mahdian | Sciencx (2025-11-20T22:58:25+00:00) Build a Face Detection App with Python OOP — From Zero to Pro(part-5). Retrieved from https://www.scien.cx/2025/11/20/build-a-face-detection-app-with-python-oop-from-zero-to-propart-5/

MLA
" » Build a Face Detection App with Python OOP — From Zero to Pro(part-5)." MohammadReza Mahdian | Sciencx - Thursday November 20, 2025, https://www.scien.cx/2025/11/20/build-a-face-detection-app-with-python-oop-from-zero-to-propart-5/
HARVARD
MohammadReza Mahdian | Sciencx Thursday November 20, 2025 » Build a Face Detection App with Python OOP — From Zero to Pro(part-5)., viewed ,<https://www.scien.cx/2025/11/20/build-a-face-detection-app-with-python-oop-from-zero-to-propart-5/>
VANCOUVER
MohammadReza Mahdian | Sciencx - » Build a Face Detection App with Python OOP — From Zero to Pro(part-5). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/20/build-a-face-detection-app-with-python-oop-from-zero-to-propart-5/
CHICAGO
" » Build a Face Detection App with Python OOP — From Zero to Pro(part-5)." MohammadReza Mahdian | Sciencx - Accessed . https://www.scien.cx/2025/11/20/build-a-face-detection-app-with-python-oop-from-zero-to-propart-5/
IEEE
" » Build a Face Detection App with Python OOP — From Zero to Pro(part-5)." MohammadReza Mahdian | Sciencx [Online]. Available: https://www.scien.cx/2025/11/20/build-a-face-detection-app-with-python-oop-from-zero-to-propart-5/. [Accessed: ]
rf:citation
» Build a Face Detection App with Python OOP — From Zero to Pro(part-5) | MohammadReza Mahdian | Sciencx | https://www.scien.cx/2025/11/20/build-a-face-detection-app-with-python-oop-from-zero-to-propart-5/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.