javaCV 视频处理技术 ->提取人像视频 | 百度AI
2021/12/24 9:38:28
本文主要是介绍javaCV 视频处理技术 ->提取人像视频 | 百度AI,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
效果图对比
左侧的为原视频,右侧为提取人像跳舞的视频。
之前写的文章 JAVA代码实现人物照片的人像分割 | 百度AI 是处理图片的 ,视频处理也是在图片处理基础上实现的。
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 视频处理技术 ->提取人像视频 | 百度AI的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南