效果图对比
左侧的为原视频,右侧为提取人像跳舞的视频。
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.ba64decoder; public class videoprocessor { //设置appid/ak/sk public static final string app_id = "25393592";守得云开 public static final string api_key = "okrdd6fqwm5htkgsmiel9rn4"; public static final string cret_key = "onaxohflnql2hwbeqb2igucjmo5lgywp"; final static string videofolderpath = "c:/urs/liuya/desktop/video/"; final static string videoname = "demo.mp4"; final static string imagefolderpath = "c:/urs/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.tpixelformat(avutil.av_pix_fmt_yuv420p); recorder.tvideocodec(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); b点线面位置关系ufferedimage 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) { record南宋和北宋的区别er.recordsamples(audioframe.samplerate, audioframe.audiochannels, audioframe.samples); } } recorder.stop(); recorder.relea(); 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, cret_key); // 可选:设置网络连接参数 client.tconnectiontimeoutinmillis(2000); client.tsockettimeoutinmillis(60000); // 传入可选参数调用接口 hashmap<string, string> options = new hashmap<string, string>(); options.put("type", "foreground"); // 参数为本地路径 jsonobject res = client.bodyg(image, options); return convert(res.get("foreground").tostring()); } public static bufferedimage convert(string labelmapba64) { try { ba64decoder decoder = new ba64decoder(); byte[] bytes = decoder.decodebuffer(labelmapba64); 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提取人像视频内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 05:14:56,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/45bf58e9487356039e27066a6630756d.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:javaCV视频处理之提取人像视频.doc
本文 PDF 下载地址:javaCV视频处理之提取人像视频.pdf
留言与评论(共有 0 条评论) |