代码之家  ›  专栏  ›  技术社区  ›  ironmantis7x

src不是numpy数组或scaler-python人脸检测

  •  0
  • ironmantis7x  · 技术社区  · 6 年前

    但是,显示人脸检测和情感识别的帧没有显示出来。我在控制台中遇到以下错误:

    灰色=cv2.CVT颜色(边框,cv2.COLOR\u BGR2GRAY) TypeError:src不是numpy数组,也不是标量

    from PyQt4 import QtCore, QtGui, uic
    import sys
    import cv2
    import numpy as np
    import threading
    import time
    import Queue
    import imutils
    
    from keras.preprocessing.image import img_to_array
    from keras.models import load_model
    
    # parameters for loading data and images
    detection_model_path = '/xxxxxxxx/haarcascade_files/haarcascade_frontalface_default.xml'
    emotion_model_path = '/xxxxxxxx//models/_mini_XCEPTION.102-0.66.hdf5'
    
    # hyper-parameters for bounding boxes shape
    # loading models
    face_detection = cv2.CascadeClassifier(detection_model_path)
    emotion_classifier = load_model(emotion_model_path, compile=False)
    EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised",
     "neutral"]
    
    
    running = False
    capture_thread = None
    form_class = uic.loadUiType("simple.ui")[0]
    q = Queue.Queue()
    
    
    def grab(cam, queue, width, height, fps):
        global running
        capture = cv2.VideoCapture(cam)
        capture.set(cv2.CAP_PROP_FRAME_WIDTH, width)
        capture.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
        capture.set(cv2.CAP_PROP_FPS, fps)
    
        while(running):
            frame = {}        
            capture.grab()
            retval, img = capture.retrieve(0)
            frame["img"] = img
    
            if queue.qsize() < 10:
                queue.put(frame)
            else:
                print queue.qsize()
    
    class OwnImageWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            super(OwnImageWidget, self).__init__(parent)
            self.image = None
    
        def setImage(self, image):
            self.image = image
            sz = image.size()
            self.setMinimumSize(sz)
            self.update()
    
        def paintEvent(self, event):
            qp = QtGui.QPainter()
            qp.begin(self)
            if self.image:
                qp.drawImage(QtCore.QPoint(0, 0), self.image)
            qp.end()
    
    
    
    class MyWindowClass(QtGui.QMainWindow, form_class):
        def __init__(self, parent=None):
            QtGui.QMainWindow.__init__(self, parent)
            self.setupUi(self)
    
            self.startButton.clicked.connect(self.start_clicked)
    
            self.window_width = self.ImgWidget.frameSize().width()
            self.window_height = self.ImgWidget.frameSize().height()
            self.ImgWidget = OwnImageWidget(self.ImgWidget)       
    
            self.timer = QtCore.QTimer(self)
            self.timer.timeout.connect(self.update_frame)
            self.timer.start(1)
    
    
        def start_clicked(self):
            global running
            running = True
            capture_thread.start()
            self.startButton.setEnabled(False)
            self.startButton.setText('Starting...')
    
    
        def update_frame(self):
            if not q.empty():
                self.startButton.setText('Camera is live')
                frame = q.get()
                img = frame["img"]
    
                img_height, img_width, img_colors = img.shape
                scale_w = float(self.window_width) / float(img_width)
                scale_h = float(self.window_height) / float(img_height)
                scale = min([scale_w, scale_h])
    
                if scale == 0:
                    scale = 1
    
                img = cv2.resize(img, None, fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC)
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                height, width, bpc = img.shape
                bpl = bpc * width
                image = QtGui.QImage(img.data, width, height, bpl, QtGui.QImage.Format_RGB888)
                self.ImgWidget.setImage(image)
    
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)
    
                canvas = np.zeros((250, 300, 3), dtype="uint8")
                frameClone = frame.copy()
                if len(faces) > 0:
                    faces = sorted(faces, reverse=True,
                    key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
                    (fX, fY, fW, fH) = faces
                                # Extract the ROI of the face from the grayscale image, resize it to a fixed 28x28 pixels, and then prepare
                        # the ROI for classification via the CNN
                    roi = gray[fY:fY + fH, fX:fX + fW]
                    roi = cv2.resize(roi, (64, 64))
                    roi = roi.astype("float") / 255.0
                    roi = img_to_array(roi)
                    roi = np.expand_dims(roi, axis=0)
    
    
                    preds = emotion_classifier.predict(roi)[0]
                    emotion_probability = np.max(preds)
                    label = EMOTIONS[preds.argmax()]
    
    
                    for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
                            # construct the label text
                            text = "{}: {:.2f}%".format(emotion, prob * 100)
    
                            # draw the label + probability bar on the canvas
                           # emoji_face = feelings_faces[np.argmax(preds)]
    
    
                            w = int(prob * 300)
                            cv2.rectangle(canvas, (7, (i * 35) + 5),
                            (w, (i * 35) + 35), (0, 0, 255), -1)
                            cv2.putText(canvas, text, (10, (i * 35) + 23),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.45,
                            (255, 255, 255), 2)
                            cv2.putText(frameClone, label, (fX, fY - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
                            cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH),
                                          (0, 0, 255), 2)
    
        def closeEvent(self, event):
            global running
            running = False
    
    
    
    capture_thread = threading.Thread(target=grab, args = (0, q, 1920, 1080, 30))
    
    app = QtGui.QApplication(sys.argv)
    w = MyWindowClass(None)
    w.setWindowTitle('window #1')
    w.show()
    app.exec_()
    

    我哪里出错了?

    1 回复  |  直到 5 年前
        1
  •  1
  •   tel    6 年前

    src 第一个论点是什么 cvtColor ,根据 cv2.cvtColor docs . CVT颜色 期望 成为

    参数:

    所以问题是 这些都不是。据我从你的代码中可以看出, frame 好像是一本字典。如果你把电话改成 致:

    gray = cv2.cvtColor(frame['img'], cv2.COLOR_BGR2GRAY)
    

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    

    然后你的代码(或者至少是它的一部分)应该像你期望的那样工作。