带有AWS重新认知,我能够在MP4视频中检测到带有以下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重新认知获取 - face-tetection -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 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
这是提取面的步骤。使用AWS重新认知的输出,创建一个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/faces/recursive
希望它有帮助。