English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Recently, I led my students to design a URTP project and need to do face recognition. Since OpenCV has reached version 2.X, I don't want to use the original 1.X version code, and the code on the Internet is all 1.X version code. I tried to write a section of 2.X version code, reviewed the information repeatedly, and finally tested it successfully today (it is obvious that the 2.X version code is simpler than the 1.X code), for everyone's reference, the code is as follows: (made a simple modification in python3.6.1 on May 12, 2017)
import cv2 import numpy as np cv2.namedWindow("test")#Name a window cap=cv2.VideoCapture(1)#Open camera 1 success, frame = cap.read()#Read a frame image, the previous return value is whether it is successful, and the next return value is the image itself color = (0,0,0)#Set the color of the face box classfier=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")#Define the classifier while success: success, frame = cap.read() size=frame.shape[:2]#Get the size of the current frame color image image=np.zeros(size,dtype=np.float16)#Define a grayscale image matrix with the same size as the current frame image image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)#Convert the current frame image to a grayscale image (with modification here) cv2.equalizeHist(image, image)#Eguagliamento della distribuzione di histogramma dell'immagine in scala di grigio #Le seguenti tre righe stabiliscono la dimensione minima dell'immagine divisor=8 h, w = size minSize=(int(w/divisor), int(h/divisor))#Aggiunto una funzione di intero faceRects = classfier.detectMultiScale(image, 1.2, 2, cv2.CASCADE_SCALE_IMAGE,minSize)#Rilevamento facciale if len(faceRects)>0:#Se la lunghezza dell'array faceRects è maggiore di 0 for faceRect in faceRects: #Disegna un rettangolo intorno a ogni faccia x, y, w, h = faceRect cv2.rectangle(frame, (x, y), (x+w, y+h), color) cv2.imshow("test", frame)#Visualizza l'immagine key=cv2.waitKey(10) c = chr(key & 255) if c in ['q', 'Q', chr(27)]: break cv2.destroyWindow("test")
Immagine di esempio:
Questo è tutto il contenuto dell'articolo, spero che sia utile per il tuo studio e ti auguro di supportare e applaudire il tutorial.
Dichiarazione: il contenuto di questo articolo è stato tratto da Internet, il copyright spetta ai rispettivi proprietari. Il contenuto è stato contribuito e caricato autonomamente dagli utenti di Internet, il sito web non detiene il diritto di proprietà, non è stato elaborato manualmente e non assume alcuna responsabilità legale connessa. Se trovi contenuti sospetti di violazione del copyright, sei invitato a inviare una email a: notice#oldtoolbag.com (al momento dell'invio dell'email, sostituisci # con @) per segnalare il problema e fornire prove pertinenti. Una volta verificata la veridicità, il sito web rimuoverà immediatamente i contenuti sospetti di violazione del copyright.