Android Face and Eye Detection using Python and OpenCV with Kivy #Python #kivy #kivymd #opencv

Posted by


In this tutorial, we will be learning how to use Python OpenCV on Android with Kivy for face and eye detection. OpenCV is a popular open-source computer vision and machine learning software library that is widely used for image processing and object detection. Kivy is a Python library for developing multi-touch applications.

To begin with, make sure you have Python and Kivy installed on your computer. You can install Kivy using pip:

pip install kivy

Next, we will need to install OpenCV on our Android device. One way to do this is by using the Kivy Launcher app, which allows you to run Kivy applications on Android devices. You can download the Kivy Launcher app from the Google Play Store.

Now, let’s start by creating a simple Kivy application that will use OpenCV for face and eye detection. Create a new Python file and add the following code:

from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
import cv2

class FaceDetectionApp(App):
    def build(self):
        self.img = Image(source='')
        Clock.schedule_interval(self.update, 1.0 / 30.0)
        return self.img

    def update(self, dt):
        cap = cv2.VideoCapture(0)
        ret, frame = cap.read()
        if ret:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
            eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

            faces = face_cascade.detectMultiScale(gray, 1.3, 5)
            for (x, y, w, h) in faces:
                cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
                roi_gray = gray[y:y+h, x:x+w]
                roi_color = frame[y:y+h, x:x+w]
                eyes = eye_cascade.detectMultiScale(roi_gray)
                for (ex, ey, ew, eh) in eyes:
                    cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)

            cv2.imwrite('frame.jpg', frame)
            self.img.source = 'frame.jpg'

        cap.release()

if __name__ == '__main__':
    FaceDetectionApp().run()

In this code, we have created a Kivy application called FaceDetectionApp that captures frames from the camera using the cv2.VideoCapture method. We use the cv2.CascadeClassifier method to load pre-trained Haar cascades for face and eye detection.

The update method is called every 1/30th of a second, where we read a frame from the camera, convert it to grayscale, and use the Haar cascades to detect faces and eyes in the frame. We draw rectangles around the detected faces and eyes and save the processed frame as an image file. Finally, we set the image source in the Kivy app to display the processed frame.

To run this application on your Android device, you can package it as an APK using Buildozer, a Python tool that automates the process of building Kivy applications for Android. You can find detailed instructions on how to use Buildozer on the Kivy documentation website.

In conclusion, this tutorial has shown you how to use Python OpenCV on Android with Kivy for face and eye detection. By combining the power of OpenCV for image processing and Kivy for developing touch-based applications, you can create interactive and visually appealing computer vision applications on the Android platform.

0 0 votes
Article Rating
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@abhishekkaintura6398
3 months ago

after running kivymd code on android app my front camera is in horizontal how can i correct please tell

@golposudhugolponoy
3 months ago

Wow sir… Just awesome

@12_sirusiye-dx3rr
3 months ago

This this is developed using opencv and kivy ?

@12_sirusiye-dx3rr
3 months ago

Sir source code please

@djjsjsh
3 months ago

Tutorial?

@mayurmore6686
3 months ago

Where is source code sir

@mahnamnasir1977
3 months ago

source code