def process(self, tup):
        mylogger = loger.getLoger("ExtractImages", Constants.boltpath + "logs")
        try:

            filepath = tup.values[0]
            input_image = tup.values[1]
            type = tup.values[2]

            mylogger.info("Loading image from video " + str(filepath) +
                          " input_image: " + str(input_image) + " Type: " +
                          str(type))
            frame = cv2.imread(input_image)
            mylogger.info("Image loaded")

            # convert the input frame from BGR to RGB then resize it to have
            # a width of 500px (to speedup processing)
            mylogger.info("Converting color")
            rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            mylogger.info("Resizing")
            rgb = imutils.resize(rgb, width=videoWidth)
            r = frame.shape[1] / float(rgb.shape[1])

            # detect the (x, y)-coordinates of the bounding boxes
            # corresponding to each face in the input frame, then compute
            # the facial embeddings for each face
            boxes = face_recognition.face_locations(rgb,
                                                    model=Constants.model_type)
            mylogger.info("Locating faces")
            encodings = face_recognition.face_encodings(rgb, boxes)
            mylogger.info("Encoding faces")
            encoding_number = 0
            x = uuid.uuid4()
            for encoding in encodings:
                video_file_name = os.path.basename(filepath)
                encoding_number = encoding_number + 1
                mylogger.info("saving faces files")
                if not os.path.exists(Constants.boltpath + "results/"):
                    os.makedirs(Constants.boltpath + "results/")

                output_image = Constants.boltpath + "results/" \
                               + video_file_name \
                               + "_frame_" \
                               + str(x) \
                               + "_encoding_" \
                               + str(encoding_number) \
                               + "_result.png"
                mylogger.info("Saving file " + output_image)
                cv2.imwrite(output_image, rgb)
                # mylogger.info(encoding)
                self.emit(
                    [filepath, output_image,
                     encoding.tolist(), boxes, r])

        except Exception as inst:
            mylogger.error("Python error.")
            mylogger.error(type(inst))
            mylogger.error.error(inst)
    def process(self, tup):
        mylogger = loger.getLoger("IdentifyFaces", Constants.boltpath + "logs")
        try:

            filepath = tup.values[0]
            type = tup.values[1]
            size = tup.values[2]

            mylogger.info("Loading video " + str(filepath) + " Size: " +
                          str(size) + " Type: " + str(type))
            stream = cv2.VideoCapture(filepath)

            mylogger.info("Video loaded")
            (haveMoreFrames, frame) = stream.read()
            if haveMoreFrames:
                new_Videos = Videos(video_path=str(filepath))
                DatabaseSession.session.add(new_Videos)
                DatabaseSession.session.commit()
                # session.query(Videos).all()
            frame_number = 0
            while True:
                # grab the next frame

                frame_number = frame_number + 1
                # end of the stream
                # only 1 of Constants.numberOfFrames
                if frame_number % Constants.numberOfFrames != 0:
                    continue
                if not haveMoreFrames:
                    break
                x = uuid.uuid4()
                if not os.path.exists(Constants.boltpath + "frames/"):
                    os.mkdir(Constants.boltpath + "frames/")
                    print("Directory ",
                          Constants.boltpath + "frames/" + " Created ")
                output_image = Constants.boltpath + "frames/" + str(x) + ".png"
                cv2.imwrite(output_image, frame)
                self.emit([filepath, output_image, "PNG"])
                (haveMoreFrames, frame) = stream.read()
            stream.release()
        except Exception as inst:
            mylogger.error("Python error.")
            mylogger.error(type(inst))
            mylogger.error.error(inst)
def process():
    mylogger = loger.getLoger("Whisper", Constants.boltpath + "logs")
    try:
        timenow = Blocker.current_milli_time()
        # Generic models
        video = DatabaseSession.session.query(Videos).first()

        # Some paths

        detector = dlib.get_frontal_face_detector()
        sp = dlib.shape_predictor(
            Constants.predictor_path)  # 128D face descriptor predictor
        facerec = dlib.face_recognition_model_v1(Constants.face_rec_model_path)
        descriptors = []
        images = []

        # Now find all the faces and compute 128D face descriptors for each face.
        for f in glob.glob(os.path.join(Constants.faces_folder_path, "*.png")):
            mylogger.info("Processing file: {}".format(f))
            img = dlib.load_rgb_image(f)

            # Ask the detector to find the bounding boxes of each face. The 1 in the
            # second argument indicates that we should upsample the image 1 time. This
            # will make everything bigger and allow us to detect more faces.
            dets = detector(img, 1)
            mylogger.info("Number of faces detected: {}".format(len(dets)))

            # Now process each face we found.
            for k, d in enumerate(dets):
                # Get the landmarks/parts for the face in box d.
                shape = sp(img, d)

                # Compute the 128D vector that describes the face in img identified by
                # shape.
                face_descriptor = facerec.compute_face_descriptor(img, shape)
                descriptors.append(face_descriptor)
                images.append((img, shape))

        # Now let's cluster the faces.
        labels = dlib.chinese_whispers_clustering(descriptors,
                                                  Constants.TOLERANCE_WHISPER)
        num_classes = len(set(labels))
        mylogger.info("Number of clusters: {}".format(num_classes))
        if num_classes > 0:
            # Find the indices for the biggest class
            indices = []
            counts = 0
            DatabaseSession.session.query(Wishper).filter(
                Wishper.video == video).delete()
            DatabaseSession.session.commit()
            for i, label in enumerate(labels):
                # Ensure output directory exists
                output_folder_path_real = Constants.output_folder_path + str(
                    label)
                if not os.path.isdir(output_folder_path_real):
                    os.makedirs(output_folder_path_real)

                # Save the extracted faces
                mylogger.info("Saving faces cluster to output folder...")
                img, shape = images[i]
                file_path = os.path.join(output_folder_path_real,
                                         "face_" + str(counts))
                # The size and padding arguments are optional size=300x300 and padding=0.25
                dlib.save_face_chip(img,
                                    shape,
                                    file_path,
                                    size=300,
                                    padding=0.25)
                counts = counts + 1
                whisper = Wishper(alias=file_path,
                                  original_image=file_path,
                                  video=video,
                                  group=str(label),
                                  path=file_path + ".jpg")

                DatabaseSession.session.add(whisper)
                DatabaseSession.session.commit()

    except Exception as inst:
        mylogger.error("Python error.")
        mylogger.error(type(inst))
        mylogger.error(inst)
Example #4
0
    def process(self, tup):
        mylogger = loger.getLoger("RecognizeFaces",
                                  Constants.boltpath + "logs")
        dataset = loger.getLoger("dataset",
                                 Constants.boltpath + "dataset",
                                 format='%(message)s')
        names = []
        colors = []
        try:

            filepath = tup.values[0]
            output_image = tup.values[1]
            encoding_array = tup.values[2]
            encoding = np.array(encoding_array)
            boxes = tup.values[3]
            r = tup.values[4]
            mylogger.info("Loading tuple output_image: " + output_image +
                          " boxes:" + str(boxes) + " r: " + str(r))
            mylogger.info("Load model file")
            models = Constants.know_people_pickle
            data = pickle.loads(open(models, "rb").read())
            matches = face_recognition.compare_faces(
                data["encodings"], encoding, tolerance=Constants.TOLERANCE)
            name = "Unknown"

            # check to see if we have found a match
            if True in matches:
                # find the indexes of all matched faces then initialize a
                # dictionary to count the total number of times each face
                # was matched
                matchedIdxs = [i for (i, b) in enumerate(matches) if b]
                counts = {}

                # loop over the matched indexes and maintain a count for
                # each recognized face face
                for i in matchedIdxs:
                    name = data["names"][i]
                    counts[name] = counts.get(name, 0) + 1

                # determine the recognized face with the largest number
                # of votes (note: in the event of an unlikely tie Python
                # will select first entry in the dictionary)
                name = max(counts, key=counts.get)
                colors.append((0, 0, 255))
            else:
                colors.append((0, 255, 0))

            # update the list of names
            names.append(name)

            frame = cv2.imread(output_image)
            # loop over the recognized faces
            for ((top, right, bottom, left), name,
                 color) in zip(boxes, names, colors):
                # rescale the face coordinates
                top = int(top)
                right = int(right)
                bottom = int(bottom)
                left = int(left)

                # draw the predicted face name on the image
                cv2.rectangle(frame, (left, top), (right, bottom), color, 2)
                y = top - 15 if top - 15 > 15 else top + 15
                cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,
                            0.75, color, 2)
                x = uuid.uuid4()

                if not os.path.exists(Constants.boltpath + "faces/" +
                                      str(name) + "/"):
                    os.makedirs(Constants.boltpath + "faces/" + str(name) +
                                "/")

                output_image_faces = Constants.boltpath + "faces/" + str(
                    name) + "/" + str(x) + "_faces.png"
                dataset.info(
                    '"' + filepath + '"' + ', "' + output_image_faces + '"' +
                    ', "' + name + '"' + ', ' + str(top) + ', ' + str(right) +
                    ', ' + str(bottom) + ', ' + str(left) + ', "Encoding" ' +
                    ', ' +
                    str(encoding_array).replace("[", "").replace("]", ""))

            cv2.imwrite(output_image_faces, frame)
            video = DatabaseSession.session.query(Videos).filter(
                Videos.video_path == filepath).first()

            if name == "Unknown":
                alias = str(uuid.uuid4())
                mylogger.info("UNKNOWN FOUND!!!!!!!")
                mylogger.info(output_image_faces)
                unknow = Unknows(alias=alias,
                                 video=video,
                                 path=output_image_faces)
                DatabaseSession.session.add(unknow)
                DatabaseSession.session.commit()

                self.emit([output_image_faces, alias, filepath])
            else:
                know = Knows(name=name, video=video, path=output_image_faces)
                DatabaseSession.session.add(know)
                DatabaseSession.session.commit()
            Blocker.actualtime = Blocker.current_milli_time()

        except Exception as inst:
            mylogger.error("Python error.")
            mylogger.error(type(inst))
            mylogger.error(inst)