赞
踩
所安装的python库
pip install opencv-python
pip install Pillow
pip install numpy
图片#灰度界限,大于这个值为黑色,小于这个值为白色
可以使用ps软件使用颜色拾取工具,其中RGB模式下的G值则为灰度值。
全部代码如下:
from cv2 import cv2 as cv from PIL import Image import base64,re from io import BytesIO from aip import AipOcr import time #""" 你的 APPID AK SK """ APP_ID = '百度id' API_KEY = '百度key' SECRET_KEY = '百度secret' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) vedio_contents = [[]] #""" 读取图片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() def words_baidu_read(imagefile): try: image = get_file_content(imagefile) #""" 调用通用文字识别, 图片参数为本地图片 """ jieguo = client.basicGeneral(image) words = jieguo['words_result'][0]['words'] return words except: print('IndexError') #视频地址 video_filename = r'本地视频位置' videoCap = cv.VideoCapture(video_filename) # 帧频 fps = videoCap.get(cv.CAP_PROP_FPS) # 视频总帧数 total_frames = int(videoCap.get(cv.CAP_PROP_FRAME_COUNT)) # 图像尺寸 image_size = (int(videoCap.get(cv.CAP_PROP_FRAME_HEIGHT)), int(videoCap.get(cv.CAP_PROP_FRAME_WIDTH))) #计算视频的时间秒数 video_time = int(total_frames / fps) print(video_time) # print(fps) # print(total_frames) # print(image_size) #获取某一秒的image def get_word_from_vedio(fpsnow): #调用 read() 方法可以读取一帧的图片,当 sucess 为 True 时,读取成功,此时 frame 是图像像素矩阵,三维 numpy 矩阵。 for i in range(fpsnow): sucess, frame = videoCap.read() #确定字幕范围 im = frame[:, :, 0] im = im[950:1050, 100:1800] # 确定字幕的范围,注意不同的视频文件剪切的索引值不同,im[950:1050, 100:1900]分别是高度950-1050范围,宽度100-1900范围,根据视频情况调整 #在进行下一步分析前,需对图像做二值化处理。由于字幕的白色的,像素值为 255,将二值化阈值设为 220。 thresh = 160 _, im = cv.threshold(im, thresh, 255, cv.THRESH_BINARY) img = Image.fromarray(im) # img.show() return im,img #公式计算两张图像间每个像素点的平方误差之和的平均值百分比,当只大于1时出现字幕 def fps_percentage(im): fps_percentage_value = (im ** 2).sum() / im.size * 100 if fps_percentage_value > 1: return True else: return False #计算两张图差异大小值,误差值 > 1 时,字幕发生切换 def im_percentage(im1,im2): im_percentage = ((im2 - im1) ** 2).sum() / im1.size * 100 if im_percentage > 1: return True else: return False def main_work(): for i in range(total_frames): i +=1 im1,img1 = get_word_from_vedio(i) im2,img2 = get_word_from_vedio(i+2) vlue1 = fps_percentage(im1) vlue2 = fps_percentage(im2) if vlue1 and vlue2: print('--------\n都有文字') is_change_word = im_percentage(im1,im2) if is_change_word: print('文字发生变化') temp_pic1 = r'C:\Users\Administrator\desktop\test1.jpg' temp_pic2 = r'C:\Users\Administrator\desktop\test2.jpg' img1.save(temp_pic1) img2.save(temp_pic2) time.sleep(1) words1 = words_baidu_read(temp_pic1) words2 = words_baidu_read(temp_pic2) print('识别文字结果',words1,words2) if words1 in vedio_contents: pass else: vedio_contents.append([i,words1]) if words2 in vedio_contents: pass else: vedio_contents.append([i,words2]) else: print('文字没有变化') print('--------') pass elif vlue1: print('--------\n第1张文字,第2张没有') temp_pic1 = r'C:\Users\Administrator\desktop\test1.jpg' img1.save(temp_pic1) time.sleep(5) words1 = words_baidu_read(temp_pic1) if words1 in vedio_contents: pass else: vedio_contents.append([i,words1]) print('--------') print('识别文字结果',words1) pass elif vlue2: print('--------\n第2张文字,第1张没有') temp_pic2 = r'C:\Users\Administrator\desktop\test2.jpg' img2.save(temp_pic2) time.sleep(5) words2 = words_baidu_read(temp_pic2) if words2 in vedio_contents: pass else: vedio_contents.append([i,words2]) print('--------') print('识别文字结果',words2) pass else: pass main_work() #打印最后获得的list for i in vedio_contents: print(i)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。