"Exploring the Mysteries of Image Processing: Image and Video Processing with Python and OpenCV"

 1. Upload the picture and download it after removing the background. Online matting software_picture background removal | remove.bg – remove.bg

2. Enlarge the downloaded picture by 2 times. ClipDrop - Image upscaler

 3. Edit the enlarged downloaded photos.

 4. Use deepfacelive to change faces.

1) Copy the photos in the third step to the designated folder. C:\myApp\deepfakelivetemp\DeepFaceLive_NVIDIA\userdata\animatables

2) Set the deepfacelive software as follows.

  

3) Write a python program to convert the automatically generated jpg file into a video file.

a) Install the cv2 module.

pip install opencv-python

 b) Source code writing:

import os
import wx
import cv2

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title, size=(300, 200))

        panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)

        self.btn = wx.Button(panel, label='选择文件夹')
        self.btn.Bind(wx.EVT_BUTTON, self.onOpenFolder)
        vbox.Add(self.btn, flag=wx.ALIGN_CENTER|wx.TOP|wx.BOTTOM, border=20)

        panel.SetSizer(vbox)

        self.Show(True)

    def onOpenFolder(self, event):
        dlg = wx.DirDialog(self, '选择文件夹', style=wx.DD_DEFAULT_STYLE)

        if dlg.ShowModal() == wx.ID_OK:
            folder_path = dlg.GetPath()
            print('您选择的文件夹是:{}'.format(folder_path))
            self.createVideo(folder_path)

        dlg.Destroy()

    def createVideo(self, folder_path):
        # 获取文件夹中的所有JPG序列文件
        file_list = os.listdir(folder_path)
        file_list.sort()
        jpg_list = [os.path.join(folder_path, f) for f in file_list if f.endswith('.jpg')]

        # 读取第一张JPG文件获取视频尺寸
        img = cv2.imread(jpg_list[0])
        height, width, _ = img.shape

        # 初始化视频写入器
        video_path = os.path.join(folder_path, 'output.mp4')
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        out = cv2.VideoWriter(video_path, fourcc, 30, (width, height))

        # 逐帧读取JPG文件并写入视频
        for jpg_file in jpg_list:
            img = cv2.imread(jpg_file)
            out.write(img)

        out.release()

        print('视频文件已生成:{}'.format(video_path))

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame(None, 'JPG序列文件合成视频')
    app.MainLoop()

c) Code explanation:

The following is the Python code explanation of "Using Python to write wxPython module: select a folder and combine the JPG sequence files from 001.jpg to 304.jpg into a video file":

First we imported the necessary modules, including os, wx and cv2 modules. The os module is used to handle files and folders, the wx module is used to create GUI applications, and the cv2 module is used to read and write image and video files.

Here we define a wxPython frame class called "MyFrame" and implement two methods in it: __init__ and onOpenFolder. The __init__ method is used to initialize the GUI frame and buttons, and the onOpenFolder method is used to handle the event that the folder selection button is clicked. We also define a method called "createVideo" for merging JPG sequence files into a video file.

Finally, we create an instance of the wxPython application in the main program, and create an instance of the frame called "MyFrame". We put the application main loop on the last line so that the application can run until the user closes the window.

In summary, this Python program uses the wxPython module to create a GUI application that allows the user to select a folder containing JPG sequence files and merge them into one video file. It uses the OpenCV library to read and write image and video files.

d) Results:

Guess you like

Origin blog.csdn.net/winniezhang/article/details/131744536