Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

i am creating a dataset for real time face recognition using python. i want it should intimate if the image already available in dataset. how to implement in opencv?

import cv2 from PIL import Image import os import time

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

dir_path = "static/capture_image"

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

    for (x, y, w, h) in faces:
        cv2.rectangle(output, (x + 400, y + 300), (x + 400 + w, y + 300 + h), (255, 0, 0), 3)
        cv2.putText(output, 'Number of Faces : ' + str(len(faces)), (40, 40), font, 1, (255, 0, 0), 2)
        face_image = gray[y+300:y + 300 + h, x + 400:x + 400 + w]
        pil_image = Image.fromarray(face_image)
        File_Formatted = ("%s" % (y+300)) + ".jpg"
        file_path = os.path.join(dir_path, File_Formatted)
        pil_image.save(file_path)
        break
    ret, jpeg = cv2.imencode('.jpg', output)
    return jpeg.tobytes()
click to hide/show revision 2
None

updated 2020-12-04 06:51:01 -0600

berak gravatar image

i am creating a dataset for real time face recognition using python. i want it should intimate if the image already available in dataset. how to implement in opencv?

.

import cv2
from PIL import Image
import os
import time

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

cv2.FONT_HERSHEY_SIMPLEX dir_path = "static/capture_image"

"static/capture_image" 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 = image.copy()
     cv2.rectangle(image, (400, 300), (800, 600), (255, 255, 255), 2)
     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)
     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)

     for (x, y, w, h) in faces:
         cv2.rectangle(output, (x + 400, y + 300), (x + 400 + w, y + 300 + h), (255, 0, 0), 3)
         cv2.putText(output, 'Number of Faces : ' + str(len(faces)), (40, 40), font, 1, (255, 0, 0), 2)
         face_image = gray[y+300:y + 300 + h, x + 400:x + 400 + w]
         pil_image = Image.fromarray(face_image)
         File_Formatted = ("%s" % (y+300)) + ".jpg"
         file_path = os.path.join(dir_path, File_Formatted)
         pil_image.save(file_path)
         break
     ret, jpeg = cv2.imencode('.jpg', output)
     return jpeg.tobytes()