I want to extract the text from the video data. I can extract the text but facing some problems. I am getting repetitive texts from the video. I want to extract the exact text from the whole video. How could I solve my problem? I am sharing my code.
import cv2
import os
import pytesseract
import numpy as np
import time
# Mention the installed location of Tesseract-OCR in your system
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
FRAME_RATE = 2
# Read the video from specified path
video_data = cv2.VideoCapture(r"jonty.mp4")
video_length= int(video_data.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
fps=int(video_data.get(cv2.CAP_PROP_FPS))
pos_frame= int(video_data.get(cv2.CAP_PROP_POS_FRAMES))
width = int(video_data.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video_data.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_time = 0
frame_count = 0
print("total_frames: ", video_length)
print("FRAME_RATE", FRAME_RATE)
print("fps", fps)
print("pos_frame", pos_frame)
text_data=[]
while(True):
video_data.set(cv2.CAP_PROP_POS_MSEC, frame_time * 10000) # move frame to a timestamp
frame_time += 1/FRAME_RATE
# reading from frame
ret,frame = video_data.read()
if ret:
# if video is still left continue creating images
image = r'\images\frame' + str(frame_count) + '.jpg
# writing the extracted images
cv2.imwrite(image, frame)
# Apply OCR on the cropped image
text = pytesseract.image_to_string(image,lang='eng+deu+fra')
#print(text)
text_data.append(text)
# frame in every 1 second
frame_count += 1
else:
break
# Release all space and windows once done
video_data.release()
cv2.destroyAllWindows()