opencv如何在屏幕录像机上使用级联

时间:2019-01-15 08:09:16

标签: python opencv machine-learning computer-vision bots

因此,我是opencv的新手,在练习了一些面部检测器并了解了如何使用该库之后,我创建了自己的级联,应该可以识别计算机上的图标(例如徽标和其他图标)。 首先,要确保我的级联工作正常,我写了一个,从我拍摄的图像中检测出图标。我拍摄了一个屏幕截图,并通过级联将其作为图像进行处理,并且效果很好。的代码是

import numpy as np
import cv2
img = cv2.imread('body.jpg')

face_csc = cv2.CascadeClassifier('new_cascade.xml')

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

faces = face_csc.detectMultiScale(gray, 1.1 , 4)

for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)

cv2.imshow('img',img)
cv2.waitKey(0)

一段时间后,我为此编写了它,以呈现屏幕的同时检测图标,就像在屏幕截图上尝试时一样:

import numpy as np
import cv2
from PIL import ImageGrab

fourcc = cv2.VideoWriter_fourcc(*'XVID')

face_csc = cv2.CascadeClassifier('new_cascade.xml')

out = cv2.VideoWriter("test_output.avi", fourcc, 5.0, (1366, 768))

while True:

    img = ImageGrab.grab(bbox=(100, 10, 750, 750))
    # convert image to numpy array
    img_np = np.array(img)
    # convert color space from BGR to RGB
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
    # show image on OpenCV frame
    faces = face_csc.detectMultiScale(frame, 1.1 , 4)
    cv2.imshow("stream", frame)
    # write frame to video writer
    out.write(frame)
    for (x,y,w,h) in faces:
        cv2.rectangle(frame,(x,y),(x+w,y+h), (255,0,0), 2)
        roi_gray = frame[y:y+h, x:x+w]
        roi_color = img_np[y:y+h,x:x+w]
        if cv2.waitKey(1) == 27:
            break
cv2.waitKey(0)
out.release()

但是在运行代码时,它没有显示任何错误,但是也没有检测到或识别出它只是记录我的屏幕的任何图标,我已经尝试调试了好几个小时了,但是没有任何想法?

1 个答案:

答案 0 :(得分:0)

您应该在绘制矩形之后而不是之前显示和编写视频。

import numpy as np
import cv2
from PIL import ImageGrab

fourcc = cv2.VideoWriter_fourcc(*'XVID')

face_csc = cv2.CascadeClassifier('new_cascade.xml')

out = cv2.VideoWriter("test_output.avi", fourcc, 5.0, (1366, 768))

while True:

    img = ImageGrab.grab(bbox=(100, 10, 750, 750))
    # convert image to numpy array
    img_np = np.array(img)
    # convert color space from BGR to RGB
    frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
    # show image on OpenCV frame
    faces = face_csc.detectMultiScale(frame, 1.1 , 4)

    for (x,y,w,h) in faces:
        cv2.rectangle(frame,(x,y),(x+w,y+h), (255,0,0), 2)
        roi_gray = frame[y:y+h, x:x+w]
        roi_color = img_np[y:y+h,x:x+w]
        if cv2.waitKey(1) == 27:
            break

    cv2.imshow("stream", frame)
    # write frame to video writer
    out.write(frame)

cv2.waitKey(0)
out.release()