OpenCV:利用命令行进行人脸检测

时间:2015-05-22 09:37:59

标签: python opencv command-line openbr

我运行此(第一个)example,启动我的镭拓的网络摄像头,以便我可以在屏幕上看到自己。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

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

我在Ubuntu 14.04 LTS上安装了OpenBr,并在我自己的照片上成功运行this命令:

br - gui -algorithm ShowFaceDetection -enrollAll -enroll /home/nakkini/Desktop/myself.png

我在终端上运行的上述命令显示我的照片并在我的脸部周围绘制一个正方形(面部检测),它还以绿色突出我的眼睛。

我的梦想:

我想知道是否有办法将此命令与上面的短程序结合使用,以便在启动网络摄像头时我可以看到我的脸被绿色矩形包围?

我为什么需要这个?

我为纯粹的OpenCV / Python找到了类似的程序。然而,为了以后的需要,我需要更多的东西,而不是简单的面部检测,我自己判断OpenBR将为我节省很多头痛。这就是为什么我正在寻找一种方法在上面的代码中的某个地方运行命令行作为第一步但又是一大步。

提示:

代码中的frame对应于命令行的myself.png。找到的解决方案将尝试将frame替换为myself.png到程序本身的命令行。

非常感谢你。

修改

在纠正@ Xavier解决方案的错误后,我没有错误。但是程序没有按我的意愿运行:

首先,相机启动,我看到自己,但我的脸没有检测到绿色矩形。其次,我按任意键退出,但程序没有退出:它显示我自己的照片,检测到我的脸。该计划存在最后一次按键。我的目标是在相机功能期间检测到我的脸部。

2 个答案:

答案 0 :(得分:2)

你根本不需要openbr。

只需查看opencv' python face-detect tutorial

答案 1 :(得分:1)

这样的事情应该起作用

import numpy as np
import cv2
import os

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.imwrite( "/home/nakkini/Desktop/myself.png", gray );
        os.system('br - gui -algorithm -ShowFaceDetection -enrollAll -enroll /home/nakkini/Desktop/myself.png')
        break

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