Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to extract Portrait Video from javaCV Video processing

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to extract portrait video from javaCV video processing". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to extract portrait video from javaCV video processing".

Effect picture comparison

The video on the left is the original video, and the video on the right is the video of the dancing portrait.

Pom files introduce dependencies

Com.baidu.aip java-sdk 4.16.3 org.bytedeco javacv-platform 1.5.5

Java core implementation code (complete)

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 {/ / set 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);} / Video watermark public static void videoProcess (String filePath) {/ / grab video image resource FFmpegFrameGrabber videoGrabber = new FFmpegFrameGrabber (filePath); / / grab video image resource 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 (); / / processed image int videoSize = videoGrabber.getLengthInVideoFrames () For (int I = 0; I < videoSize; iTunes +) {Frame videoFrame = videoGrabber.grabImage (); if (videoFrame! = null & & videoFrame.image! = null) {System.out.println ("video total" + videoSize + "frame", processing frame "+ (I + 1) + frame picture"); Java2DFrameConverter converter = new Java2DFrameConverter () BufferedImage bi=converter.getBufferedImage (videoFrame); BufferedImage bufferedImage = splitting (bi); recorder.record (converter.convert (bufferedImage));}} / / process audio for (int I = 0; I < audioGrabber.getLengthInAudioFrames (); iTunes +) {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) {/ / initialize an AipBodyAnalysis AipBodyAnalysis client = new AipBodyAnalysis (APP_ID, API_KEY, SECRET_KEY); / / optional: set network connection parameters client.setConnectionTimeoutInMillis (2000); client.setSocketTimeoutInMillis (60000); / / enter optional parameters to call the API HashMap options = new HashMap () Options.put ("type", "foreground"); / / Parameter is the local path 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); / / distortion processing BufferedImage newBufferedImage = newBufferedImage (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;}

Console output

The above is all the contents of the article "how to extract Portrait Video from javaCV Video processing". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report