ITCoders / Human-detection-and-Tracking

Human-detection-and-Tracking
Apache License 2.0
850 stars 307 forks source link

Running the code #19

Closed ArshadAliDev closed 7 years ago

ArshadAliDev commented 7 years ago

I am trying to recognize faces from a.mp4 file. I already have some sample images of faces in my database. When I run the program, I want it to recognize the faces from the video. The location of video file is passed as a command line argument sys.argv[1].

code :

import cv2
import numpy as np
import os
import sqlite3
import pickle
from PIL import Image
import sys

faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml');

rec = cv2.createLBPHFaceRecognizer();

rec.load("recognizer\\trainningData.yml")
path ='dataSet'

font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX,1,1,0,2,2)
font2=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX,1,1,0,1,1)
def getProfile(id):
    conn=sqlite3.connect("FaceBase.db")
    cmd="SELECT * FROM People WHERE ID="+str(id)
    cursor = conn.execute(cmd)
    profile=None
    for row in cursor:
        profile=row
    conn.close()
    return profile

cam = cv2.VideoCapture(sys.argv[1])

while cam.isOpened():

    ret,img = cam.read()

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    faces = faceDetect.detectMultiScale(gray,1.3,5);
    for(x,y,w,h) in faces :
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        id,conf=rec.predict(gray[y:y+h,x:x+w])

        if conf<60   :
                        profile=getProfile(id)
            if(profile!=None):
                cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[1]),(x,y+h+30),font,255);
                cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[2]),(x,y+h+60),font,255);
                cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[3]),(x,y+h+90),font,255);
                print("{} is Correctly Recognized with confidence {}".format(id,conf))

        else :
             cv2.cv.PutText(cv2.cv.fromarray(img),'unkown',(x,y+h+30),font,255);

    cv2.imshow("Face",img);
    if (cv2.waitKey(1) == ord('q')):
        break;
cam.release()
cv2.destroyAllWindows()

The command in command prompt is

C:\FaceRocogProject\Face Recog sqlite\python Recognize_video.py 'C:\FaceRocogProject\Face Recog sqlite\test.mp4'

prsntmaurya commented 7 years ago

Can you elaborate your problem? Are u getting any error message? Please mention it so that it will be easier to understand your problem.

arpit1997 commented 7 years ago

@codeArshad This is debugging problem. Please mention what you have tried so far. Here are couple of things you can try to debug this code.

Instead of this:


cv2.imshow("Face",img);
    if (cv2.waitKey(1) == ord('q')):
        break;
ArshadAliDev commented 7 years ago

It worked after copying opencv_ffmpeg2411.dll file to python27 directory.

source : http://stackoverflow.com/questions/35242735/can-not-read-or-play-a-video-in-opencvpython-using-videocapture

Thank you @prsntmaurya and @arpit1997