Otaku Welfare! I used Python to make a young lady dancing at station B, with beautiful legs all over the screen!

Click on the blue "Rookie Learn Python" above and select the "Star" public account

重磅干货,第一时间送到

At home on the weekend, the weather is so cold, so cold, it's snowing here! There is nothing to do at home, so I stewed an old hen to make up my body.

While stewing chicken soup, code some fun scripts in Python. After working hard for a weekend, rookie brother wrote a fun little script, and I quickly shared it with you.

There are really many beautiful ladies on station B. Today we will play a video to capture the dancing girls on station B, and use everyone's bullet screen in the video to draw a dance video that jumps the bullet screen, which is very dazzling. Cool~~ Let's take a look together.

01. Barrage grab

First, we select a dance video of Miss Sister with a lot of barrage, and download the video. At the same time, use the following program to capture the barrage information in the video of Miss Sister.

In the above program, first construct the bullet screen URL link of the specified date. Since the bullet screen link is a static web page, requests are directly used to request the source code of the web page, and the bullet screen information is extracted, and the extracted bullet screen information is deduplicated and written. into a local file.

02. Convert video into picture

Next, we convert the video into a picture. Since the video is too long, we need to extract a part of the video as the next processing video. The procedure is as follows:

videoCrop = VideoFileClip("video.flv").subclip(18, 33)videoCrop.write_videofile("subvideo.mp4")

In the above program, the video from 18 seconds to 33 seconds of the complete video is extracted and saved as an mp4 file. After extracting the video, the next step is to convert each frame of the video into a picture and save it to a local folder. The procedure is as follows:

In the above program, opencv is used to read the video frame by frame and save it to the local folder in order. The converted picture is shown in the following figure.

03. Use Baidu AI to convert portrait segmentation into binary image

After converting the video to the picture, the next step is to segment the portrait, and convert the lady into a binary image to separate the portrait from the background. Here, you can use Baidu AI's portrait segmentation interface. The program is as follows.

In the above program, the interface of Baidu AI is used to perform portrait segmentation. Here, we only need to return the binary result. We will process the returned binary result and save it as a picture in png format.

04. Miss sister word cloud generation

After having the binary segmentation map of Miss Sister, the next step is to use these maps as masks to generate word cloud maps. The program is as follows:

In the program, first read the picture in binary form, then use jieba to segment words and remove the phrases we don't want to appear in the result of word segmentation; use stylecloud to make word clouds.

It should be noted here that the source code of stylecloud does not support custom masks, so we need to modify and add them in the source code of stylecloud so that it supports custom masks. The created word cloud graph is shown below.

It can be seen that most of everyone's barrage information is envy and liking for the wonderful figure of the young lady. Next, let's take a look at how to use the program to make a word cloud into a video and display it in combination with the original audio.

05. Composite video

The program first sets the name and frame rate of the output video, then uses opencv to write the picture into the video file to form a word cloud video, and finally merges the word cloud video and the audio of the original video to form our final little sister Word cloud video, the synthesized video effect is as follows:

The above is the little sister word cloud dance video brought to you today. You can also make a favorite dynamic word cloud video and synthesize your favorite video.

After a hard weekend, please give me a squeak in the message area, and remember to give me three consecutive times!


程序员GitHub,现已正式上线!



接下来我们将会在该公众号上,专注为大家分享GitHub上有趣的开源库包括Python,Java,Go,前端开发等优质的学习资源和技术,分享一些程序员圈的新鲜趣事。
推荐阅读:这个GitHub 1400星的Git魔法书火了,斯坦福校友出品丨有中文版贼 TM 好用的 Java 工具类库
超全Python IDE武器库大总结,优缺点一目了然!
秋招来袭!GitHub28.5颗星!这个汇聚阿里,腾讯,百度,美团,头条的面试题库必须安利!
收获10400颗星!这个Python库有点黑科技,竟然可以伪造很多'假'的数据!
牛掰了!这个Python库有点逆天了,竟然能把图片,视频无损清晰放大!
点这里,获取一大波福利

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326831946&siteId=291194637