javaCV视频处理之提取人像视频

 更新时间:2021年12月24日 11:03:07   作者:洛阳泰山  
这篇文章主要介绍了利用JavaCV实现提取视频中的人像并保存为视频,文中的示例代码讲解详细,对我们学习JavaCV有一定的帮助,需要的可以参考一下

效果图对比

左侧的为原视频,右侧为提取人像跳舞的视频。

pom文件引入依赖

	<!-- https://mvnrepository.com/artifact/com.baidu.aip/java-sdk -->
		<dependency>
			<groupId>com.baidu.aip</groupId>
			<artifactId>java-sdk</artifactId>
			<version>4.16.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
		<dependency>
			<groupId>org.bytedeco</groupId>
			<artifactId>javacv-platform</artifactId>
			<version>1.5.5</version>
		</dependency>

 java核心实现代码(完整) 

 
import com.baidu.aip.bodyanalysis.AipBodyAnalysis;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
 
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
 
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.ffmpeg.global.avcodec;
import org.json.JSONObject;
import sun.misc.BASE64Decoder;
 
public class VideoProcessor {
    //设置APPID/AK/SK
    public static final String APP_ID = "25393592";
    public static final String API_KEY = "OkRDD6FQwm5hTKGSMIEL9RN4";
    public static final String SECRET_KEY = "ONAxohflnqL2HwBEQB2iGUCjmO5lgywp";
 
 
    final static String videoFolderPath = "C:/Users/liuya/Desktop/video/";
    final static String videoName = "demo.mp4";
    final static String imageFolderPath = "C:/Users/liuya/Desktop/people/";
 
 
    public static void main(String[] args) throws Exception {
        videoProcess(videoFolderPath + videoName);
    }
 
    //视频水印
    public static void videoProcess(String filePath) {
        //抓取视频图像资源
        FFmpegFrameGrabber videoGrabber = new FFmpegFrameGrabber(filePath);
        //抓取视频图像资源
        FFmpegFrameGrabber audioGrabber = new FFmpegFrameGrabber(filePath);
        try {
            videoGrabber.start();
            audioGrabber.start();
            FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(videoFolderPath + "new" + videoName, videoGrabber.getImageWidth(), videoGrabber.getImageHeight(), videoGrabber.getAudioChannels());
            recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            recorder.start();
            //处理图像
            int videoSize = videoGrabber.getLengthInVideoFrames();
            for (int i = 0; i < videoSize; i++) {
                Frame videoFrame = videoGrabber.grabImage();
                if (videoFrame != null && videoFrame.image != null) {
                    System.out.println("视频共" + videoSize + "帧,正处理第" + (i + 1) + "帧图片");
                    Java2DFrameConverter converter = new Java2DFrameConverter();
                    BufferedImage bi=converter.getBufferedImage(videoFrame);
                    BufferedImage bufferedImage = splitting(bi);
                    recorder.record(converter.convert(bufferedImage));
                }
            }
            //处理音频
            for (int i = 0; i < audioGrabber.getLengthInAudioFrames(); i++) {
                Frame audioFrame = audioGrabber.grabSamples();
                if (audioFrame != null && audioFrame.samples != null) {
                    recorder.recordSamples(audioFrame.sampleRate, audioFrame.audioChannels, audioFrame.samples);
                }
            }
            recorder.stop();
            recorder.release();
            videoGrabber.stop();
            audioGrabber.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static BufferedImage splitting(BufferedImage image){
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        try {
            ImageIO.write(image,"png",out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return splitting(out.toByteArray());
    }
 
    public static BufferedImage splitting(byte[] image){
        // 初始化一个AipBodyAnalysis
        AipBodyAnalysis client = new AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY);
        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("type", "foreground");
        // 参数为本地路径
        JSONObject res = client.bodySeg(image, options);
        return  convert(res.get("foreground").toString());
    }
 
 
    public static BufferedImage convert(String labelmapBase64) {
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bytes = decoder.decodeBuffer(labelmapBase64);
            InputStream is = new ByteArrayInputStream(bytes);
            BufferedImage image = ImageIO.read(is);
            //失真处理
            BufferedImage newBufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
            newBufferedImage.createGraphics().drawImage(image, 0, 0, Color.WHITE, null);
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            ImageIO.write(newBufferedImage, "png", out);
            ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
            return ImageIO.read(in);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
 
 
}

控制台输出

到此这篇关于javaCV视频处理之提取人像视频的文章就介绍到这了,更多相关javaCV提取人像视频内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • spring boot配合前端实现跨域请求访问

    spring boot配合前端实现跨域请求访问

    本篇文章主要介绍了spring boot配合前端实现跨域请求访问,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • java final 和instanceof 关键字的区别

    java final 和instanceof 关键字的区别

    这篇文章介绍了java final 和instanceof 关键字的区别,有需要的朋友可以参考一下
    2013-09-09
  • Spring Boot实现微信扫码登录功能流程分析

    Spring Boot实现微信扫码登录功能流程分析

    这篇文章主要介绍了Spring Boot 实现微信扫码登录功能,介绍了授权流程代码和用户登录和登出的操作代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • java组件commons-fileupload实现文件上传

    java组件commons-fileupload实现文件上传

    这篇文章主要介绍了java借助commons-fileupload组件实现文件上传,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • SpringBoot中实现异步调用@Async详解

    SpringBoot中实现异步调用@Async详解

    这篇文章主要介绍了SpringBoot中实现异步调用@Async详解,在SpringBoot的日常开发中,一般都是同步调用的,但实际中有很多场景非常适合使用异步来处理,需要的朋友可以参考下
    2024-01-01
  • SpringBoot 中实现跨域的5种方式小结

    SpringBoot 中实现跨域的5种方式小结

    这篇文章主要介绍了SpringBoot 中实现跨域的5种方式小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Spring注解驱动之BeanDefinitionRegistryPostProcessor原理解析

    Spring注解驱动之BeanDefinitionRegistryPostProcessor原理解析

    这篇文章主要介绍了Spring注解驱动之BeanDefinitionRegistryPostProcessor原理,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • 初识sa-token及登录授权简单实现

    初识sa-token及登录授权简单实现

    这篇文章主要为大家介绍了sa-token及登录授权简单实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Java调用外接设备详解(制卡机)

    Java调用外接设备详解(制卡机)

    这篇文章主要为大家详细介绍了Java调用外接设备的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Java中的Hashtable源码详细解析

    Java中的Hashtable源码详细解析

    这篇文章主要介绍了Java中的Hashtable源码详细解析,Hashtable 的函数都是同步的,这意味着它是线程安全的,它的key、value都不可以为null,此外,Hashtable中的映射不是有序的,需要的朋友可以参考下
    2023-11-11

最新评论