Cov txheej txheem:

Kev Tshawb Fawb Lub Ntsej Muag+paub: 8 Kauj Ruam (nrog Duab)
Kev Tshawb Fawb Lub Ntsej Muag+paub: 8 Kauj Ruam (nrog Duab)

Video: Kev Tshawb Fawb Lub Ntsej Muag+paub: 8 Kauj Ruam (nrog Duab)

Video: Kev Tshawb Fawb Lub Ntsej Muag+paub: 8 Kauj Ruam (nrog Duab)
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Kaum ib hlis
Anonim
Image
Image
Kev Tshawb Fawb Lub ntsej muag+kev lees paub
Kev Tshawb Fawb Lub ntsej muag+kev lees paub

Nov yog qhov piv txwv yooj yim ntawm kev ua kom pom lub ntsej muag thiab paub nrog OpenCV los ntawm lub koob yees duab. NCO TSEG: Kuv tau ua txoj haujlwm no rau SENSOR CONTEST THIAB Kuv tau siv lub koob yees duab los ua lub ntsej muag txhawm rau taug qab thiab RECOGNITION FACES. So, Peb Lub Hom Phiaj Hauv ntu no, 1. Txhim Kho Anaconda 2. Rub Tawm Qhib CV Pob 3. Teeb Ib puag ncig hloov pauv 4. Kuaj xyuas kom paub meej 5. Ua tus lej kom pom lub ntsej muag 6. Ua tus lej txhawm rau tsim cov ntaub ntawv teeb tsa 7. Ua tus lej los qhia tus neeg paub 8. Ua tus lej kom paub lub ntsej muag & Cov txiaj ntsig.

Kauj ruam 1: Nruab Anaconda

Nruab Anaconda
Nruab Anaconda

Anaconda yog qhov tseem ceeb ntim tau zoo Python IDE uas tau xa nrog ntau pob khoom muaj txiaj ntsig, xws li NumPy, Pandas, IPython Notebook, thiab lwm yam Nws zoo li tau pom zoo nyob txhua qhov hauv zej zog kev tshawb fawb. Txheeb xyuas Anaconda kom nws tau teeb tsa.

Kauj Ruam 2: Rub Tawm Qhib CV Pob

Rub tawm Qhib CV Pob
Rub tawm Qhib CV Pob

Ua ntej tshaj, mus rau qhov chaw OpenCV raug cai txhawm rau rub tawm pob OpenCV ua tiav. Xaiv qhov koj nyiam (2.x lossis 3.x). Kuv nyob ntawm Python 2.x thiab OpenCV 2.x - feem ntau vim qhov no yog li cas OpenCV -Python Tutorials tau teeb tsa/raws li.

Hauv kuv qhov xwm txheej, kuv tau rho tawm pob (qhov tseem ceeb yog daim nplaub tshev) ncaj qha rau kuv F tsav. (F: / opencv).

Kauj Ruam 3: Teem Ib puag ncig hloov pauv

Teem Ib puag ncig hloov pauv
Teem Ib puag ncig hloov pauv

Luam thiab Muab tshuaj txhuam cov ntaub ntawv cv2.pyd

Anaconda Site-packages directory (piv txwv li F: / Program Files / Anaconda2 / Lib / site-packages in my case) muaj Python tej pob khoom uas koj tuaj yeem nqa tau. Peb lub hom phiaj yog theej thiab muab cov ntaub ntawv cv2.pyd tso rau hauv phau ntawv no (kom peb tuaj yeem siv cov ntawv ntshuam cv2 hauv peb cov lej nab npawb.)

Txhawm rau ua qhov no, luam daim ntawv cv2.pyd …

Los ntawm phau ntawv OpenCV no (ntu pib yuav txawv me ntsis ntawm koj lub tshuab):

# Python 2.7 thiab 64-ntsis tshuab: F: / opencv / build / python / 2.7 / x64# Python 2.7 thiab 32-ntsis tshuab: F: / opencv / build / python / 2.7 / x84

Txog phau ntawv Anaconda no (ntu pib yuav txawv me ntsis ntawm koj lub tshuab):

F: / Program Files / Anaconda2 / Lib / site-packages

Tom qab ua tiav cov kauj ruam no peb yuav tam sim no tuaj yeem siv ntshuam cv2 hauv Python code. TAB SIS, peb tseem yuav tsum ua haujlwm ntxiv me ntsis kom tau FFMPEG (video codec) los ua haujlwm (kom pab peb ua tej yam xws li ua cov vis dis aus.)

Txoj cai-nias ntawm "Kuv Lub Computer" (lossis "Lub PC no" ntawm Windows 8.1)-> sab laug-nyem Cov Khoom-> sab laug-nyem "Advanced" tab-> sab laug-nyem "Ib puag ncig hloov pauv …" khawm. taw tes rau OpenCV (xws li x86 rau 32-ntsis system lossis x64 rau 64-ntsis system.) Tam sim no kuv nyob ntawm lub tshuab 64-ntsis.

32-bitOPENCV_DIRC: / opencv / build / x86 / vc12

64-bitOPENCV_DIRC: / opencv / build / x64 / vc12

Ntxiv rau %OPENCV_DIR %\ bin rau Tus Neeg Siv Hloov PATH.

Piv txwv li, kuv tus neeg siv PATH qhov txawv txav zoo li qhov no …

Ua ntej:

F: / Cov neeg siv / Johnny / Anaconda; C: / Cov neeg siv / Johnny / Anaconda / Scripts

Tom qab:

F: / Cov neeg siv / Johnny / Anaconda; C: / Cov neeg siv / Johnny / Anaconda / Scripts;%OPENCV_DIR%\ bin

Nov yog nws peb tau ua tiav! FFMPEG tau npaj los siv!

Kauj Ruam 4: Xeem Kom Pom Zoo

Xeem kom paub meej
Xeem kom paub meej
Xeem kom paub meej
Xeem kom paub meej

Peb yuav tsum sim seb peb puas tuaj yeem ua cov no hauv Anaconda (ntawm Spyder IDE):

  • Ntshuam OpenCV pob
  • Siv FFMPEG cov khoom siv (nyeem/sau/txheej txheem cov vis dis aus)

Kuaj 1: Peb puas tuaj yeem nqa OpenCV?

Txhawm rau lees paub tias Anaconda tam sim no tuaj yeem ntsaws OpenCV-Python pob (uas yog, cv2), muab cov no tso rau hauv IPython Console:

ua cv2

sau cv2._ version_

Yog tias pob ntawv cv2 tau xa tawm ok uas tsis muaj qhov yuam kev, thiab cov ntawv cv2 tau luam tawm, ces peb txhua tus zoo!

Ntsuas 2: Peb puas tuaj yeem siv FFMPEG codec?

Muab ib qho piv txwv

input_video.mp4

video file hauv phau ntawv teev npe. Peb xav sim seb peb puas tuaj yeem:

  • nyeem qhov no.mp4 video file, thiab
  • sau tawm cov vis dis aus tshiab (tuaj yeem yog.avi lossis.mp4 thiab lwm yam)

Txhawm rau ua qhov no peb yuav tsum muaj qhov ntsuas tus nab npawb nab npawb, hu nws test.py. Muab nws tso rau hauv cov npe tib yam li cov qauv

input_video.mp4

ntaub ntawv.

Nov yog dab tsi

kev xeem.py

tej zaum yuav zoo li (Nco tseg: ntau ua tsaug rau Pete's thiab Warren cov lus qhia hauv cov lus teb - Kuv tau hloov kuv tus lej xeem thawj nrog nws - thov sim nws koj tus kheej thiab qhia rau peb paub tias qhov no ua haujlwm zoo dua):

ua cv2

cap = cv2. VideoCapture ("input_video.mp4") luam tawm cap.isOpened () # Tseeb = nyeem video tau zoo. Cuav - tsis nyeem video. fourcc = cv2. VideoWriter_fourcc (*'XVID') out = cv2. VideoWriter ("output_video.avi", fourcc, 20.0, (640, 360)) print out.isOpened () # Tseeb = sau cov vis dis aus tau zoo. Cuav - ua tsis tiav sau cov vis dis aus. cap.release () tawm.release ()

Qhov kev xeem no tseem ceeb heev. Yog tias koj xav ua cov ntaub ntawv video, koj yuav tsum ua kom ntseeg tau tias Anaconda / Spyder IDE tuaj yeem siv FFMPEG (video codec). Nws siv kuv hnub kom tau nws ua haujlwm. Tab sis kuv vam tias nws yuav siv sijhawm koj tsawg dua!:) Nco tseg: ib qho lus qhia tseem ceeb ntxiv thaum siv Anaconda Spyder IDE. Nco ntsoov xyuas koj Daim Ntawv Qhia Ua Haujlwm Tam Sim No (CWD) !!!

Kauj Ruam 5: Ua Txoj Cai rau Kev Pom Lub Ntsej Muag

Ua Code rau Kev Pom Lub Ntsej Muag
Ua Code rau Kev Pom Lub Ntsej Muag
Ua Code rau Kev Pom Lub Ntsej Muag
Ua Code rau Kev Pom Lub Ntsej Muag

Lub hom phiaj

Hauv ntu no,

  • Peb yuav pom cov hauv paus ntawm kev paub lub ntsej muag siv Haar Feature-based Cascade Classifiers
  • Peb yuav txuas ntxiv tib yam rau qhov muag pom thiab lwm yam

Haar-cascade Detection in OpenCV

Ntawm no peb yuav daws nrog kev txheeb xyuas. OpenCV twb muaj ntau tus neeg kawm paub ua ntej rau lub ntsej muag, qhov muag, luag thiab lwm yam. Cov ntaub ntawv XML no tau khaws cia rau hauv opencv/data/haarcascades/folder. Cia peb tsim lub ntsej muag thiab lub qhov muag pom nrog OpenCV. Ua ntej peb yuav tsum tau ntim cov XML uas xav tau cais. Tom qab ntawd ntsaws peb cov duab nkag (lossis yees duab) hauv hom grayscale LOSSIS peb tuaj yeem siv lub koob yees duab (rau lub sijhawm pom lub ntsej muag tiag tiag)

import numpy as np

ntshuam cv2 face_cascade = cv2. CascadeClassifier ('F:/Program Files/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml') eye_cascade = cv2. CascadeClassifier ('F:/Program Files/opencv/sources/data/haarcascades.xml ') cap = cv2. VideoCapture (0) thaum 1: ret, img = cap.read () grey = cv2.cvtColor (img, cv2. COLOR_BGR2GRAY) ntsej muag = face_cascade.detectMultiScale (grey, 1.5, 5) 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] qhov muag = eye_cascade.detectMultiScale (roi_gray) rau (ex, ey, ew, eh) hauv qhov muag: cv2.rectangle (roi_color, (ex, ey), (ex +ew, ey +eh), (0, 255, 0), 2) luam "pom" +str (len (ntsej muag)) +"ntsej muag (s)" cv2.imshow ('img', img) k = cv2.waitKey (30) & 0xff yog k == 27: so cap.release () cv2.destroyAllWindows ()

Kauj Ruam 6: Ua Code los Tsim Cov Ntaub Ntawv Teev

Ua Code los Tsim Cov Ntaub Ntawv Teeb
Ua Code los Tsim Cov Ntaub Ntawv Teeb
Ua Code los Tsim Cov Ntaub Ntawv Teeb
Ua Code los Tsim Cov Ntaub Ntawv Teeb

Peb tab tom ua lub ntsej muag paub, yog li koj yuav xav tau qee lub ntsej muag duab! Koj tuaj yeem tsim koj tus kheej cov ntaub ntawv lossis pib nrog ib qho ntawm lub ntsej muag muaj cov ntaub ntawv khaws cia, https://face-rec.org/databases/ muab cov ntaub ntawv tshiab rau koj. Peb nthuav cov ntaub ntawv khaws tseg yog (ib feem ntawm qhov kev piav qhia tau hais los ntawm

  • AT & T Facedatabase
  • Yale Facedatabase A.
  • Ntxiv Yale Facedatabase B

NTUJ kuv siv kuv tus kheej cov ntaub ntawv…. Nrog kev pab ntawm cov lej uas tau muab hauv qab no:

import numpy as np

ntshuam cv2 face_cascade = cv2. CascadeClassifier ('F:/Program Files/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml') cap = cv2. VideoCapture (0) id = raw_input ('enter user id') sampleN = 0; thaum 1: ret, img = cap.read () grey = cv2.cvtColor (img, cv2. COLOR_BGR2GRAY) ntsej muag = face_cascade.detectMultiScale (grey, 1.3, 5) rau (x, y, w, h) hauv ntsej muag: qauv N = piv txwv N+1; cv2.imwrite ("F:/Program Files/projects/face_rec/facesData/User."+str (id)+"."+str (sampleN)+".jpg", grey [y: y+h, x: x+w]) cv2.rectangle (img, (x, y), (x+w, y+h), (255, 0, 0), 2) cv2.waitKey (100) cv2.imshow ('img', img) cv2.waitKey (1) yog qauv N> 20: so cap.release () cv2.destroyAllWindows ()

Kauj Ruam 7: Ua Txoj Cai los Qhia Tus Neeg Paub

Ua Code los Qhia Tus Neeg Paub
Ua Code los Qhia Tus Neeg Paub

Tsim kom muaj nuj nqi los npaj cov txheej txheem kawm

Tam sim no, peb yuav txhais lub luag haujlwm

getImagesWithID (txoj kev)

uas siv txoj hauv kev tsis meej rau cov duab database raws li kev sib cav tswv yim thiab xa rov qab tuple ntawm 2 daim ntawv teev npe, ib qho uas muaj lub ntsej muag pom thiab lwm yam uas muaj cov ntawv sib thooj rau lub ntsej muag ntawd. Piv txwv li, yog tias qhov ntsuas ith hauv daim ntawv teev npe ntsej muag sawv cev rau tus neeg thib 5 hauv cov ntaub ntawv khaws tseg, tom qab ntawd qhov sib thooj ith qhov chaw nyob hauv daim ntawv teev npe muaj tus nqi sib npaug 5.

Tam sim no hloov cov ntaub ntawv cov ntsej muag (uas tau tsim hauv qib 6) rau hauv.yml cov ntaub ntawv nrog kev pab ntawm cov lej uas tau muab hauv qab no:

import os

ntshuam tus lej raws li np ntshuam cv2 los ntawm PIL ntshuam duab # Rau lub ntsej muag lees paub peb yuav LBPH Lub ntsej muag lees paub tus lees paub = cv2.createLBPHFaceRecognizer (); path = "F:/Program Files/projects/face_rec/facesData" def getImagesWithID (path): imagePaths = [os.path.join (path, f) for f in os.listdir (path)] # print image_path #getImagesWithID (path) face = IDs = rau imagePath hauv imagePaths: # Nyeem cov duab thiab hloov mus rau grayscale facesImg = Image.open (imagePath).convert ('L') faceNP = np.array (facesImg, 'uint8') # Tau txais daim ntawv lo ntawm daim duab ID = int (os.path.split (imagePath) [-1].split (".") [1]) # Tshawb pom lub ntsej muag hauv daim duab ntsej muag.append (faceNP) IDs.append (ID) cv2.imshow ("Ntxiv ntsej muag rau kev ua haujlwm", faceNP) cv2.waitKey (10) xa np.array (IDs), ntsej muag Ids, ntsej muag = getImagesWithID (txoj kev) lees paub.train (ntsej muag, Ids) lees paub.save ("F:/Program Files/projects/face_rec/faceREC/trainingdata.yml") cv2.destroyAllWindows ()

los ntawm kev siv cov cai no txhua lub ntsej muag cov ntaub ntawv hloov pauv mus rau hauv ib qho.yml cov ntaub ntawv …..path qhov chaw yog ("F:/Cov Ntaub Ntawv Cov Haujlwm/cov haujlwm/face_rec/faceREC/trainingdata.yml")

Kauj Ruam 8: Ua Code kom paub txog Lub ntsej muag & Cov txiaj ntsig

Guyzz qhov no yog cov kauj ruam kawg uas peb tuaj yeem tsim cov cai kom paub lub ntsej muag nrog kev pab ntawm koj lub vev xaib HNUB NO KAWM NTAWV YUAV TSUM muaj ob txoj haujlwm uas yuav mus rau qhov ua tiav …. 1. ntes cov vis dis aus los ntawm cam 2. piv nws nrog koj cov ntaub ntawv.yml

import numpy as npimport cv2 face_cascade = cv2. CascadeClassifier ('F:/Program Files/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml') cap = cv2. VideoCapture (0) rec = cv2.createLBPHFaceRecognizer (); rec.load ("F:/Program Files/projects/face_rec/faceREC/trainingdata.yml") id = 0 font = cv2.cv. InitFont (cv2.cv. CV_FONT_HERSHEY_COMPLEX_SMALL, 5, 1, 0, 4) thaum 1: ret, img = cap.read () grey = cv2.cvtColor (img, cv2. COLOR_BGR2GRAY) ntsej muag = face_cascade.detectMultiScale (grey, 1.5, 5) rau (x, y, w, h) hauv ntsej muag: cv2.rectangle (img, (x, y), (x+w, y+h), (255, 0, 0), 2) id, conf = rec.predict (grey [y: y+h, x: x+w]) if (id == 2): id = "alok" if id == 1: id = "alok" if id == 3: id = "anjali" if id == 4: id = "Gaurav" if id = = 5: id = 'rahul' if id == 6: id = "akshay" cv2.cv. PutText (cv2.cv.fromarray (img), str (id), (x, y+h), font, 255) cv2.imshow ('img', img) yog cv2.waitKey (1) == ord ('q'): so cap.release ()

cv2.destroyAllWindows ()

thiab thaum kawg qhov tshwm sim yuav los rau ntawm koj lub qhov muag …… koj tseem tuaj yeem rub cov ntawv zip los ntawm hauv qab qhov txuas: Nyem rau qhov no txhawm rau rub cov lej Yog li, hauv cov lus qhia no peb tau ua tiav txoj haujlwm ntawm kev paub lub ntsej muag+paub siv OpenCV….. yog koj zoo li cov lus qhia no….. plzzz subscribe kuv thiab pov npav rau kuv….. Ua tsaug phooj ywg:)

Pom zoo: