Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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()
    output = cv2.rectangle(image, (400, 300), (800, 600), (255, 255, 255), 2)
    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()
click to hide/show revision 2
None

updated 2020-12-03 04:55:30 -0600

berak gravatar image

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

.

import cv2

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

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

cv2.VideoCapture(0)

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

 def get_frame(self):
     success, image = self.video.read()
     output = cv2.rectangle(image, (400, 300), (800, 600), (255, 255, 255), 2)
     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()

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

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

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()
     output = image.copy()
    cv2.rectangle(image, (400, 300), (800, 600), (255, 255, 255), 2)
    -1)
    cv2.addWeighted(image, 0.5, output, 1 - .5, 0, output)
    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()

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

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()
    output = image.copy()
    cv2.rectangle(image, (400, 300), (800, 600), (255, 255, 255), -1)
    cv2.addWeighted(image, 0.5, output, 1 - .5, 0, output)
    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()

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

Code:

import cv2

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

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

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