def recognizeVideo(faceRecognizer, videoFileName, subjects, haarFolder): faceCascade = loadCascadeClassifier(haarFolder + "/haarcascade_frontalface_alt2.xml") leftEyeCascade = loadCascadeClassifier(haarFolder + "/haarcascade_lefteye_2splits.xml") rightEyeCascade = loadCascadeClassifier(haarFolder + "/haarcascade_righteye_2splits.xml") if not videoFileName: videoFileName = 0 capture = cv2.VideoCapture(videoFileName) readOk, image = capture.read() if readOk: height, width, channels = image.shape else: logging.warning("Could not read capture!!") return minFaceSize = (int(width * 0.1), int(width * 0.1)) rectColor = (255, 0, 0) rectThickness = 2 fontColor = (255, 255, 255) fontScale = 0.8 fontThickness = 1 title = 'Face Recognizer App' cv2.namedWindow(title) while cv2.waitKey(10) == -1 and readOk: faces = detectFaces(image, faceCascade, leftEyeCascade, rightEyeCascade, minFaceSize) if len(faces) == 0 : for i in xrange(0, 3): cv2.imshow(title, image) _, image = capture.read() else: for (x, y, w, h, _, _) in faces: face = cv2.cvtColor(image[y:y+h, x:x+w], cv2.COLOR_BGR2GRAY) faceGray = cv2.resize(face, (92, 112)) (prediction, distance) = faceRecognizer.predict(faceGray) if distance > 140: predictionLegend = "Unknow subject" else: predictionLegend = "Predicted {0} - Distance {1}".format(subjects[prediction], distance) cv2.rectangle(image, (x,y), (x+w,y+h), rectColor, rectThickness) drawLabel(predictionLegend, image, (x-20, y-10)) cv2.imshow(title, image) readOk, image = capture.read() cv2.destroyWindow(title)
def main(): args = configureArguments() configureLogging(args.log) windowTitle = "Test draw app" cv2.namedWindow(windowTitle) haarFolder = "/home/juan/ciberpunks/opencv-2.4.11/data/haarcascades" faceCascade = loadCascadeClassifier(haarFolder + "/haarcascade_frontalface_alt2.xml") leftEyeCascade = loadCascadeClassifier(haarFolder + "/haarcascade_lefteye_2splits.xml") rightEyeCascade = loadCascadeClassifier(haarFolder + "/haarcascade_righteye_2splits.xml") mouthCascade = loadCascadeClassifier(haarFolder + '/haarcascade_mcs_mouth.xml') color = (120,120,130) thickness = 2 width = 600 image = cv2.imread('/home/juan/ciberpunks/faces/news/[email protected]') image = cv2.resize(image, calculateScaledSize(width, image=image)) if image is None: print 'ERROR: no se pudo leer la imagen.' return minFaceSize = (10, 10) minEyeSize = (5, 5) faces = detectFaces(image, faceCascade, leftEyeCascade, rightEyeCascade, minFaceSize, minEyeSize) for (x, y, w, h, leftEyes, rightEyes) in faces: center = calculateCenter((x,y,w,h)) cv2.line(image, (x,0), (x, width), color, 2) cv2.line(image, (x+w,0), (x+w, width), color, 2) cv2.line(image, (0,y), (width, y), color, 2) cv2.line(image, (0,y+h), (width, y+h), color, 2) drawLabel("Juan Gabriel", image, (x, y+20)) cv2.imshow(windowTitle, image) cv2.waitKey(6000) cv2.destroyWindow(windowTitle)