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?

asked 2020-12-04 06:49:45 -0500

revathi gravatar image

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

berak gravatar image

.

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()
edit retag flag offensive close merge delete

Comments

i want it should intimate

what does this mean ?

berak gravatar imageberak ( 2020-12-04 06:53:14 -0500 )edit
1

read "to intimate" as "to reveal/indicate".

crackwitz gravatar imagecrackwitz ( 2020-12-04 07:39:28 -0500 )edit

as for the subject: you would have to recognize subjects. you can do that by teaching the system a subject from sample faces, or you can let it collect faces and figure out which faces are very similar and which faces aren't (a clustering problem). this problem is harder than simply collecting faces as you do.

crackwitz gravatar imagecrackwitz ( 2020-12-04 07:41:12 -0500 )edit

I am creating a dataset for face recognition using python. Then i want to validate if the face is already available in the dataset or it is the new one. If it is the new face I want the text box to enter his/her name and details. If it is already available need to show the pop up like "the face is already available".

revathi gravatar imagerevathi ( 2020-12-05 00:09:54 -0500 )edit

look at facenet or other face recognition methods (eigenfaces, fisher faces). you need to formulate a similarity/distance measure on faces.

crackwitz gravatar imagecrackwitz ( 2020-12-05 13:21:11 -0500 )edit