H@lp 与 OpenCV python

问题描述 投票:0回答:1
import numpy as np
import cv2


def initialize_face():
    cascade = '/Users/gadziaskarov/Desktop/py.projects/script_SO2/main.xml'

    clf = cv2.CascadeClassifier(cascade)
    camera = cv2.VideoCapture('video.mp4')

    while True:
        _, frame = camera.read()

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = clf.detectMultiScale(

            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(30,30),
            flags=cv2.CASCADE_SCALE_IMAGE
            
        )
        for (x,y, width, height) in faces:
            cv2.rectangle(frame, (x, y), (x + width, y + height), (255, 255, 0), 2)
        
        cv2.imshow('Faces', 'frame')

        if cv2.waitKey(1) == ord('q'):
            break

    camera.release()
    cv2.destroyAllWindows()
        

def main():
    initialize_face()

if __name__ == '__main__':
    main()

错误看起来像这样: cv2.error:OpenCV(4.9.0):-1:错误:(-5:错误参数)在函数“imshow”中

重载解析失败:

  • mat 不是 numpy 数组,也不是标量
  • 参数 'mat' 的预期 Ptr
  • 参数 'mat' 的预期 Ptr

我什至不知道该怎么办,所以...

python opencv
1个回答
0
投票

您正在传递字符串而不是图像作为参数。

        cv2.imshow('Faces', 'frame')

        if cv2.waitKey(1) == ord('q'):
            break

删除“frame”周围的引号:

        cv2.imshow('Faces', frame)

        if cv2.waitKey(1) == ord('q'):
            break
© www.soinside.com 2019 - 2024. All rights reserved.