# -*- coding: utf-8 -*-
import cv2
import numpy as np
import os

cv2.namedWindow("-------------------------------------人脸识别窗口--------------------------------")
cap=cv2.VideoCapture(0)
success, frame = cap.read()
color = (255,0,255)
classfier=cv2.CascadeClassifier("/usr/lib/python2.7/dist-packages/haarcascade_frontalface_alt.xml")
#classfier=cv2.CascadeClassifier("/home/pi/opencv-2.4.10/deta/haarcascades/haarcascade_frontalface_alt.xml")
#classfier=cv2.CascadeClassifier("/usr/lib/python2.7/dist-packages/haarcascade_fullbody.xml")
ada=0
while success:
    success, frame = cap.read()
    size=frame.shape[:2]
    image=np.zeros(size,dtype=np.float16)
    image = cv2.cvtColor(frame, cv2.cv.CV_BGR2GRAY)
    cv2.equalizeHist(image, image)
    divisor=8
    h, w = size
    minSize=(w/divisor, h/divisor)
    faceRects = classfier.detectMultiScale(image, 1.2, 2, cv2.CASCADE_SCALE_IMAGE,minSize)
    
    if len(faceRects)>0:
        ada=ada+1
        
        print("检测成功%d次"%(ada))
        os.system('./home/pi/webcam/face-recognition/prepare-data/recognition-verify.sh')
        #c='q'
        if ada==4:
            c='Q'
        for faceRect in faceRects:
                x, y, w, h = faceRect
                cv2.rectangle(frame, (x, y), (x+w, y+h), color)
                print(x ,y)
                if(x<=250):
                    
                    if(y>=135):#xia
                      
                       print("左下转") 
                      
                    if(y<135):#shang
                     
                        print("左上转")
                     
                    
                if(x>250):   #you
                 
                     if(y>=135):#xia
                      
                       print("右下转") 
                      
                     if(y<135):#shang
                    
                        print("右上转")
                      
                 

    cv2.imshow("------------------------------人脸识别窗口------------------------------", frame)
    key=cv2.waitKey(10)
    c = chr(key & 255)
    print ("检测成功")
    if c in ['q', 'Q', chr(27)]:
       print ("退出检测") 
       break
   
#cv2.destroyWindow("-----------------------------人脸识别窗口------------------------------")

