使用opencv进行视频解析,获取每一帧图像后需要对该帧图像进行模块匹配进行水印图像查询。
(对多个视频操作,查询到水印的视频存储到list中等待下一步处理,未查询到的视频存储到失败list中)
logger.info('开始进行视频图像处理...')
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('开始进行每一帧图像处理!')
// 通过opencv中模块匹配进行判断该帧是否存在水印
result = self.find_watermark(frame, watermark_path)
if result == 1:
self.deal_list.append(watermark_path, video_path)
video.release()
logger.info('视频匹配成功!跳出循环!')
return True
if index == 600:
logger.error('600帧均未找到视频水印!' + "路径:" + str(video_path))
video.release()
return False
success, frame = video.read()
index = index + 1
self.no_watermark_list.append(video_info)
logger.error('视频处理失败!没有找到帧图像!' + str(success) + "路径:" + str(video_path))
logger.info('开始进行帧图像模块匹配...')
template = cv2.imread(watermark_path)
// 进行模块匹配
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
// 指定一个阈值
threshold = 0.8
// 将匹配区域的坐标存储在numpy数组中
loc = np.where(res >= threshold)
x = loc[0]
y = loc[1]
if len(x) and len(y):
for pt in zip(*loc[::-1]):
// 存储找到水印的坐标方便ffmpeg进行水印去除操作
self.watermark_index_left = pt[0]
self.watermark_index_top = pt[1]
logger.info('帧图像模块匹配成功!left:' + str(pt[0]) + ",top:" + str(pt[1]))
return True
else:
logger.error('帧图像模块匹配失败!继续重试!')
return False
ffmpeg需要在本地环境安装,windows安装自行搜索,安装后需配置环境变量!
logger.info('ffmpeg开始处理单个视频水印... 视频路径:' + video_path)
try:
// ffmpeg中去除水印,需要提供视频路径,水印在视频中坐标以及水印宽高
text = 'ffmpeg -i \"%s\" -vf "delogo=x=%s:y=%s:w=%s:h=%s:show=0" -c: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处理单个视频水印失败! 视频路径:' + video_path)
return False
logger.info('ffmpeg处理单个视频水印成功! 视频路径:' + video_path)
return True
except Exception as e:
logger.error('ffmpeg处理单个视频水印出现异常! 视频路径:' + video_path + ";异常原因:" + str(e))
self.no_watermark_list.append(video_path)
return False
该处代码不全,具体创建可参考文章https://blog.csdn.net/qq_35037977/article/details/78166340
self.OpenLabel = Label(self, text="视频路径:")
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="选择视频路径", command=self.selectOpenVideoPath)
self.OpenButton.grid(row=0, column=2)
self.OpenMarkLabel = Label(self, text="水印路径:")
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="选择水印路径", command=self.selectOpenMarkPath)
self.OpenMarkButton.grid(row=1, column=2)
pyinstaller.exe -D -w --add-binary C:\Users\wjz\Anaconda3\envs\spiderTest\Lib\site-packages\cv2\opencv_ffmpeg410_64.dll;. E:\video\pyexe.py
原文参考:
[1]: https://blog.csdn.net/weixin_39000819/article/details/80942423
[2]: https://blog.csdn.net/yukinoai/article/details/88366234
[3]: https://blog.csdn.net/qq_35037977/article/details/78166340