import easyocr import cv2 as cv from PIL import Image video_file = cv.VideoCapture(r"f4459201ee68667a36dee475fe96159c.mp4") video_fps=video_file.get(cv.CAP_PROP_FPS) print(video_file.get(cv.CAP_PROP_FPS)) total_frames = int(video_file.get(cv.CAP_PROP_FRAME_COUNT)) image_size = (int(video_file.get(cv.CAP_PROP_FRAME_HEIGHT)), int(video_file.get(cv.CAP_PROP_FRAME_WIDTH))) frames_height,frames_weight=image_size[0],image_size[1] print(frames_height) count_frame_start=0 count_frame_end=0 thresh = 220# Set threshold To binarize temporary_frame=[] reader=easyocr.Reader(["ch_sim","en"],gpu=False)# For text recognition """ The following functions calculate the difference between subtitles Whether the subtitles are the same It's equivalent to from nothing -----> Yes Calculate each image and 0 Value image error return ((img - imgo) ** 2).sum() / img.size * 100 Can be added to the code , Then it becomes your own code Yes -----> change Then calculate the direct error of the same caption and different caption images """ def cal_video(img, imgo=None): return ((img - imgo) ** 2).sum() / img.size * 100 while True: success, frames = video_file.read() print(" Open the first {} frame ".format(count_frame_start)) frames_cut = frames[:, :, 0]#[(486, 864)] frames_wh_cut = frames_cut[frames_height-75: frames_height-6, :] _, frames_threshold = cv.threshold(frames_wh_cut, 220, 255, cv.THRESH_BINARY) temporary_frame.append(frames_threshold) if count_frame_start>1: del temporary_frame[0] if cal_video(temporary_frame[1],temporary_frame[0])>2: print(" The program runs ! The first {} frame ".format(count_frame_start)) result = reader.readtext(frames_wh_cut) if len(result)>0: f=open(" The Goddess Chang's fly to the moon .txt", "a", encoding="utf-8") f.write(str(count_frame_end) + "--->" + str(count_frame_start)+"\n") f.write(str(result[0][1])+"\n") f.close() count_frame_end = count_frame_start count_frame_start+=1 #cv.imwrite(r"D:\PycharmProjects\pythonProject\feiji\image{}.jpg".format(count_frame_start),frames_wh_cut) else: continue else: pass count_frame_start+=1
Please pay attention to the following points when running the program :
1. Change the video address , Select the video address you need to perform subtitle recognition
2. Binarization of the captured video caption pictures , The threshold can be changed by yourself
3. The threshold value of similar values of adjacent pictures can be changed .
On the whole , There are still some problems in identification , After you run this code, you will find the problem , If anyone can improve the effect , Hope to inform , thank you .