# Personal Identification Using Embedded Raspberry Pi-Based Face Recognition Systems
## 2 Materials and Methods
### Face Recognition Application  
The face recognition application (Figure 10) is divided into four separate scripts written in Python, which can be categorized as follows:  
- Collecting data and sample images;  
- Creating the database;  
- Verifying faces;  
- The graphical user interface of the application. The application is mainly based on the \"_OpenCV_\" library [68], which, with its functions, enables real-time image processing obtained using the Raspberry Pi camera module. The following code allows loading of the necessary libraries used by the program. _from picamera.array import PiRGBArray_  
_from picamera import PiCamera_  
_import time_  
_import cv2_  
Figure 9: Raspberry Pi 3 B+ microcomputer (**a**); printed and mounted case for the Raspberry Pi 3 B+ computer, and cover for the Raspberry Pi Rev 2.1 camera module (**b**). The \"_picamera_\" library is used to operate the camera module. It allows us to initialize and set the appropriate camera mode using the \"_exposure_mode_\" command. There are many modes to choose from, but after conducting tests, the best image quality is achieved in the \"_sports_\" mode. Additionally, the library allows setting the resolution, brightness, and frame rate, as shown in the code below. _camera = PiCamera()_  
_camera.framerate = 24_  
_camera.brightness = 50_  
_camera.iso = 800_  
_camera.exposure_mode ='sports'_  
_camera.exposure_compensation = 10_  
_camera.resolution = (640, 480)_  
_rawCapture = PiRGBArray(camera, size = (640, 480))_  
The first script for collecting sample images needs to have the image provided in grayscale. For this purpose, the command \"_cv2.COLOR_BGR2 GRAY_\" from the \"_OpenCV_\" library is used. _skala_szrosci = cv2.cvtColor(zdjecie, cv2.COLOR_BGR2 GRAY)_  
In the application, a Haar classifier is used by default, based on frontal face detection. It is loaded using the \"_cv2.CascadeClassifier_\" command. _detektor_fuzzy = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')_  
Faces of different sizes are detected using a detector, with a command running in a \"_for_\" loop that stops only after detecting 100 sample face images of a single user or by pressing the \"_k_\" key on the keyboard. _twarz = detektor_twarzy.detectMultiScale(skala_szrosci, 1.3, 5)_  
Next, a rectangle is created that encompasses only the face area, which is then cropped from the entire image captured by the camera module. The \"_cv.rectangle_\" command from the \"_OpenCV_\" library is used for this purpose. _cv2.rectangle(zdjecie, (x, y), (x + w, y + h), (255, 0, 0), 2)_  
The rectangular area is cropped from the entire image and then saved along with the user's number and name using the \"_cv.imwrite_\" command in the sample image database. _cv2.imwrite(\"baza zdjec/Uzytkownik.\" + str(nr_uzytkownika) + '.' + str)_  
The live preview from the camera and the images of faces collected by the program are enabled by the \"_cv.imshow_\" command from the \"_OpenCV_\" library. _cv2.imshow(\"Podglad rozpoznania', zdjecie)_  
Figure 10: The start screen of the facial recognition application (**a**); the window with the camera transmission preview and the face detection window that collects reference photos (**b**). Blue block—Creating a database of reference photos, orange block—Creating a data matrix, green block—Face recognition, magenta block—Close program. In the second script, a local binary pattern histogram is initially created, as shown below, in which a data matrix containing the mathematical description of the biometric features of the sample face images is later created (histogram training). _histogram = cv2_face_create_LBPHFaceRecognizer()_  
_histogram.train(turze_, _np.array(nrID))_  
Similar to the histogram, an empty image file is initially created. However, unlike the data matrix, the image is overwritten with the sample image in a _\"for\"_ loop until it is overwritten by all face images from the database. After each overwrite, a data matrix describing the given image is created.