Example #1
0
def getFace(videofile, tagfile, model='hog'):
    print("tagfile: ", tagfile)
    fileWriter = open(tagfile, "wt")
    vh = cv_video_helper.VideoHelper()

    for num_retrieved, frame in vh.frame_iterator(videofile, every_ms=1000):
        print("num_retrieved: ", num_retrieved)
        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb = frame[:, :, ::-1]

        face_locations = face_recognition.face_locations(rgb, model=model)
        height = rgb.shape[0]
        width = rgb.shape[1]
        fileWriter.write(str(num_retrieved))
        if len(face_locations) > 0:
            fileWriter.write(",")
            first = True
            for face_location in face_locations:
                # Print the location of each face in this image
                top, right, bottom, left = face_location
                # x ,y, width, height
                tem = '%s %s %s %s' % ('%.16f' %
                                       (float(left) / width), '%.16f' %
                                       (float(bottom) / height), '%.16f' %
                                       (float(right - left) / width), '%.16f' %
                                       (float(bottom - top) / height))
                if first:
                    fileWriter.write(tem)
                    first = False
                else:
                    fileWriter.write(" " + tem)

        fileWriter.write("\n")
    fileWriter.close()
Example #2
0
def process(videofile, tfrecordDir=''):
    file_arr = os.path.splitext(videofile)
    if (tfrecordDir):  # valid
        filename = os.path.basename(videofile).split(".")[0]
        output_tfrecords_file = "%s_output.tfrecord" % (tfrecordDir + '/' +
                                                        str(filename))
    else:
        output_tfrecords_file = "%s_output.tfrecord" % file_arr[0]
    FLAGS.output_tfrecords_file = output_tfrecords_file
    print("output_tfrecords_file", output_tfrecords_file)

    extractor = feature_extractor.YouTube8MFeatureExtractor(FLAGS.model_dir)
    writer = tf.python_io.TFRecordWriter(FLAGS.output_tfrecords_file)

    total_written = 0
    total_error = 0
    labels = "0"
    vh = cv_video_helper.VideoHelper()

    for num_retrieved, rgb in vh.frame_iterator(videofile,
                                                every_ms=1000.0 /
                                                FLAGS.frames_per_second):
        rgb_features = []
        features = extractor.extract_rgb_frame_features(rgb[:, :, ::-1])
        rgb_features.append(_bytes_feature(quantize(features)))
        if not rgb_features:
            printError('Could not get features for ' + videofile +
                       ' ,frame order is ' + num_retrieved)
            total_error += 1
            continue

        # Create SequenceExample proto and write to output.
        feature_list = {
            FLAGS.image_feature_key:
            tf.train.FeatureList(feature=rgb_features),
        }
        if FLAGS.insert_zero_audio_features:
            feature_list['audio'] = tf.train.FeatureList(
                feature=[_bytes_feature(_make_bytes([0] * 128))] *
                len(rgb_features))

        # print(feature_list)
        # E:/work/ai_script/tmp.MP4 -> E:/work/ai_script/tmp/%d.MP4
        # url encode
        virtualPath = quote(
            file_arr[0]) + '/' + str(num_retrieved) + file_arr[1]
        print("virtualPath = ", virtualPath)
        example = tf.train.SequenceExample(
            context=tf.train.Features(
                feature={
                    FLAGS.labels_feature_key:
                    _int64_list_feature(sorted(map(int, labels.split(';')))),
                    FLAGS.video_file_key_feature_key:
                    _bytes_feature(_make_bytes(map(ord, virtualPath))),
                }),
            feature_lists=tf.train.FeatureLists(feature_list=feature_list))
        writer.write(example.SerializeToString())
        total_written += 1

    writer.close()
    print('Successfully encoded %i out of %i videos' %
          (total_written, total_written + total_error))