opencv2 imshow在级联detectMultiScale后不更新

0 投票
1 回答
632 浏览
提问于 2025-04-17 20:59

我是OpenCV的新手,跟着一些网上的文章学了一些东西,然后根据自己的理解做了一些修改,写出了这段代码:

import cv2, time

cap = cv2.VideoCapture(0)
time.sleep(1)
cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")

def detect(image):
    faces = cascade.detectMultiScale(image)
.
    for _face in faces:
        cv2.rectangle(image, (_face[0], _face[1]), (_face[0]+_face[2], _face[1]+_face[3]), (255,255,255))

def repeat():
    ret, image = cap.read()
    detect(image)
    cv2.imshow("w1", image)


while True:
    repeat()

问题是,如果我使用detect这个方法,窗口就不会更新图像流。没有看到图像流,我就没法继续在脸上画矩形。

1 个回答

1

在这里加一个:

cv2.waitKey(5)

在这个之后

cv2.imshow()

否则你的窗口就不会更新了

撰写回答