【问题标题】:Extract faces from video using AWS Rekognition使用 AWS Rekognition 从视频中提取人脸
【发布时间】:2018-09-25 09:22:12
【问题描述】:

借助 AWS Rekognition,我能够使用以下 nodejs 在 mp4 视频中检测到人脸,

var AWS = require("aws-sdk");                                                                                              
AWS.config.update({                                                                                                        
  region: "us-east-1"                                                                                                      
});                                                                                                                        

var rekognition = new AWS.Rekognition();                                                                                   



var params = {                                                                                                             
  Video: { /* required */                                                                                                  
    S3Object: {                                                                                                            
      Bucket: 'videobucket',                                                                                          
      Name: 'testvideo.mp4'                                                                                                
    }                                                                                                                      
  },                                                                                                                       
  FaceAttributes: "ALL",                                                                                                   
  NotificationChannel: {                                                                                                   
    RoleArn: 'arn:aws:iam::xxx:role/xxx', /* required */                                                     
    SNSTopicArn: 'arn:aws:sns:us-east-1:xxx:alerts' /* required */                                                
  }                                                                                                                        
};                                                                                                                         
rekognition.startFaceDetection(params, function(err, data) {                                                               
  if (err) console.log(err, err.stack); // an error occurred                                                               
  else     console.log(data);           // successful response                                                             
});    

并且能够通过以下cli得到结果,

aws rekognition get-face-detection --job-id xxxxxxx

并以以下json格式输出人脸,

{                                                                                                                          
    "Faces": [                                                                                                             
        {                                                                                                                  
            "Timestamp": 0,                                                                                                
            "Face": {                                                                                                      
                "BoundingBox": {                                                                                           
                    "Width": 0.029999999329447746,                                                                         
                    "Top": 0.2588889002799988,                                                                             
                    "Left": 0.29374998807907104,                                                                           
                    "Height": 0.052222222089767456                                                                         
                },                                                                                                         
                "Landmarks": [                                                                                             
                    {                                                                                                      
                        "Y": 0.28277161717414856,                                                                          
                        "X": 0.3052537739276886,                                                                           
                        "Type": "eyeLeft"                                                                                  
                    },                                                                                                     
                    {                                                                                                      
                        "Y": 0.27957838773727417,                                                                          
                        "X": 0.3085327744483948,                                                                           
                        "Type": "eyeRight"    

如何将这些人脸提取为图像并将它们转储到 s3 存储桶中?

谢谢

【问题讨论】:

  • 你是怎么解决你的问题的?
  • 尚未解决。
  • 你为什么不直接在 aws 提供的时间戳上取帧,裁剪区域(再次由 aws 提供)并一张一张转储人脸?
  • 如何选择一个框架并从中裁剪图像?

标签: amazon-web-services aws-cli amazon-rekognition


【解决方案1】:

对于您的问题,您可以通过使用从 aws rekognition 返回的边界框从原始图像中裁剪人脸部分来单独获取人脸。我已经完成了 python

     widtho = 717 #width of the given image
     heighto = 562 #height of the given image
     counter = 0
     facecount = 1
     s3 = boto3.resource('s3')
     bucket = s3.Bucket('rek')
     if __name__ == "__main__":
     #Choosing the file in s3 bucket
         photo = 'sl.jpg'
         bucket = 'rek'
     #Intilization of rekognition and performing detect_faces 
     client = boto3.client('rekognition', region_name='eu-west-1')
     response = client.detect_faces(
     Image={'S3Object': {'Bucket': bucket, 'Name': photo}}, Attributes=['ALL'])
     print('Detected faces for ' + photo)
     print('The faces are detected and labled from left to right')
     for faceDetail in response['FaceDetails']:
         print('Face Detected= ', i)
         #To mark a bounding box of the image using coordinates
         print('Bounding Box')
         bboxlen = len(faceDetail['BoundingBox'])
         print(bboxlen)
         width = faceDetail['BoundingBox'].get('Width')
         height = faceDetail['BoundingBox'].get('Height')
         left = faceDetail['BoundingBox'].get('Left')
         top = faceDetail['BoundingBox'].get('Top')
         w = int(width * widtho)
         h = int(height * heighto)
         x = int(left * widtho)
         y = int(top * heighto)
         img2 = image_np[y:h, x:w]
         #now you can push the img2 which is one of the face in the single frame

【讨论】:

  • 我只有 mp4 文件和识别结果。如何从视频中获取帧并提取图像?看起来您的代码从图像中提取人脸。
  • 您必须创建另一个脚本,该脚本使用 OpenCV 在检测到人脸时以特定时间戳读取视频帧,然后裁剪每个帧的人脸并按照上述方法从人脸边界框存储在本地或 s3 中方法
  • 感谢您的建议。如果你有一个脚本可以请你分享,这就是我的问题。输入视频,提取人脸并保存在 s3 中。
  • 正是我没有代码....但是您必须聪明地思考,并非视频中的所有帧都有人脸...如果您想从 aws rekognition 中省钱做本地opencv检测帧中的人脸,如果检测到将帧发送到s3桶......然后在桶中的图像中进行重新识别,确保保存具有相关帧时间名称的图像......这样当你返回坐标时特定帧,您可以使用获得的坐标为视频中检测到的帧执行 cv2.rectangle
  • 谢谢。将视频转换为图像是一个繁琐的过程,并且读取和写入需要更长的时间。 rekognition 应该能够即时完成。还是没时间看。会持续更新。
【解决方案2】:

以下是提取人脸的步骤。使用 AWS rekognition 的输出,创建一个 shell 脚本输出以使用 ffmpeg 提取人脸。在面部添加额外的填充物以获得头发、下巴和侧耳。您可以根据自己的需要进行调整。

var faces = require('./output1.json');                                                                                     

var oldtimestamp = -1;                                                                                                     
var sequence = 1;                                                                                                          



faces.Faces.forEach(function(element) {                                                                                    
    var size = element.Face.BoundingBox;                                                                                   
    var vheight = 576;                                                                                                     
    var vwidth = 720;                                                                                                      
    size.Width = parseInt(size.Width * 1.25 *  vwidth);                                                                    
    size.Left = parseInt(size.Left * vwidth);                                                                              
    size.Top = parseInt(size.Top * 0.5  * vheight)  ;                                                                      
    size.Height = parseInt(size.Height * 1.75 * vheight);                                                                  

  var newtimestamp = element.Timestamp;                                                                                    
  if( oldtimestamp != newtimestamp ) {                                                                                     
        console.log('ffmpeg  -i f1.vob  -ss 00:00:' + (element.Timestamp/1000) + '   -qmin 1 -q:v 1 -frames:v 1 ./pictures/frame' + element.Timestamp + '.png ');                                                                                     
     oldtimestamp = newtimestamp;                                                                                          
     sequence = 1;                                                                                                         
        }                                                                                                                  
  console.log('ffmpeg -i ./pictures/frame' + element.Timestamp + '.png -vf  "crop=' + size.Width + ':' + size.Height + ':' + size.Left + ':' + size.Top +  '"  ./faces/frame' + element.Timestamp + '' + sequence + '.png');                          
     sequence++;                                                                                                           
}); 

ffmpeg使用docker,零安装,pick并运行。

docker run -it --rm --entrypoint='bash' -v ${PWD}:/tmp/workdir jrottenberg/ffmpeg

您需要除以 1000 以将毫秒转换为秒的时间戳。手动添加#!/bin/sh。

#!/bin/sh                                                                                                                  
ffmpeg  -i f1.vob  -ss 00:00:0   -qmin 1 -q:v 1 -frames:v 1 ./pictures/frame0.png                                          
ffmpeg -i ./pictures/frame0.png -vf  "crop=171:200:446:70"  ./faces/frame01.png                                            
ffmpeg -i ./pictures/frame0.png -vf  "crop=170:198:168:43"  ./faces/frame02.png                                            
ffmpeg -i ./pictures/frame0.png -vf  "crop=192:224:532:23"  ./faces/frame03.png                                            
ffmpeg -i ./pictures/frame0.png -vf  "crop=151:176:82:85"  ./faces/frame04.png                                             
ffmpeg  -i f1.vob  -ss 00:00:0.2   -qmin 1 -q:v 1 -frames:v 1 ./pictures/frame200.png                                      
ffmpeg -i ./pictures/frame200.png -vf  "crop=170:198:446:71"  ./faces/frame2001.png                                        
ffmpeg -i ./pictures/frame200.png -vf  "crop=174:203:167:43"  ./faces/frame2002.png                                        
ffmpeg -i ./pictures/frame200.png -vf  "crop=194:226:531:22"  ./faces/frame2003.png                                        
ffmpeg -i ./pictures/frame200.png -vf  "crop=152:176:81:86"  ./faces/frame2004.png                                         
ffmpeg  -i f1.vob  -ss 00:00:0.4   -qmin 1 -q:v 1 -frames:v 1 ./pictures/frame400.png                                      
ffmpeg -i ./pictures/frame400.png -vf  "crop=170:197:446:71"  ./faces/frame4001.png                                        
ffmpeg -i ./pictures/frame400.png -vf  "crop=174:203:167:43"  ./faces/frame4002.png                                        
ffmpeg -i ./pictures/frame400.png -vf  "crop=194:226:531:22"  ./faces/frame4003.png                                        
ffmpeg -i ./pictures/frame400.png -vf  "crop=152:176:81:86"  ./faces/frame4004.png                                         
ffmpeg  -i f1.vob  -ss 00:00:0.6   -qmin 1 -q:v 1 -frames:v 1 ./pictures/frame600.png 

将输出复制到 S3:

aws s3 cp faces s3://outputbucket/faces/ --recursive

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 2017-06-09
    • 2019-10-19
    • 2014-11-28
    • 2020-07-16
    相关资源
    最近更新 更多