javaCV視頻處理之提取人像視頻
效果圖對(duì)比
左側(cè)的為原視頻,右側(cè)為提取人像跳舞的視頻。
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核心實(shí)現(xiàn)代碼(完整)?
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 { //設(shè)置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){ // 初始化一個(gè)AipBodyAnalysis AipBodyAnalysis client = new AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY); // 可選:設(shè)置網(wǎng)絡(luò)連接參數(shù) client.setConnectionTimeoutInMillis(2000); client.setSocketTimeoutInMillis(60000); // 傳入可選參數(shù)調(diào)用接口 HashMap<String, String> options = new HashMap<String, String>(); options.put("type", "foreground"); // 參數(shù)為本地路徑 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; } } }
控制臺(tái)輸出
到此這篇關(guān)于javaCV視頻處理之提取人像視頻的文章就介紹到這了,更多相關(guān)javaCV提取人像視頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot配合前端實(shí)現(xiàn)跨域請(qǐng)求訪問(wèn)
本篇文章主要介紹了spring boot配合前端實(shí)現(xiàn)跨域請(qǐng)求訪問(wèn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04java final 和instanceof 關(guān)鍵字的區(qū)別
這篇文章介紹了java final 和instanceof 關(guān)鍵字的區(qū)別,有需要的朋友可以參考一下2013-09-09Spring?Boot實(shí)現(xiàn)微信掃碼登錄功能流程分析
這篇文章主要介紹了Spring?Boot?實(shí)現(xiàn)微信掃碼登錄功能,介紹了授權(quán)流程代碼和用戶登錄和登出的操作代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04java組件commons-fileupload實(shí)現(xiàn)文件上傳
這篇文章主要介紹了java借助commons-fileupload組件實(shí)現(xiàn)文件上傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10SpringBoot中實(shí)現(xiàn)異步調(diào)用@Async詳解
這篇文章主要介紹了SpringBoot中實(shí)現(xiàn)異步調(diào)用@Async詳解,在SpringBoot的日常開(kāi)發(fā)中,一般都是同步調(diào)用的,但實(shí)際中有很多場(chǎng)景非常適合使用異步來(lái)處理,需要的朋友可以參考下2024-01-01SpringBoot 中實(shí)現(xiàn)跨域的5種方式小結(jié)
這篇文章主要介紹了SpringBoot 中實(shí)現(xiàn)跨域的5種方式小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Spring注解驅(qū)動(dòng)之BeanDefinitionRegistryPostProcessor原理解析
這篇文章主要介紹了Spring注解驅(qū)動(dòng)之BeanDefinitionRegistryPostProcessor原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09初識(shí)sa-token及登錄授權(quán)簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家介紹了sa-token及登錄授權(quán)簡(jiǎn)單實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Java調(diào)用外接設(shè)備詳解(制卡機(jī))
這篇文章主要為大家詳細(xì)介紹了Java調(diào)用外接設(shè)備的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07