folder Tahribat.com Forumları
linefolder Python
linefolder Python Opencv Yüz Ve Mimik Algılama



Python Opencv Yüz Ve Mimik Algılama

  1. KısayolKısayol reportŞikayet pmÖzel Mesaj
    cnr437
    cnr437's avatar
    Banlanmış Üye
    Kayıt Tarihi: 03/Nisan/2007
    Erkek

    Python ile OpenCV kütüphanesini kullanarak yüz bulma ve bulunan yüz üzerinde basitçe ağız, göz ve kaş hareketlerini izleme, ağızda, gözde ve kaşta oluşan değişimleri belirli değerler vererek döndürmektedir. Çok basitçe kısa bi zaman içerisinde yazdığım için program ilk çalıştığında bi kalibrasyon gerektirir, ilgilenenlerin işine yarayabilir.


    #!/usr/bin/python

    import os, sys
    import cv2.cv as cv
    import random

    cv.NamedWindow("FaceWindow", 1)        # OpenCV icin Pencere aciliyor.

    capture = cv.CreateCameraCapture(0)    # Kameradan kayit aliniyor.

    cascade_face = cv.Load("haarcascades\\haarcascade_frontalface_alt.xml")
    cascade_eyepair = cv.Load("haarcascades\\haarcascade_eye.xml")
    cascade_mouth = cv.Load("haarcascades\\haarcascade_mcs_mouth.xml")

    storage = cv.CreateMemStorage (0)

    image_scale = 1

    def detect(img):
        ft1 = mt1 = (0,0)
        by = 30
        gray = cv.CreateImage((img.width,img.height), 8, 1)
        cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
        cv.EqualizeHist(gray, gray)
        faces = cv.HaarDetectObjects(gray, cascade_face, storage,
                                     1.1, 2, 1, (50, 50))

        width = 300
        height = 300
        rp1 = ((img.width-width)/2, (img.height-height)/2)
        rp2 = ((img.width-width)/2+width, (img.height-height)/2+height)
        cv.Rectangle(img, rp1,rp2, cv.RGB(255, 255, 0), 2, 8, 0)

        if faces:
            ((x, y, w, h), n) = faces[0]
            ft1 = (x, y)
            ft2 = (x + w, y + h)
            color = cv.RGB(255, 0, 0)
            if rp1[0] < ft1[0] and rp1[1] < ft1[1] and rp2[0] > ft2[0] and rp2[1] > ft2[1]:
                    color = cv.RGB(0, 255, 0)
            cv.Rectangle(img, ft1, ft2, color, 2, 8, 0)

            eyeroi = (x,int(y+(h/5.5)),w,int(h/3.0))
            mouthroi = (x,int(y+(h/1.5)),w,int(h/2.5))
            browroi = (x,int(y+(h/7.0)),w,int(h/6.0))
            cv.SetImageROI(gray, eyeroi)
            

            eyes = cv.HaarDetectObjects(gray, cascade_eyepair, storage,
                                     1.15, 3, 0, (25, 15))

            cv.SetImageROI(gray, browroi)
            cnt_img = cv.CreateImage((gray.width,gray.height), 8, 1)
            
            contours = cv.FindContours(gray,
                                    storage,
                                    cv.CV_RETR_LIST,
                                    cv.CV_CHAIN_APPROX_SIMPLE,
                                    (0,0))

            y = [y for (x,y) in contours]
            by = max(y)
            if contours:
                cv.DrawContours (gray, contours,
                                (255,0,0), (0,255,0),
                                0, 1, cv.CV_AA,
                                (0, 0))
                            
            #cv.ShowImage("ContourWindow", gray)    # Resim Pencere icinde gosteriliyor.

            cv.SetImageROI(gray, mouthroi)
            mouths = cv.HaarDetectObjects(gray, cascade_mouth, storage,
                                     1.15, 3, 0, (30, 30))
            cv.ResetImageROI(gray)
                                    
            if eyes:
                for ((x, y, w, h), n) in eyes:
                    pt1 = (x + eyeroi[0], y + eyeroi[1])
                    pt2 = (x + w + eyeroi[0], y + h + eyeroi[1])
                    cv.Rectangle(img, pt1, pt2, cv.RGB(0, 255, 0), 2, 8, 0)
                                    
            if mouths:
                for ((x, y, w, h), n) in mouths:
                    mt1 = (x + mouthroi[0], y + mouthroi[1])
                    mt2 = (x + w + mouthroi[0], y + h + mouthroi[1])
                    cv.Rectangle(img, mt1, mt2, cv.RGB(0, 255, 0), 2, 8, 0)
        return ft1, mt1, by

    if __name__ == "__main__":
        counter = 25
        fth = 0
        mth = 0
        oldb = 0
        sumval = 0
        aval = 0
        f = 5
        while True:
            img = cv.QueryFrame(capture)    # Kayittan her bir kare icin resim olusturuluyor.
            font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 2, 3)
            face,mouth,brow = detect(img)
            if counter > 0:
                cv.PutText(img,"Get your position for calibrate. %d" % (counter), (0,25),font, (0,255,0))
                counter -= 1
                fth = face[1]
                mth = mouth[1]
                oldb = brow
            else:
                counter = 0
                if (mouth[1]-mth > face[1]-fth):
                    val = (mouth[1]-mth)-(face[1]-fth)
                    sumval += val
                    f -= 1
                    if f == 0:
                        f = 5
                        aval = sumval/f
                        sumval = 0
                    cv.PutText(img,"Rank of mouth: %d" % (aval/5), (0,25), font, (0,0,150))

                if abs(brow - oldb) > 7:
                    cv.PutText(img,"Brow motion detected.", (0,50), font, (0,0,200))
                    
            cv.ShowImage("FaceWindow", img)    # Resim Pencere icinde gosteriliyor.
            if cv.WaitKey(10) == 27:        # ESC tusunu bekliyor.
                break


    windowsta derlemedim hiç, source olarak bu şekilde duruyor, haarcascade'leri opencv'nin sample klasörü altında bulabilirsiniz.


    Bizim olduğumuz her yerde herşey bizim yüzümüzden olmuştur. Ben benim amk bana bişey olmasın!
  2. KısayolKısayol reportŞikayet pmÖzel Mesaj
    All hail to Tux
    sandman
    sandman's avatar
    Kayıt Tarihi: 01/Eylül/2005
    Erkek

    ilgilenen arkadaşlar haartraning'i araştırsınlar bunun sayesinde aynen nasıl yüz tanıyorsa aynı şekilde objede tanımayı ögretebiliyorsunuz.


    Mühendis kahveyi projeye dönüştüren bir insan evladıdır.
  3. KısayolKısayol reportŞikayet pmÖzel Mesaj
    CAgAtAy
    CAgAtAy's avatar
    Kayıt Tarihi: 15/Haziran/2008
    Erkek

    Eyvallah hocam ellerine sağlık


    Lider doğru işleri yapar; Yönetici işleri doğru yapar;
Toplam Hit: 1813 Toplam Mesaj: 3