PyQt5 making to create a opencv face recognization

I used below the code for face recognization . but iam getting errors.
error is VIDEOIO ERROR: V4L: can’t open camera by index 0.
what is that error and how to solve that error.

import sys
from os import path

import cv2
import numpy as np

from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui
class RecordVideo:
def init(self, camera_port=0): = cv2.VideoCapture(camera_port)
self.running = False

    def run(self):
            self.running = True
            while self.running:
                    read, image =
                    # TODO: detect faces now

class FaceDetection:
def init(self, haar_cascade_filepath):
self.classifier = cv2.CascadeClassifier(haar_cascade_filepath)
self._min_size = (70, 70)

    def detect_faces(self, image):
            # haarclassifiers work better in black and white
            gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            gray_image = cv2.equalizeHist(grey_image)

            faces = self.classifier.detectMultiScale(grey_image, scaleFactor=1.3, minNeighbors=4, flags=cv2.CASCADE_SCALE_IMAGE, min_size=self._min_size)

            # TODO: Paint on a surface and add the faces.

class RecordVideo(QtCore.QObject):
image_data = QtCore.pyqtSignal(np.ndarray)

    def __init__(self, camera_port=0, parent=None):
   = cv2.VideoCapture(camera_port)
            self.timer = QtCore.QBasicTimer()

    def start_recording(self):
            self.timer.start(0, self)

    def timerEvent(self, event):
            if (event.timerId() != self.timer.timerId()):

            read, image =
            if read:

class FaceDetectionWidget(QtWidgets.QWidget):
def init(self, haar_cascade_filepath, parent=None):
self.classifier = cv2.CascadeClassifier(haar_cascade_filepath)
self.image = QtGui.QImage()
self._red = (0, 0, 255)
self._width = 2
self._min_size = (30, 30)

    def detect_faces(self, image: np.ndarray):
            # haarclassifiers work better in black and white
            gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            gray_image = cv2.equalizeHist(gray_image)

            faces = self.classifier.detectMultiScale(gray_image, scaleFactor=1.3, minNeighbors=4, flags=cv2.CASCADE_SCALE_IMAGE, minSize=self._min_size)

            return faces

    def image_data_slot(self, image_data):
            faces = self.detect_faces(image_data)
            for (x, y, w, h) in faces:
                    cv2.rectangle(image_data, (x, y), (x+w, y+h), self._red, self._width)

            self.image = self.get_qimage(image_data)
            if self.image.size() != self.size():


    def get_qimage(self, image: np.ndarray):
            height, width, colors = image.shape
            bytesPerLine = 3 * width
            QImage = QtGui.QImage

            image = QImage(, width, height, bytesPerLine, QImage.Format_RGB888)

            image = image.rgbSwapped()
            return image

    def paintEvent(self, event):
            painter = QtGui.QPainter(self)
            painter.drawImage(0, 0, self.image)
            self.image = QtGui.QImage()

class MainWidget(QtWidgets.QWidget):
def init(self, haarcascade_filepath, parent=None):
fp = haarcascade_filepath
self.face_detection_widget = FaceDetectionWidget(fp)

            # TODO: set video port
            self.record_video = RecordVideo()
            self.run_button = QtWidgets.QPushButton('Start')

            # Connect the image data signal and slot together
            image_data_slot = self.face_detection_widget.image_data_slot
            # connect the run button to the start recording slot

            # Create and set the layout
            layout = QtWidgets.QVBoxLayout()


def main(haar_cascade_filepath):
app = QtWidgets.QApplication(sys.argv)

    main_window = QtWidgets.QMainWindow()
    main_widget = MainWidget(haar_cascade_filepath)

if name == ‘main’:
script_dir = path.dirname(path.realpath(file))
cascade_filepath = path.join(script_dir, ‘…’, ‘data’, ‘haarcascade_frontalface_default.xml’)

    cascade_filepath = path.abspath(cascade_filepath)

Did you get this issue solved? PyQt isn’t finding a video camera, that’s what the error is.

There’s a way to list out all the available cameras.

What has happened to your programming output? Seems like you went AWOL ~Aug. 2018, at the latest. No streaming. No pushes to github. No blogging. Forums (here) are dead. What gives? I’m sort of a self-taught programmer myself, and lately I’ve been frustrated with it all, and I’m starting to lose interest and wonder what the point is. I’m just curious as to how you’re holding up, and whether I should not be deterred.

1 Like

@Kyle_C, a lot happened!
I published two courses through Packt Publishing. One went really well and the other ended up just ok.

I tried blogging, but didn’t put enough time into it to get the results back that I wanted.

I’ve moved to three different locations since I stopped streaming. I’m also working on a computer science masters.

I picked up a job doing software development, which was one of the reasons I started streaming. My last job was in the AI space doing similar things in scope to my streaming. I worked a lot of hours and didn’t have much motivation to livestream anymore.

Most recently, I’ve been putting a lot of effort into CVAT:

Part of my challenge with streaming was I was making things I thought were cool for myself, but I wasn’t sure that other people were going to use them. CVAT is a neat project that will be used by other people, so that’s really cool.

I hope someday to get back and do some streaming or do some teaching of some sort. Streaming never paid the bills, but it did allow me to get into software development which I’m grateful for. That was always the goal. Don’t give up! You probably just haven’t found the right niche yet!

1 Like

That’s how I’ve always felt, except I’ve never streamed.

I did see you were working on CVAT recently.

I will try not to give up. I think I need to get involved in a project or, at least, make some friends in this realm.

Thanks for getting back to me, man; and doing so in a timely manner! Either way (whether I give up or not), good luck with your pursuits.

1 Like