Cov txheej txheem:

Kev Paub Lub Ntsej Muag Lub Sij Hawm Tiag: Ib Qhov Kawg-rau-Kawg: 8 Kauj Ruam (nrog Duab)
Kev Paub Lub Ntsej Muag Lub Sij Hawm Tiag: Ib Qhov Kawg-rau-Kawg: 8 Kauj Ruam (nrog Duab)

Video: Kev Paub Lub Ntsej Muag Lub Sij Hawm Tiag: Ib Qhov Kawg-rau-Kawg: 8 Kauj Ruam (nrog Duab)

Video: Kev Paub Lub Ntsej Muag Lub Sij Hawm Tiag: Ib Qhov Kawg-rau-Kawg: 8 Kauj Ruam (nrog Duab)
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Hlis ntuj nqeg
Anonim
Kev lees paub lub ntsej muag ntawm lub sijhawm tiag tiag: Txoj Haujlwm Kawg-rau-kawg
Kev lees paub lub ntsej muag ntawm lub sijhawm tiag tiag: Txoj Haujlwm Kawg-rau-kawg

Ntawm kuv qhov kev qhia zaum kawg tshawb nrhiav OpenCV, peb kawm AUTOMATIC VISION OBJECT TRACKING. Tam sim no peb yuav siv peb PiCam kom paub lub ntsej muag hauv lub sijhawm tiag tiag, raws li koj tuaj yeem pom hauv qab no:

Duab
Duab

Txoj haujlwm no tau ua tiav nrog qhov zoo heev "Qhib Qhov Chaw Siv Lub Computer Vision", OpenCV. Ntawm qhov kev qhia no, peb yuav tsom mus rau Raspberry Pi (yog li, Raspbian li OS) thiab Python, tab sis kuv kuj tau sim cov cai ntawm Kuv Mac thiab nws kuj ua haujlwm tau zoo. OpenCV tau tsim los rau kev ua kom muaj txiaj ntsig zoo thiab nrog kev mob siab rau ntawm daim ntawv thov sijhawm tiag. Yog li, nws zoo tshaj plaws rau kev paub lub ntsej muag lub sijhawm tiag tiag uas siv lub koob yees duab.

Txhawm rau tsim kom tiav txoj haujlwm ntawm Kev lees paub lub ntsej muag, peb yuav tsum ua haujlwm ntawm 3 ntu sib txawv:

  1. Kev Tshawb Nrhiav Lub Ntsej Muag thiab Kev Sib Sau Cov Ntaub Ntawv
  2. Qhia tus neeg paub
  3. Lub ntsej muag lees paub

Daim duab hauv qab no thaiv rov ua cov theem no:

Duab
Duab

Kauj Ruam 1: BoM - Cov Nqi Khoom

Qhov loj:

  1. Raspberry Pi V3 - US $ 32.00
  2. 5 Megapixels 1080p Sensor OV5647 Mini Lub Koob Yees Duab Video Module - US $ 13.00

Kauj Ruam 2: Txhim Kho OpenCV 3 Pob

Txhim kho OpenCV 3 Pob
Txhim kho OpenCV 3 Pob

Kuv tab tom siv Raspberry Pi V3 hloov kho mus rau qhov kawg ntawm Raspbian (Ncab), yog li txoj hauv kev zoo tshaj kom muaj OpenCV teeb tsa, yog ua raws cov lus qhia tau zoo tsim los ntawm Adrian Rosebrock: Raspbian Stretch: Nruab OpenCV 3 + Python ntawm koj Raspberry Pi.

Kuv sim ntau qhov kev qhia sib txawv los teeb tsa OpenCV ntawm kuv tus Pi. Adrian txoj kev qhia yog qhov zoo tshaj plaws. Kuv qhia koj kom ua ib yam, ua raws nws cov lus qhia ib qib zuj zus.

Thaum koj ua tiav Adrian cov lus qhia, koj yuav tsum muaj OpenCV virtual ib puag ncig npaj los khiav peb cov kev sim ntawm koj Pi.

Cia peb mus rau peb ib puag ncig virtual thiab paub meej tias OpenCV 3 tau teeb tsa raug.

Adrian pom zoo kom khiav cov lus txib "qhov chaw" txhua lub sijhawm koj qhib lub davhlau ya nyob twg tshiab kom ntseeg tau tias koj cov kev hloov pauv hauv lub cev tau teeb tsa raug.

qhov chaw ~/.profile

Tom ntej no, cia nkag mus rau peb ib puag ncig virtual:

siv cv

Yog tias koj pom cov ntawv (cv) ua ntej koj ua tam sim ntawd, tom qab ntawd koj nyob hauv cv virtual ib puag ncig:

(cv) pi@raspberry: ~ $Adrian hu xov tooj mloog tias cv Python virtual ib puag ncig yog kev ywj pheej nkaus xwb thiab rov ua dua los ntawm qhov ua tsis raug Python version suav nrog rub tawm Raspbian Stretch. Yog li, ib qho Python pob hauv ntiaj teb lub vev xaib-pob ntawv teev npe yuav tsis muaj rau cv virtual ib puag ncig. Ib yam li ntawd, ib qho Python pob tau teeb tsa hauv qhov chaw-pob ntawm cv yuav tsis muaj rau thoob ntiaj teb kev teeb tsa Python

Tam sim no, nkag rau hauv koj tus neeg txhais lus Python:

nab hab sej

thiab lees paub tias koj tab tom khiav 3.5 (lossis siab dua) version

Hauv tus neeg txhais lus (">>>" yuav tshwm sim), ntshuam OpenCV lub tsev qiv ntawv:

ua cv2

Yog tias tsis muaj cov lus yuam kev tshwm sim, OpenCV raug teeb tsa raug rau ntawm koj lub VIRTUAL VIRTUAL ENVIRONMENT.

Koj tseem tuaj yeem tshawb xyuas OpenCV version teeb tsa:

cv2._ version_

3.3.0 yuav tsum tshwm sim (lossis zoo dua uas tuaj yeem tso tawm yav tom ntej). Cov Terminal PrintScreen saum toj no qhia cov kauj ruam dhau los.

Kauj Ruam 3: Kuaj Koj Lub Koob Yees Duab

Kuaj Koj Lub Koob Yees Duab
Kuaj Koj Lub Koob Yees Duab

Thaum koj tau OpenCV teeb tsa hauv koj li RPi cia peb sim kom paub tseeb tias koj lub koob yees duab ua haujlwm tau zoo.

Kuv tab tom kwv yees tias koj muaj PiCam twb tau teeb tsa ntawm koj li Raspberry Pi.

Sau tus lej Python hauv qab no ntawm koj daim IDE:

import numpy as np

ntshuam cv2 cap = cv2. VideoCapture (0) cap.set (3, 640) # teeb Dav cap.set (4, 480) # teeb Qhov Siab thaum (Tseeb): ret, thav duab = cap.read () thav duab = cv2. flip (thav duab, -1) # Tig lub koob yees duab ntsug grey = cv2.cvtColor (thav duab, cv2. COLOR_BGR2GRAY) cv2.imshow ('thav duab', thav duab) cv2.imshow ('grey', grey) k = cv2.waitKey (30) & 0xff yog k == 27: # nias 'ESC' kom txiav tawm qhov tawg cap.release () cv2.destroyAllWindows ()

Cov cai saum toj no yuav ntes cov vis dis aus uas yuav tsim los ntawm koj PiCam, tso tawm ob qho tib si, hauv BGR xim thiab hom Grey.

Nco ntsoov tias kuv tig kuv lub koob yees duab ntsug vim txoj kev nws tau sib sau ua ke. Yog tias nws tsis yog koj li ntaub ntawv, tawm tswv yim lossis rho tawm kab ntawv "flip".

Koj tuaj yeem hloov pauv txoj cai los ntawm kuv GitHub: simpleCamTest.py

Txhawm rau ua, sau cov lus txib:

nab hab sej SimpleCamTest.py

Txhawm rau ua tiav txoj haujlwm, koj yuav tsum nias tus yuam sij [ESC] ntawm koj cov keyboard.

Nyem koj nas ntawm lub qhov rais video, ua ntej nyem [ESC]

Daim duab saum toj no qhia qhov tshwm sim.

Qee tus kws tsim khoom pom teeb meem thaum sim qhib lub koob yees duab ("Kev lees paub ua tsis tiav" cov lus yuam kev). Qhov ntawd tuaj yeem tshwm sim yog tias lub koob yees duab tsis tau qhib thaum lub sijhawm OpenCv teeb tsa thiab yog li, lub koob yees duab tsav tsheb tsis teeb tsa kom raug. Txhawm rau kho, siv cov lus txib:

sudo modprobe bcm2835-v4l2

Koj kuj tseem tuaj yeem ntxiv bcm2835-v4l2 rau kab kawg ntawm /etc /modules file kom tus tsav tsheb thauj khoom ntawm khau raj.

Txhawm rau paub ntau ntxiv txog OpenCV, koj tuaj yeem ua raws li kev qhia: thauj khoom -video-python-opencv-tutorial

Kauj Ruam 4: Tshawb Pom Lub Ntsej Muag

Kev Pom Lub Ntsej Muag
Kev Pom Lub Ntsej Muag
Kev Pom Lub Ntsej Muag
Kev Pom Lub Ntsej Muag

Txoj haujlwm tseem ceeb tshaj plaws ntawm Kev lees paub lub ntsej muag yog qhov tseeb, "Tshawb Pom Lub ntsej muag". Ua ntej ib yam dab tsi, koj yuav tsum "ntes" lub ntsej muag (Phase 1) txhawm rau lees paub nws, thaum piv nrog lub ntsej muag tshiab raug ntes yav tom ntej (Phase 3).

Txoj hauv kev feem ntau txhawm rau txheeb xyuas lub ntsej muag (lossis ib yam khoom), yog siv "Haar Cascade classifier"

Kev Txheeb Xyuas Lub Hom Phiaj siv Haar cov cuab yeej ua raws txheej txheem txheej txheem yog qhov ua tau zoo txhawm rau txheeb xyuas cov txheej txheem uas tau thov los ntawm Paul Viola thiab Michael Jones hauv lawv daim ntawv, "Ceev Kev Tshawb Fawb Lub Hom Phiaj siv Boosted Cascade ntawm Cov Yam Ntxim Saib Ntxim Ua" hauv 2001. Nws yog lub tshuab kev kawm raws txoj hauv kev uas cascade muaj nuj nqi tau kawm los ntawm ntau cov duab zoo thiab tsis zoo. Tom qab ntawd nws tau siv los tshuaj xyuas cov khoom hauv lwm cov duab.

Ntawm no peb yuav ua haujlwm nrog nrhiav lub ntsej muag. Thaum pib, lub txheej txheem xav tau ntau cov duab zoo (cov duab ntawm lub ntsej muag) thiab cov duab tsis zoo (cov duab tsis muaj ntsej muag) los qhia tus neeg faib khoom. Tom qab ntawd peb yuav tsum rho cov yam ntxwv los ntawm nws. Qhov xov xwm zoo yog OpenCV los nrog tus kws qhia thiab tus kuaj pom. Yog tias koj xav qhia koj tus kheej cais rau ib yam khoom zoo li tsheb, dav hlau thiab lwm yam koj tuaj yeem siv OpenCV los tsim ib qho. Nws tag nrho cov ntsiab lus tau muab ntawm no: Cascade Classifier Training.

Yog tias koj tsis xav tsim koj tus kheej cais, OpenCV twb muaj ntau yam kev qhia paub ua ntej rau lub ntsej muag, qhov muag, luag ntxhi, thiab lwm yam. Cov ntaub ntawv XML tuaj yeem rub tawm los ntawm cov npe haarcascades.

Txoj kev xav txaus, cia peb tsim lub ntsej muag ntsuas nrog OpenCV!

Rub tawm cov ntawv: faceDetection.py los ntawm kuv GitHub.

import numpy as np

ntshuam cv2 faceCascade = cv2. CascadeClassifier ('Cascades/haarcascade_frontalface_default.xml') cap = cv2. VideoCapture (0) cap.set (3, 640) # teeb Dav cap.set (4, 480) # teeb Qhov Siab Thaum Tseeb: ret, img = cap.read () img = cv2.flip (img, -1) grey = cv2.cvtColor (img, cv2. COLOR_BGR2GRAY) ntsej muag = faceCascade.detectMultiScale (grey, scaleFactor = 1.2, min Neighbors = 5, minSize = (20, 20)) rau (x, y, w, h) hauv ntsej muag: cv2.rectangle (img, (x, y), (x+w, y+h), (255, 0, 0), 2) roi_gray = grey [y: y+h, x: x+w] roi_color = img [y: y+h, x: x+w] cv2.imshow ('video', img) k = cv2.waitKey (30) & 0xff yog k == 27: # nias 'ESC' kom txiav tawm qhov tawg cap.release () cv2.destroyAllWindows ()

Ntseeg nws lossis tsis ntseeg, ob peb kab kab lus saum toj no yog txhua yam koj xav tau los tshuaj xyuas lub ntsej muag, siv Python thiab OpenCV.

Thaum koj piv nrog tus lej kawg uas siv los ntsuas lub koob yees duab, koj yuav paub tias ob peb feem ntxiv rau nws. Nco tseg kab hauv qab no:

faceCascade = cv2. CascadeClassifier ('Cascades/haarcascade_frontalface_default.xml')

Nov yog kab ntawv uas thauj cov "classifier" (uas yuav tsum muaj nyob hauv cov npe "Cascades/", hauv qab koj qhov project directory).

Tom qab ntawd, peb yuav teeb tsa peb lub koob yees duab thiab sab hauv lub voj, ntsaws peb cov vis dis aus tawm hauv hom greyscale (tib yam peb tau pom ua ntej).

Tam sim no peb yuav tsum hu peb cov neeg ua haujlwm cais tawm, hla nws qee qhov tsis tseem ceeb, raws li qhov ntsuas, tus naj npawb ntawm cov neeg nyob ze thiab qhov tsawg kawg ntawm qhov pom ntawm lub ntsej muag.

ntsej muag = faceCascade.detectMultiScale (grey, scaleFactor = 1.2, min Neighbors = 5, minSize = (20, 20))

Qhov twg,

  • grey yog cov duab grayscale nkag.
  • scaleFactor yog qhov ntsuas ntsuas ntau npaum li cas cov duab loj tau raug txo ntawm txhua qhov ntsuas duab. Nws yog siv los tsim cov nplai pyramid.
  • min Neighbors yog qhov ntsuas tshwj xeeb muaj pes tsawg tus neeg nyob ze txhua tus neeg sib tw duab plaub yuav tsum muaj, txhawm rau khaws nws. Tus lej siab dua muab qis dua qhov tsis zoo.
  • minSize yog qhov loj me me ntawm lub kaum sab xis kom txiav txim siab lub ntsej muag.

Lub luag haujlwm yuav pom lub ntsej muag ntawm daim duab. Tom ntej no, peb yuav tsum "cim" lub ntsej muag hauv daim duab, siv, piv txwv li, lub duab plaub xiav. Qhov no ua tiav nrog ntu no ntawm cov cai:

rau (x, y, w, h) hauv ntsej muag:

cv2.rectangle (img, (x, y), (x+w, y+h), (255, 0, 0), 2) roi_gray = grey [y: y+h, x: x+w] roi_color = img [y: y+h, x: x+w]

Yog pom lub ntsej muag, nws rov ua txoj haujlwm ntawm pom lub ntsej muag ua lub duab plaub nrog lub kaum sab laug (x, y) thiab muaj "w" raws li nws Dav thiab "h" raws li nws qhov siab ==> (x, y, w, h). Thov saib daim duab saum toj no.

Thaum peb tau txais cov chaw no, peb tuaj yeem tsim "ROI" (kos duab plaub) rau lub ntsej muag thiab nthuav qhia qhov tshwm sim nrog imshow () muaj nuj nqi.

Khiav cov ntawv nab hab sej saum toj no ntawm koj thaj tsam nab, siv Rpi Terminal:

nab hab sej faceDetection.py

Qhov tshwm sim:

Duab
Duab

Koj kuj tseem tuaj yeem suav nrog cov neeg sib cais rau "nrhiav lub qhov muag" lossis tseem "luag ntxhi". Ntawm cov xwm txheej no, koj yuav suav nrog kev faib tawm ua haujlwm thiab kos duab plaub sab hauv lub ntsej muag lub ntsej muag, vim tias yuav tsis muaj qhov nkag siab pom lub qhov muag lossis luag sab nraum lub ntsej muag.

Nco ntsoov tias ntawm Pi, muaj ntau tus neeg sib cais ntawm tib tus lej yuav ua rau qeeb, ib zaug txoj kev tshawb pom no (HaarCascades) siv ntau qhov kev suav ua lub zog. Ntawm lub desktop, nws yooj yim dua los khiav nws.

Ntawm kuv GitHub koj yuav pom lwm qhov piv txwv:

faceEyeDetection.py

ntsej muagSmileDetection.py

ntsej muagSmileEyeDetection.py

Thiab hauv daim duab saum toj no, koj tuaj yeem pom qhov tshwm sim.

Koj tseem tuaj yeem ua raws cov lus qhia hauv qab no kom nkag siab zoo txog Kev Pom Lub ntsej muag:

Haar Cascade Object Detection Face & Eye OpenCV Python Tutorial

Kauj Ruam 5: Kev Sib Sau Cov Ntaub Ntawv

Kev Sau Cov Ntaub Ntawv
Kev Sau Cov Ntaub Ntawv
Kev Sau Cov Ntaub Ntawv
Kev Sau Cov Ntaub Ntawv

Ua ntej tshaj plaws, kuv yuav tsum ua tsaug Ramiz Raja rau nws txoj haujlwm zoo ntawm Face Recognition ntawm cov duab:

FACE RECOGNITION SIV OPENCV THIAB PYTHON: Tus Thawj Coj Ua Haujlwm

thiab kuj Anirban Kar, uas tau tsim kev qhia paub ntau yam siv yees duab:

FACE RECOGNITION - 3 ntu

Kuv pom zoo tiag tiag uas koj ua tib zoo saib ob qho kev qhia.

Hais tias, cia peb pib thawj theem ntawm peb tes num. Peb yuav ua dab tsi ntawm no, yog pib los ntawm cov kauj ruam kawg (Tshawb Pom Lub ntsej muag), peb yuav yooj yim tsim cov ntaub ntawv, qhov uas peb yuav khaws cia rau txhua tus lej, ib pab pawg ntawm cov duab hauv grey nrog rau qhov uas tau siv rau lub ntsej muag tshuaj xyuas.

Ua ntej, tsim cov npe uas koj txhim kho koj txoj haujlwm, piv txwv li, FacialRecognitionProject:

mkdir FacialRecognitionProject

Hauv phau ntawv qhia no, ntxiv rau 3 tsab ntawv nab nab uas peb yuav tsim rau peb tes num, peb yuav tsum tau khaws tseg rau ntawm nws lub ntsej muag cais. Koj tuaj yeem rub nws los ntawm kuv GitHub: haarcascade_frontalface_default.xml

Tom ntej no, tsim cov subdirectory qhov twg peb yuav khaws peb cov qauv ntawm lub ntsej muag thiab npe nws "cov ntaub ntawv":

mkdir cov ntaub ntawv

Thiab rub tawm cov cai los ntawm kuv GitHub: 01_face_dataset.py

ua cv2

ntshuam os cam = cv2. VideoCapture (0) cam.set (3, 640) # teeb tsa video dav cam.set (4, 480) # teeb tsa qhov siab video face_detector = cv2. CascadeClassifier ('haarcascade_frontalface_default.xml') # Rau txhua tus neeg, nkag mus rau ib tus lej ntsej muag id face_id = input ('\ n nkag mus rau tus neeg siv tus lej kawg nias ==>') luam tawm ("\ n [INFO] Pib ua lub ntsej muag ntes. Saib lub koob yees duab thiab tos … = 0 thaum (Tseeb): ret, img = cam.read () img = cv2.flip (img, -1) # tig cov duab yees duab ntsug grey = cv2.cvtColor (img, cv2. COLOR_BGR2GRAY) ntsej muag = face_detector.detectMultiScale (grey, 1.3, 5) rau (x, y, w, h) hauv ntsej muag: cv2.rectangle (img, (x, y), (x+w, y+h), (255, 0, 0), 2) suav + = 1 # Khaws cov duab ntes rau hauv cov ntaub ntawv khaws cia cv2.imwrite ("dataset/User." + str (face_id) + '.' + str (suav) + ".jpg", grey [y: y + h, x: x+w]) cv2.imshow ('duab', img) k = cv2.waitKey (100) & 0xff # Nias 'ESC' kom tawm video yog k == 27: so elif count> = 30: # Nqa 30 lub ntsej muag piv txwv thiab tso tseg cov vis dis aus # Ua ab nws ntawm kev ntxuav tu luam ntawv ("\ n [INFO] Tawm Tawm Txoj Haujlwm thiab ntxuav cov khoom") cam.release () cv2.destroyAllWindows ()

Cov cai yog zoo ib yam li cov cai uas peb pom rau lub ntsej muag pom. Dab tsi peb ntxiv, yog "cov lus txib nkag mus" txhawm rau ntes tus neeg siv tus lej, uas yuav tsum yog tus lej naj npawb (1, 2, 3, thiab lwm yam)

face_id = input ('\ n nkag mus rau tus neeg siv tus lej kawg nias ==>')

Thiab rau txhua tus ntawm cov ntas ntes, peb yuav tsum khaws nws ua cov ntaub ntawv ntawm "cov ntaub ntawv" cov npe:

cv2.imwrite ("dataset/User." + str (face_id) + '.' + str (suav) + ".jpg", grey [y: y + h, x: x + w])

Nco ntsoov tias txhawm rau khaws cov ntaub ntawv saum toj no, koj yuav tsum tau xa cov tsev qiv ntawv "os". Txhua lub npe ntawv yuav ua raws tus qauv:

User.face_id.count.jpg

Piv txwv li, rau tus neeg siv nrog lub ntsej muag_id = 1, daim ntawv ua piv txwv 4 ntawm cov ntaub ntawv/ npe yuav yog qee yam xws li:

Tus neeg siv 1.4.jpg

raws li qhia hauv daim duab saum toj no los ntawm kuv Pi. Ntawm kuv tus lej, Kuv tabtom ntes 30 qhov piv txwv los ntawm txhua tus lej. Koj tuaj yeem hloov nws ntawm qhov kawg "elif". Tus naj npawb ntawm cov qauv yog siv txhawm rau txhawm rau lub voj uas lub ntsej muag raug ntes.

Khiav Python tsab ntawv thiab ntes ob peb Ids. Koj yuav tsum sau tsab ntawv txhua lub sijhawm uas koj xav sau ua ke cov neeg siv tshiab (lossis hloov cov duab rau ib qho uas twb muaj lawm).

Kauj ruam 6: Tus kws qhia

Tus kws qhia
Tus kws qhia

Nyob rau theem thib ob no, peb yuav tsum nqa tag nrho cov neeg siv cov ntaub ntawv los ntawm peb cov ntaub ntawv thiab "tus kws qhia" OpenCV Recognizer. Qhov no ua tiav ncaj qha los ntawm kev ua haujlwm tshwj xeeb OpenCV. Qhov tshwm sim yuav yog.yml cov ntaub ntawv uas yuav raug cawm ntawm "tus kws qhia/" cov npe.

Yog li, cia peb pib tsim cov subdirectory uas peb yuav khaws cov ntaub ntawv kawm:

mkdir tus kws qhia

Rub tawm los ntawm kuv GitHub tsab ntawv nab thib ob: 02_face_training.py

ua cv2

import numpy li np los ntawm PIL ntshuam Image ntshuam os # Txoj kev rau lub ntsej muag duab database path = 'dataset' lees paub = cv2.face. LBPHFaceRecognizer_create () ntes = cv2. CascadeClassifier ("haarcascade_frontalface_default.xml"); # ua haujlwm kom tau txais cov duab thiab daim ntawv lo cov ntaub ntawv def getImagesAndLabels (txoj kev): imagePaths = [os.path.join (path, f) rau f hauv os.listdir (path)] faceSamples = id = rau imagePath hauv imagePaths: PIL_img = Image.open (imagePath).convert ('L') # hloov nws mus rau grayscale img_numpy = np.array (PIL_img, 'uint8') id = int (os.path.split (imagePath) [-1]. phua (".") [1]) ntsej muag = detector.detectMultiScale (img_numpy) rau (x, y, w, h) hauv ntsej muag: faceSamples.append (img_numpy [y: y+h, x: x+w]) ids.append (id) rov ntsej muagSamples, ids print ("\ n [INFO] Kev cob qhia lub ntsej muag. Nws yuav siv ob peb feeb. Tos …") ntsej muag, ids = getImagesAndLabels (txoj kev) recognizer.train (ntsej muag, np.array (ids)) # Txuag tus qauv rau tus kws qhia/tus kws qhia. yml lees paub.write ('tus kws qhia/tus kws qhia.yml') # tus lees paub.save () ua haujlwm ntawm Mac, tab sis tsis yog Pi # Luam tus lej ntawm lub ntsej muag uas tau kawm thiab xaus qhov program luam tawm ("\ n [INFO] {0} ntsej muag tau kawm tiav. Tawm ntawm Txoj Haujlwm".format (len (np.unique (ids))))

Paub meej tias koj muaj PIL tsev qiv ntawv teeb tsa ntawm koj li Rpi. Yog tias tsis yog, khiav cov lus txib hauv qab no hauv Terminal:

pip nruab hauv ncoo

Peb yuav siv los ua tus lees paub, LBPH (LOCAL BINARY PATTERNS HISTOGRAMS) Lub ntsej muag lees paub, suav nrog hauv OpenCV pob. Peb ua qhov no hauv kab hauv qab no:

lees paub = cv2.face. LBPHFaceRecognizer_create ()

Txoj haujlwm "getImagesAndLabels (txoj kev)", yuav nqa tag nrho cov duab ntawm cov npe: "cov ntaub ntawv/", rov qab 2 arrays: "Ids" thiab "ntsej muag". Nrog cov arrays ua cov tswv yim, peb yuav "qhia peb tus neeg lees paub":

recognizer.train (ntsej muag, id)

Raws li qhov tshwm sim, cov ntaub ntawv npe "trainer.yml" yuav raug cawm hauv cov kws qhia ntawv uas tau tsim los ntawm peb yav dhau los.

Qhov ntawd yog nws! Kuv suav nrog daim ntawv tshaj tawm qhov kawg uas kuv tau pom rau kev lees paub, tus naj npawb ntawm Tus Neeg Siv Lub ntsej muag peb tau kawm.

Txhua lub sijhawm koj ua Phase 1, Phase 2 yuav tsum tau ua

Kauj Ruam 7: Tus lees paub

Paub
Paub
Paub
Paub

Tam sim no, peb tau mus txog theem kawg ntawm peb txoj haujlwm. Ntawm no, peb yuav ntes lub ntsej muag tshiab ntawm peb lub koob yees duab thiab yog tias tus neeg no tau ntes nws lub ntsej muag thiab tau kawm ua ntej, peb tus neeg lees paub yuav ua "twv ua ntej" rov qab nws tus lej thiab tus lej ntsuas, qhia tias muaj kev ntseeg siab npaum li cas nrog qhov kev sib tw no.

Cia peb rub kab ntawv thib 3 ntawm nab nab los ntawm kuv GitHub: 03_face_recognition.py.

ua cv2

import numpy as np import os recognizer = cv2.face. LBPHFaceRecognizer_create () recognizer.read ('tus kws qhia/kws qhia ntawv.yml') cascadePath = "haarcascade_frontalface_default.xml" faceCascade = cv2. CascadeClassifier (cascadePath); font = cv2. FONT_HERSHEY_SIMPLEX #iniciate id counter id = 0 # cov npe cuam tshuam nrog id: piv txwv ==> Marcelo: id = 1, thiab lwm yam npe = ['Tsis muaj', 'Marcelo', 'Paula', 'Ilza', 'Z ',' W '] # Pib thiab pib lub sijhawm tiag tiag video ntes cam = cv2. VideoCapture (0) cam.set (3, 640) # teeb tsa video widht cam.set (4, 480) # teeb tsa video siab # Txhais min qhov loj me kom pom tau tias yog lub ntsej muag minW = 0.1*cam.get (3) minH = 0.1*cam.get (4) thaum Tseeb: ret, img = cam.read () img = cv2.flip (img, -1) # Tig vertically grey = cv2.cvtColor (img, cv2. COLOR_BGR2GRAY) ntsej muag = faceCascade.detectMultiScale (grey, scaleFactor = 1.2, min Neighbors = 5, minSize = (int (minW), int (minH)),) rau (x, y, w, h) hauv ntsej muag: cv2.rectangle (img, (x, y), (x+w, y+h), (0, 255, 0), 2) id, cog qoob loo = recognizer.predict (grey [y: y+h, x: x+w]) # Txheeb xyuas yog tias kev ntseeg siab tsawg dua 100 ==> "0" yog qhov sib tw zoo tshaj yog tias (kev ntseeg siab <100): id = npe [id] kev ntseeg siab = "{0}% ".format (puag ncig (100 - kev ntseeg siab)) lwm qhov: id =" tsis paub "kev ntseeg siab =" {0}%". hom ntawv (puag ncig (100 - conf idence)) cv2.putText (img, str (id), (x+5, y-5), font, 1, (255, 255, 255), 2) cv2.putText (img, str (cog qoob loo),, x+5, y+h-5), font, 1, (255, 255, 0), 1) cv2.imshow ('camera', img) k = cv2.waitKey (10) & 0xff # Nias 'ESC' rau kev tawm vis dis aus yog k == 27: so # Ua qhov me me ntawm kev ntxuav tu ("\ n [INFO] Tawm Qhov Kev Pabcuam thiab ntxuav cov khoom") cam.release () cv2.destroyAllWindows ()

Peb suav nrog ntawm no cov kab tshiab, yog li peb yuav tso tawm "cov npe", tsis txhob suav tus lej:

cov npe = ['Tsis muaj', 'Marcelo', 'Paula', 'Ilza', 'Z', 'W']

Yog li, piv txwv li: Marcelo yuav tus neeg siv nrog id = 1; Paula: id = 2, thiab lwm yam.

Tom ntej no, peb yuav kuaj pom lub ntsej muag, tib yam peb tau ua ua ntej nrog tus cais haasCascade. Muaj lub ntsej muag pom peb tuaj yeem hu ua txoj haujlwm tseem ceeb tshaj plaws hauv cov cai saum toj no:

id, kev ntseeg siab = lees paub. twv ua ntej (grey feem ntawm lub ntsej muag)

Tus lees paub.predict (), yuav ua raws qhov ntsuas ib feem ntawm lub ntsej muag kom raug tshuaj xyuas thiab yuav rov qab nws tus tswv uas yuav tshwm sim, qhia nws tus lej thiab kev ntseeg siab npaum li cas tus lees paub hauv kev sib raug zoo nrog qhov kev sib tw no.

Nco ntsoov tias qhov ntsuas kev ntseeg siab yuav rov "xoom" yog tias nws yuav suav hais tias yog kev sib tw zoo tshaj plaws

Thiab thaum kawg, yog tus lees paub tuaj yeem kwv yees lub ntsej muag, peb muab cov ntawv sau rau ntawm daim duab nrog qhov tshwm sim tus lej thiab ntau npaum li cas yog "qhov tshwm sim" hauv % uas qhov kev sib tw raug ("qhov tshwm sim" = 100 - kev ntseeg siab ntsuas). Yog tsis yog, daim ntawv "tsis paub" tau muab tso rau ntawm lub ntsej muag.

Hauv qab-g.webp

Duab
Duab

Ntawm daim duab saum toj no, Kuv qhia qee qhov kev xeem ua tiav nrog txoj haujlwm no, qhov twg kuv kuj tau siv cov duab los txheeb xyuas yog tias tus neeg lees paub ua haujlwm.

Kauj Ruam 8: Xaus

Xaus
Xaus

Raws li ib txwm muaj, Kuv vam tias txoj haujlwm no tuaj yeem pab lwm tus pom lawv txoj hauv kev mus rau hauv lub ntiaj teb zoo ntawm hluav taws xob!

Yog xav paub ntxiv thiab tus lej kawg, thov mus saib kuv qhov kev tso nyiaj GitHub: OpenCV-Face-Recognition

Yog xav paub ntau ntxiv, thov mus saib kuv qhov blog: MJRoBot.org

Hauv qab qhov pom ntawm kev qhia yav tom ntej, qhov uas peb yuav tshawb nrhiav "tsis siv neeg lub ntsej muag taug qab thiab lwm txoj hauv kev rau kev nrhiav lub ntsej muag":

Duab
Duab

Saludos los ntawm sab qab teb ntawm lub ntiaj teb!

Pom koj hauv kuv cov lus qhia tom ntej!

Ua tsaug, Marcelo

Pom zoo: