In [1]:
import cv2
import numpy as np
import scipy.misc
import os
import time
os.chdir("//home//mckc//Imagedb/")
import uuid


face_cascade = cv2.CascadeClassifier('/home/mckc/Downloads/opencv-2.4.13/data/haarcascades_GPU/haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)

In [10]:
while True:
#   Capture frame-by-frame
    time.sleep(1)
    video_capture.open(0)
    ret, frame = video_capture.read()
    print frame.shape
    cv2.imshow('Video',frame)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)
    start_time = time.time()
    faces = face_cascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
       flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )
    print(time.time()-start_time)

    # Draw a rectangle around the faces
    if len(faces)>0:    
        for (x, y, w, h) in faces:
            fac = np.array(frame)[y:(y+h),x:(x+h),:]            
            cv2.rectangle(frame, (x, y), (x+h, y+w), (0, 255, 0), 2)
            cv2.putText(frame,'omar',(x,y+h), cv2.FONT_HERSHEY_DUPLEX,1,(0,0,255), 2,8)            
            scipy.misc.toimage(cv2.cvtColor(fac, cv2.COLOR_RGB2BGR)).save(str(uuid.uuid4()) +'.jpg')

    # Display the resulting frame
    cv2.imshow('Video', frame)
#        fac = np.array(frame)[y:(y+h),x:(x+h)]
    if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        


# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-8cc95124c7e0> in <module>()
      4     video_capture.open(0)
      5     ret, frame = video_capture.read()
----> 6     print frame.shape
      7     cv2.imshow('Video',frame)
      8     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

AttributeError: 'NoneType' object has no attribute 'shape'

In [13]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(fac)


Out[13]:
<matplotlib.image.AxesImage at 0x7f5bcdd61950>

In [17]:
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
plt.imshow(gray)


Out[17]:
<matplotlib.image.AxesImage at 0x7f5bcb2c0d50>

In [ ]: