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 use python+opencv to parse a video and process the watermark in the video

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use python+opencv to parse videos and deal with watermarks in videos", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use python+opencv to parse videos and deal with watermarks in videos.

1. Use python+opencv to parse video

Opencv is used for video parsing. After obtaining each frame image, it is necessary to perform module matching on the frame image to query the watermark image.

(for multiple video operations, videos queried with watermarks are stored in list for further processing, and unqueried videos are stored in failed list)

Logger.info ('start video image processing.')

Watermark_path = "G:\\ video\\ watermark\ test.png"

Video_path = "G:\\ video\\ video.mp4"

Video = cv2.VideoCapture (video_path)

Index = 0

Success, frame = video.read ()

While success:

Logger.info ('start processing every frame!')

/ / determine whether there is a watermark in the frame by module matching in opencv

Result = self.find_watermark (frame, watermark_path)

If result = = 1:

Self.deal_list.append (watermark_path, video_path)

Video.release ()

Logger.info ('video match succeeded! Jump out of the cycle!')

Return True

If index = = 600:

Logger.error (no video watermark was found in '600 frames!' + "path:" + str (video_path))

Video.release ()

Return False

Success, frame = video.read ()

Index = index + 1

Self.no_watermark_list.append (video_info)

Logger.error ('video processing failed! No frame image found!'+ str (success) + "path:" + str (video_path))

two。 Use opencv+numpy to query watermarks

Logger.info ('start frame image module matching.')

Template = cv2.imread (watermark_path)

/ / perform module matching

Res = cv2.matchTemplate (img, template, cv2.TM_CCOEFF_NORMED)

/ / specify a threshold

Threshold = 0.8

/ / store the coordinates of the matching area in the numpy array

Loc = np.where (res > = threshold)

X = loc [0]

Y = loc [1]

If len (x) and len (y):

For pt in zip (* loc [::-1]):

/ / storing the coordinates of the watermark to facilitate ffmpeg to remove the watermark

Self.watermark_index_left = pt [0]

Self.watermark_index_top = pt [1]

Logger.info ('frame image module matches successfully! left:' + str (pt [0]) + ", top:" + str (pt [1]))

Return True

Else:

Logger.error ('frame image module matching failed! Continue to try again!')

Return False

3. Using ffmpeg to deal with watermark in video

Ffmpeg needs to be installed in the local environment, windows installation searches on its own, and environment variables need to be configured after installation!

Logger.info ('ffmpeg starts processing a single video watermark... video path:' + video_path)

Try:

/ / to remove the watermark in ffmpeg, you need to provide the video path, the watermark coordinates in the video and the watermark width and height.

Text = 'ffmpeg-I\ "% s\"-vf "delogo=x=%s:y=%s:w=%s:h=%s:show=0"-CMV a copy\ "% s\"-y'% (

Video_path, watermark_left, watermark_top, watermark_width, watermark_height, out_video_path)

Res = os.system (text)

If res! = 0:

Self.no_watermark_list.append (video_path)

Logger.error ('ffmpeg failed to process a single video watermark! video path:' + video_path)

Return False

Logger.info ('ffmpeg successfully processed a single video watermark! video path:' + video_path)

Return True

Except Exception as e:

Logger.error ('ffmpeg failed to handle a single video watermark! video path:' + video_path + "; cause of exception:" + str (e))

Self.no_watermark_list.append (video_path)

Return False Wuxi Gynecology Hospital which is a good http://www.xasgfk.cn/

4. Using tkinter to build a graphical interface

The code here is incomplete. For specific creation, please refer to other articles.

Self.OpenLabel = Label (self, text= "video path:")

Self.OpenLabel.grid (row=0, column=0)

Self.OpenEntry = Entry (self, textvariable=self.openVideoPath, width=45)

Self.OpenEntry.grid (row=0, column=1)

Self.OpenButton = Button (self, text= "choose a video path", command=self.selectOpenVideoPath)

Self.OpenButton.grid (row=0, column=2)

Self.OpenMarkLabel = Label (self, text= "watermark path:")

Self.OpenMarkLabel.grid (row=1, column=0)

Self.OpenMarkEntry = Entry (self, textvariable=self.openMarkPath, width=45)

Self.OpenMarkEntry.grid (row=1, column=1)

Self.OpenMarkButton = Button (self, text= "choose watermark path", command=self.selectOpenMarkPath)

Self.OpenMarkButton.grid (row=1, column=2)

5. Use pyinstaller to package applications

Install pyinstaller: pip install pyinstaller

Go to Lib in the python environment and find pyinstaller, run cmd in that directory, and execute the following command

Pyinstaller.exe-D-w-- add-binary C:\ Users\ wjz\ Anaconda3\ envs\ spiderTest\ Lib\ site-packages\ cv2\ opencv_ffmpeg410_64.dll;. E:\ video\ pyexe.py

Description:

1)-D means that the generated result is a directory where various third-party dependencies, resources, and exe are stored at the same time.

2)-w means to display the graphical interface directly without generating a black window.

3)-add-binary is followed by the dll file path of opencv. If you do not add the possible opencv version, there will be a problem.

4) finally, specify the path of the py file to be packaged

After execution, three files build,dist,pyexe.spec (specified file name) will be generated, and the main program pyexe.exe entry is in the dist folder.

Find the exe file and execute it.

At this point, I believe you have a deeper understanding of "how to use python+opencv to parse video and deal with the watermark in video". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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