Ask Your Question
1

I am create a rectangle box in video stream using python. how to apply face detection in rectangular box inside only and don't need out of the rectangular box detect face

asked 2020-12-03 04:36:11 -0600

revathi gravatar image

updated 2020-12-03 07:37:47 -0600

supra56 gravatar image

Code:

import cv2

face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
ds_factor = 0.6
font = cv2.FONT_HERSHEY_SIMPLEX

class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(0)

    def __del__(self):
        self.video.release()

    def get_frame(self):
        success, image = self.video.read()
        cv2.rectangle(image, (400, 300), (800, 600), (255, 255, 255), -1)
        cv2.resize(output, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA)
        gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(
            gray, scaleFactor=1.1,
            minNeighbors=5,
            minSize=(200, 200),
            flags=cv2.CASCADE_SCALE_IMAGE)

        for (x, y, w, h) in faces:
            cv2.rectangle(output, (x, y), (x + w, y + h), (255, 0, 0), 3)
            cv2.putText(output, 'Number of Faces : ' + str(len(faces)), (40, 40), font, 1, (255, 0, 0), 2)
            break
        ret, jpeg = cv2.imencode('.jpg', output)
        return jpeg.tobytes()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2020-12-03 05:58:42 -0600

berak gravatar image

updated 2020-12-03 06:00:48 -0600

if you want to restrict the face recognition to that rectangle, you canuse a simple ROI:

roi = gray[300:600 , 400:800] # ymin,ymax, xmin,xmax !  

faces = face_cascade.detectMultiScale(
        roi, scaleFactor=1.1,
        minNeighbors=5,
        minSize=(200, 200),
        flags=cv2.CASCADE_SCALE_IMAGE)

but since you get rectangles in the ROI coord system now, you have to add the top-left corner to the rectangles found for drawing in the "large" image:

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

besides that, a minSize of (200,200) is probably too large

edit flag offensive delete link more

Comments

1

thank you so much

revathi gravatar imagerevathi ( 2020-12-03 06:55:06 -0600 )edit

your solution is worked

revathi gravatar imagerevathi ( 2020-12-03 06:55:52 -0600 )edit
1

no need to close questions having accepted answer. better is upvoting the question and the answer.

sturkmen gravatar imagesturkmen ( 2020-12-03 08:07:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-12-03 04:34:48 -0600

Seen: 2,086 times

Last updated: Dec 03 '20