Face Detection OpenCV and PyQt


Let’s install some stuff.

Let’s import some stuff.

Now we’ll build this backwards, starting with the smallest pieces and working bigger. Let’s start working on creating the video recording and getting the face coordinates out.

This is a companion discussion topic for the original entry at http://benhoff.net/face-detection-opencv-pyqt.html


I had to replace the .so lib installed by opencv-python with the one installed by my package manager. The lib should be in venv/lib/python3.X/site-packages/cv2 if you’re using a virtual environment.


Hey there! This code works fine in my laptop. Btw, can you help me about changing the video capture parameter?
I would like to use raspberry pi camera.
I am able to detect faces using my pi camera and opencv. What challenges me is putting pi camera video stream into a gui like what you did.


What do you want to change about the video capture parameter? Just to a different camera?

You need to edit the code either here or put in some type of selection criteria. If you’re a python programmer, you could put a QComboBox tied to a slot that reinitialized the cv2.VideoCapture(camera_port) depending on the integer value that gets passed in.


It’s working now in a raspberry pi camera. Thank you
The thing is converting every frames into numpy array. Can you explain to me what is the difference when I will call the start video function in a setupUi function that is genereted by qt designer? For example:
class Show_Video(…):
def startvideofunction(…):
class ImageViewer(…):
def functionA(…):

def functionZ(…):

class My_UI_MainWindow(object):
def setupUi(self, MainWindow):
self.pushButton = QtWidgets.QPushButton(self.centralwidget)


Hi. i have a question. i always get syntax error in def(self, image: np.ndarray). It says “image:” syntax error. Thank you


Are you running python 3.6? If not, the typing annotations will cause a syntax error.


okay thanks. yes you re right need to run it on python 3.6. Its working thanks.


have you tried creating a UI first in Qt then converting it to .py then use it to display the video?. Thanks


No because I’ve never figured out how to import custom widgets into qt Designer. I think the last time I looked at how to do it, I thought it was too much work.

Not to say it can’t be done, just wasn’t worth it to me.


Ok Thanks. I have tried it just now and its working. Importing the video on a GUi made using Qt designer.