Big data, artificial intelligence, distribution, cloud computing, cloud security based on cloud, Internet of everything is really the future direction! So-called web development, the back-end server is just a start, playing with the python back-end also experience in spring buckets of study (ing) as long as do a decent “session” ten thousand people basically will not but just rely on the “delete” can be achieved, will involve the concurrent, parallel, but want to do and involves distributed, Micro architecture and so on. There are also more details about the service, such as how to analyze users’ preferences in real time to push corresponding content. These are closely related to big data, closely related to distributed computing. Of course, this is where I realized Python wasn’t a good fit, regardless of performance, and this alone shows that Python’s solution isn’t as good as Java’s in this respect, although it has its options. The good news is that Python is still attractive because of its rich library and ease of use in the areas of machine learning and artificial intelligence, regardless of the underlying level. That’s not to say I’m going to stop playing. It’s not possible. I still have a Web project to finish (White Hole) and Springboot to refactor.
The environment
1) python 3.7 (2) opencv
pip3 install opencv-python
pip3 install opencv-contrib-python
Copy the code
This is still a simple interface call.
Function interface used
Ironclad API, flowing programmer ah!
import cv2.cv2 as cv
cv.imread() # fetch image
cv.cvtColot() # Image color processing (grayscale processing)
cv.imwrite() # Save images
cv.imshow() # Show pictures
cv.waitKey() Hold the incoming time in milliseconds and return the asCLL value of the key that the keyboard pressed
cv.resize() # Resize the image
cv.rectangle() # draw rectangle
cv.circle() # draw circle
cv.CascadeClassifier()# Use training models
cv.VideoCapture() Read video 0 indicates that the camera is read
face.LBPHFaceRecognizer_create() # Data training
cv.destoryAllWindows() # free memory
Copy the code
process
The words here are divided into two main steps
1 read the picture/video in the face information, face recognition position (box face position) 2. Compare the recognized faces (feature extraction)
Face detection part
Picture reading/size modification
import cv2.cv2 as cv
Image = cv.imread("Image/face1.jpg")
cv.imshow("Image",Image)
Image_resize = cv.resize(Image,dsize=(200.200))
cv.imshow("ResizeImage",Image_resize)
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
Image rectangle labeling
import cv2.cv2 as cv
img = cv.imread('Image/face1.jpg')
x,y,w,h = 100.100.100.100 # starting coordinates
# draw rectangle
cv.rectangle(img,(x,y,x+w,y+h),color=(0.0.255),thickness=1) # Box color, thickness
# draw circle
cv.circle(img,center=(x+w,y+h),radius=100,color=(255.0.0),thickness=5)
# show
cv.imshow('re_img',img)
cv.waitKey(0)
cv.destroyAllWindows()
Copy the code
Face detection
# Import CV module
import cv2.cv2 as cv
import cv2.data as data
# fetch image
img = cv.imread('Image/face1.jpg')
gary = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier(data.haarcascades+"haarcascade_frontalface_alt2.xml")
face = face_detect.detectMultiScale(gary)
for x,y,w,h in face:
cv.rectangle(img,(x,y),(x+w,y+h),color=(0.0.255),thickness=2)
cv.imshow('result',img)
cv.waitKey(0)
# free memory
cv.destroyAllWindows()
Copy the code
Function description
Gary = CV. CvtColor (img, CV.COLOR_BGR2GRAY) Binary processing image
face_detect = cv.CascadeClassifier(data.haarcascades+”haarcascade_frontalface_alt2.xml”) This is the loading of the head extraction model trained by somebody else to help us extract the face of the picture, in addition to the human eye extraction, and so on.
Face = face_detectMultiScale (Gary) Extracts the size and position of the face according to the model and returns a tuple
Focus on
face = face_detect.detectMultiScale(gary,1.01.5.0, (100.100), (300.300(You can also use it this way.1.01Represents the zoom ratio of an image5Times of comparison, which means five times and still the same, it's a face0Ignore the default parameters and add (100.100) (300.300) indicates the range of the face, which can be set. These parameters can be set without setting, and directly use the defaultCopy the code
face_detect = cv.CascadeClassifier(data.haarcascades+"haarcascade_frontalface_alt2.xml"The model loaded here is actually found in site-Pakges in the virtual environmentCopy the code
In addition, if you have OpencV installed, you can also find it in the OpencV installation directory, and then you can use whichever one you want. For example, I installed it on drive D
face_detect = cv.CascadeClassifier('D:/opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml')
Copy the code
I’ll just fill in the path and it’s recommended to use the default.
face_detect =
cv.CascadeClassifier(data.haarcascades+"haarcascade_frontalface_default.x")
Copy the code
Read video annotation face
cap = cv.VideoCapture(0) Read the cameraCopy the code
# Import CV module
import cv2.cv2 as cv
import cv2.data as data
import time
# detection function
def face_detect_demo(img) :
gary = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier(data.haarcascades+"haarcascade_frontalface_default.xml")
face = face_detect.detectMultiScale(gary)
for x,y,w,h in face:
cv.rectangle(img,(x,y),(x+w,y+h),color=(0.0.255),thickness=2)
cv.imshow('result',img)
cap = cv.VideoCapture(0)
while True:
flag, img = cap.read()
if not flag:
break
face_detect_demo(img)
if ord('q') == cv.waitKey(1) :break
cap.release() Free video head memory
# free memory
cv.destroyAllWindows()
Copy the code
At present, the video information of the camera is read. If the path is directly filled in, the content of the video will be read for face annotation
Face recognition
So far have been able to put a human face from the video or pictures to dig down (in fact, when the current step has been achieved the other day one of the more fervent AI plugin, but currently used recognition model is not great, if you want to use the specialized training, and then combined with peripheral drive, we return to obtain the coordinates of head, Even more game-like, we had to calculate the recoil offset of the gun and then drive the cursor to aim and shoot. In fact, the principle of the plug-in is similar, but the coordinates of the family is directly caught in memory, not our face recognition)
Face information input
Engage in face recognition that is bound to input face information, here by two schemes, one is directly ready for the picture (is the need to input face photos) or through the camera input, because we identify when to take the camera so it is suggested to start input from the camera. For the first solution, just prepare the photo. For the second solution, just look at the following code:
import cv2.cv2 as cv2
import os
import threading
name = None
save_flag = False
lock = threading.Lock()
def input_name() :
global name,save_flag
while 1:
if name =="Q" or name=="q":
return
else:
name=input("\n:")
save_flag = True
def Get_Face() :
global name,save_flag
cap = cv2.VideoCapture(0)
num = 1
Path_save = r"Image/InPutImg/"
if not os.path.exists(Path_save):
os.makedirs(Path_save)
while(1):
ret_flag,VImg = cap.read()
cv2.imshow("Capture_Test",VImg)
cv2.waitKey(1)
lock.acquire()
if name:
if name == "Q" or name == "q":
lock.release()
return
if save_flag:
cv2.imwrite(Path_save+str(num)+"."+name+".jpg",VImg)
print("\n Picture saved:"+str(num)+"."+name+".jpg")
num += 1
save_flag = False
lock.release()
cap.release()
cv2.destroyAllWindows()
if __name__=="__main__":
t1 = threading.Thread(target=Get_Face)
t2 = threading.Thread(target=input_name)
t1.start()
t2.start()
Copy the code
An extra thread has been opened to control input, but it is not able to synchronize the input, so it will block a file in the project folder
It contains the face of the camera.
The training data
This repeats the previous steps to extract the face from the picture, and then train the id of the corresponding face and character
import os
import cv2.cv2 as cv
import numpy as np
import cv2.data as data
def getImageIds(path) :
The # function extracts the face and returns the face and id of the character
faceseare=[] # Save the detected face
ids=[] #
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
# Face detection
face_detector = cv.CascadeClassifier(data.haarcascades+"haarcascade_frontalface_default.xml")
if imagePaths:
print('Training picture is:',imagePaths)
else:
print("Please enter your face first.")
return
for imagePath in imagePaths:
# Binarization
img = cv.imread(imagePath)
img=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
img_numpy=np.array(img,'uint8')Get the image matrix
faces = face_detector.detectMultiScale(img_numpy)
id = int(os.path.split(imagePath)[1].split('. ') [0])
for x,y,w,h in faces:
ids.append(id)
faceseare.append(img_numpy[y:y+h,x:x+w])
print('Obtained id:'.id)
return faceseare,ids
if __name__ == '__main__':
# Image path
path='Image/InPutImg'
Get image array and ID tag array and name
faces,ids=getImageIds(path)
Get the training object
recognizer=cv.face.LBPHFaceRecognizer_create()
recognizer.train(faces,np.array(ids)) # The corresponding face and ID associated training
# Save training files
model_save = "trainer/"
if not os.path.exists(model_save):
os.makedirs(model_save)
recognizer.write('trainer/trainer.yml')
Copy the code
Face recognition
This is the last step, and it’s really about identifying by rating
import cv2.cv2 as cv
import os
import cv2.data as data
recogizer=cv.face.LBPHFaceRecognizer_create()Load the training dataset file
recogizer.read('trainer/trainer.yml')
names=[]
warningtime = 0
# Pictures ready for recognition
def face_detect_demo(img) :
global warningtime
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
face_detector=cv.CascadeClassifier(data.haarcascades+"haarcascade_frontalface_default.xm")
face=face_detector.detectMultiScale(gray)
for x,y,w,h in face:
cv.rectangle(img,(x,y),(x+w,y+h),color=(0.0.255),thickness=2)
cv.circle(img,center=(x+w//2,y+h//2),radius=w//2,color=(0.255.0),thickness=1)
# Face recognition
ids, confidence = recogizer.predict(gray[y:y + h, x:x + w])
if confidence > 80: # The higher the score, the lower the credibility
warningtime += 1
if warningtime > 100:
warningtime = 0
print("This person has not been identified.") In fact, a corresponding punishment mechanism can be developed for this piece
cv.putText(img, 'unkonw', (x + 10, y - 10), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0.255.0), 1)
else:
# Here also corresponds to a set of post-recognition mechanisms
cv.putText(img,str(names[ids-1]), (x + 10, y - 10), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0.255.0), 1)
cv.imshow('result',img)
def get_name(names) :
path = 'Image/InPutImg/'
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
for imagePath in imagePaths:
name = str(os.path.split(imagePath)[1].split('. '.2) [1])
names.append(name)
cap=cv.VideoCapture(0)
get_name(names)
while True:
flag,frame=cap.read()
if not flag:
break
face_detect_demo(frame)
if ord(' ') == cv.waitKey(10) :# Press space to close
break
cap.release()
cv.destroyAllWindows()
Copy the code
Here’s a demo. 1. Entered pictures
1.lena.jpg
Effect:Here is the most basic identification is done, so about the punishment mechanism or through the identification of the mechanism, in fact, if you can combine raspberry PI, you can do a dormitory dedicated access control system.