A course in image processing at the school eventually requires submitting an image processing system.
Just before I knew something about opencv, let's write a simple face recognition applet
The effect diagram is as follows
Author IDE uses Pycharm, GUI programming uses built-in tkinter directly
Environmental Science:
python3.6
opencv4.1
First import each library you need to use
#-*- coding: utf-8 -*- import sys import importlib import cv2 import tkinter as tk import tkinter.messagebox from tkinter import filedialog
Then we need to make a path selection function because we can't change the address in the code manually every time we recognize it.
And this function we'll bind to a button later for easy use
def selectPath(): global path_ path_ = filedialog.askopenfilename() path.set(path_) path = tk.StringVar()
Key Face Recognition Functions
Download address of training parameter data used: https://github.com/opencv/opencv/tree/master/data/haarcascades
And the xml file needs to be placed in the project directory
def imgface(): try: # github Obtaining trained face parameter data face_cascade = cv2.CascadeClassifier(r'./haarcascade_frontalface_default.xml') # Read pictures image = cv2.imread(path_) #Convert to Grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Detecting faces in pictures faces = face_cascade.detectMultiScale( gray, scaleFactor=1.15, minNeighbors=5, minSize=(5, 5), flags=cv2.IMREAD_GRAYSCALE) if(len(faces)==0): tkinter.messagebox.showerror('error', 'No face recognized,Please choose a clearer picture') print("find {0} faces!".format(len(faces))) # faces The four quantities are the x-coordinate, the p-coordinate, the width, and the length of the upper left corner. for (x, y, w, h) in faces: cv2.rectangle(image,(x,y),(x+w,y+w),(255,245,0),1) if (len(faces) > 0): cv2.imshow("find {0} faces!".format(len(faces)), image) cv2.waitKey(0) except: tkinter.messagebox.showerror('error', 'Please select the correct picture file!')
Then do GUI programming:
importlib.reload(sys) window = tk.Tk() window.title('Face Recognition Applet') fm1 = tk.Frame(window) fm2 = tk.Frame(window) fm3 = tk.Frame(window) def selectPath(): global path_ path_ = filedialog.askopenfilename() path.set(path_) path = tk.StringVar() Ltop=tk.Label(fm1,text="Please select a picture path") B1=tk.Button(fm2, text = "path choice", command = selectPath) E1=tk.Entry(fm2, textvariable = path,bd=5) B2=tk.Button(fm2, text = "Determine", command =imgface) Lbot=tk.Label(fm3,text="School number: 1622107031 xx Full name: istw") Ltop.pack(side = tk.TOP) B1.pack(side=tk.LEFT) E1.pack(side = tk.LEFT) B2.pack(side=tk.LEFT) Lbot.pack(side = tk.BOTTOM) fm1.pack(side=tk.TOP) fm2.pack(side=tk.TOP) fm3.pack(side=tk.TOP) sw = window.winfo_screenwidth() #Get the screen width sh = window.winfo_screenheight() #Get screen height ww = 300 wh = 100 #Window width and height 100 x = (sw-ww) / 2 y = (sh-wh) / 3 window.geometry("%dx%d+%d+%d" %(ww,wh,x,y)) window.mainloop()
Well, a simple interactive program is written, but because the face training data used is officially provided many years ago, the recognition accuracy is not too accurate
There is also a question about how.Py files can be displayed to others, because not everyone has a py environment on their computer and the versions are incompatible, so we sometimes use some methods to convert them to exe files for easy display, which will be discussed later.