cyb3r_anarch
cyb3r_anarch

Reputation: 35

opencv how to use cascade on screen recorder

So I'm new to opencv and after practicing with some face detectors and understanding how to use the library, I created my own cascade and it's supposed to identify icons on my computer such as logos and others. first to make sure my cascade worked I wrote one the detects the icons from the images I took,I took a screenshot and processed it through the cascade as an image and worked fine. the code for that is

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)

after some time I wrote this for it to render my screen while detecting the icons the same way it did when I tried it on a screenshot:

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()

but when running the code it doesn't show any errors but it also doesn't detect or identify any of the icons it just records my screen, I've tried debugging this for hours now to no avail, any ideas?

Upvotes: 1

Views: 216

Answers (1)

Ha Bom
Ha Bom

Reputation: 2917

You should show and write the video after you draw rectangles, not before.

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()

Upvotes: 1

Related Questions