Mundarija:

Opencv yuzini aniqlash, o'qitish va tanib olish: 3 qadam
Opencv yuzini aniqlash, o'qitish va tanib olish: 3 qadam

Video: Opencv yuzini aniqlash, o'qitish va tanib olish: 3 qadam

Video: Opencv yuzini aniqlash, o'qitish va tanib olish: 3 qadam
Video: Uchburchak yuzini topish | Yuza va perimetr | Geometriya asoslari 2024, Noyabr
Anonim
Opencv yuzini aniqlash, o'qitish va tanib olish
Opencv yuzini aniqlash, o'qitish va tanib olish

OpenCV-bu ochiq manbali kompyuter ko'rish kutubxonasi bo'lib, u tasvirni qayta ishlashning asosiy vazifalarini, masalan, xiralashtirish, tasvirni aralashtirish, tasvirni yaxshilash, shuningdek, video sifatini, pol qiymatini va hokazolarni bajarish uchun juda mashhur bo'lib, tasvirni qayta ishlashga qo'shimcha ravishda, oldindan tayyorlangan har xil chuqur o'rganishni ta'minlaydi. oddiy vazifalarni hal qilish uchun to'g'ridan -to'g'ri ishlatilishi mumkin bo'lgan modellar.

opencv o'rnatish uchun ushbu havoladan foydalaning

www.instructables.com/id/Opencv-and-Python…

1 -qadam: Videoda yuzni aniqlash

siz google -dan yuzni aniqlash dasturlarini qidirishingiz mumkin va aniqlangan yuzlar rasmlarni qayta ishlash uchun papkada saqlanishi kerak. biz 30 ta namunani yig'amiz

cv2 import qilish

numpy ni np sifatida import qilish

import qilish tizimi

kamera = cv2. VideoCapture (0)

faceCascade = cv2. CascadeClassifier ("haarcascade_frontalface_default.xml") #harcascade fayl yo'lini qo'shing

name = raw_input ("Uning ismi nima?")

#barcha fayllar Foydalanuvchilar/prasad/Hujjatlar/rasmlar papkasida saqlanadi

dirName = "/Users/prasad/Documents/images/" + name

chop etish (dirName) bo'lmasa, os.path.exists (dirName): os.makedirs (dirName) chop etish ("Katalog yaratildi") boshqa: chop etish ("Ism allaqachon mavjud") sys.exit ()

hisoblash = 1

#biz 30 ta namunani yig'amiz

30 sonini hisoblashda: break # frame = frame.array grey = cv2.cvtColor (frame, cv2. COLOR_BGR2GRAY) yuzlar = faceCascade.detectMultiScale (kulrang, 1,5, 5) yuzlarida (x, y, w, h): roiGray = kulrang [y: y + h, x: x + w] fileName = dirName + "/" + name + str (count) + ".jpg" cv2.imwrite (fileName, roiGray) cv2.imshow ("yuz", roiGray)) cv2.rectangle (ramka, (x, y), (x+w, y+h), (0, 255, 0), 2) hisoblash+= 1 cv2.imshow ('ramka', ramka) kalit = cv2.kutish (1)

agar kalit == 27:

tanaffus

#kamera.release ()

cv2.destroyAllWindows ()

2 -qadam: Namuna rasmlaringizni o'rgating

Yuzni aniqlash tugallangandan so'ng, biz tasvirlarni o'rgatishimiz mumkin

osimport numpy ni PIL importidan np sifatida import Rasm import cv2 import tuzlash #import serial

#ser = serial. Serial ('/dev/ttyACM0', 9600, vaqt tugashi = 1)

faceCascade = cv2. CascadeClassifier ("haarcascade_frontalface_default.xml")

tanuvchi = cv2.face. LBPHFaceRecognizer_create ()

baseDir = os.path.dirname (os.path.abspath (_ fayl_))

#rasmlar papkasida rasmlarni o'rgating

imageDir = os.path.join (baseDir, "tasvirlar")

currentId = 1

labelIds = {} yLabels = xTrain = #ser.write ("Trening …..". encode ())

os.walk -dagi root, dirs, fayllar uchun (imageDir):

fayllardagi fayl uchun chop etish (root, dirs, files): print (file) if file.endswith ("png") or file.endswith ("jpg"): path = os.path.join (root, file) label = os.path.basename (root) chop etish (yorliq)

agar labelIds -da yozilmagan bo'lsa:

labelIds [label] = currentId chop etish (labelIds) currentId += 1

id_ = labelIds [label]

pilImage = Image.open (path).convert ("L") imageArray = np.array (pilImage, "uint8") yuzlar = faceCascade.detectMultiScale (imageArray, scaleFactor = 1.1, minNeighbors = 5)

yuzlarida (x, y, w, h) uchun:

roi = imageArray [y: y+h, x: x+w] xTrain.append (roi) yLabels.append (id_)

ochiq ("teglar", "wb") bilan f kabi:

pickle.dump (labelIds, f) f.close ()

tanınuvchi.train (xTrain, np.array (yLabels))

tanınuvchi.save ("trainer.yml") chop etish (labelIds)

3 -qadam: yuzlarni aniqlash

Trening tugagandan so'ng, siz quyidagi kodni ishlatishingiz mumkin, shunda u sizning o'qitilgan yuzlaringizni taniy boshlaydi

import osos.environ ['PYTHONINSPECT'] = 'on' import cv2 import numpy sifatida np import tuzlangan vaqt

ochiq ('teglar', 'rb') bilan f:

dicti = pickle.load (f) f.close ()

kamera = cv2. VideoCapture (0)

faceCascade = cv2. CascadeClassifier ("haarcascade_frontalface_default.xml")

tanib oluvchi = cv2.face. LBPHFaceRecognizer_create () tanib oluvchi.read ("trainer.yml")

shrift = cv2. FONT_HERSHEY_SIMPLEX

oxirgi = ''

#kamera ichidagi ramka uchun.capture_continuous (rawCapture, format = "bgr", use_video_port = True):

while True: ret, frame = camera.read () kulrang = cv2.cvtColor (ramka, cv2. COLOR_BGR2GRAY) yuzlar = faceCascade.detectMultiScale (kulrang, scaleFactor = 1.5, minNeighbors = 5) yuzlarida: roiGray = kulrang [y: y+h, x: x+w]

id_, conf = tanib oluvchi.predict (roiGray)

ism uchun, dicti.items () da qiymat:

if value == id_: print (name) cv2.putText (frame, name, (x, y), shrift, 2, (0, 0, 255), 2, cv2. LINE_AA) if name! = last: last = konf <= 70 bo'lsa, ism

cv2.imshow ('ramka', ramka)

kalit = cv2.waitKey (1)

agar kalit == 27:

cv2.destroyAllWindows -ni buzish ()

Tavsiya: