4

使用 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 存储桶中?

谢谢

4

2 回答 2

2

对于您的问题,您可以通过使用从 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
于 2018-09-12T07:19:37.363 回答
1

以下是提取人脸的步骤。使用 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++;                                                                                                           
}); 

使用 docker 进行 ffmpeg,零安装,选择并运行。

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

希望能帮助到你。

于 2018-09-25T01:22:12.197 回答