OpenCV + Python自动采样

时间:2014-12-22 09:58:57

标签: python opencv real-time face-recognition

我想从网络摄像头拍摄一些脸部图像样本。我想拍约8个样本。因此,当我按下按钮" s"时,将需要8个样本。所以我尝试像这样循环:

faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)

for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)


if cv2.waitKey(1) & 0xFF == ord('s'):
ROI = frame[y:y+h,x:x+w]
nameUser = raw_input("Input yourname: ")
for i in range(0,8):
    i_string = str(i)
    cv2.imwrite(nameUser+i_string+'.jpg',ROI)
    time.sleep(3)

但是我得到的8张图像完全相同。有一种方法可以使程序采样样面但不完全相同吗?

1 个答案:

答案 0 :(得分:1)

我认为,这只是一个逻辑问题,试图重新组织它:

recording = 0 # use it as a flag/counter

while(True):
    ok, frame= cap.read();
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY);

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)


    if cv2.waitKey(1) & 0xFF == ord('s'):
        recording = 8
        nameUser = raw_input("Input yourname: ")

    if recording >= 0 && len(faces)>0:
        ROI = frame[y:y+h,x:x+w]
        i_string = str(recording)
        cv2.imwrite(nameUser+i_string+'.jpg',ROI)
        recording -= 1
        time.sleep(3)