I have been trying to work on individual frame to extract text from individual frame. But it shows an error of
TypeError: Can’t convert object to ‘str’ for ‘filename’
Here is my code to extract the individual frame
text_data = []
while True:
ret, frame=cap.read()
mask = object_detector.apply(frame)
_, mask = cv2.threshold(mask,254,255,cv2.THRESH_BINARY) #mask the backgroubd to the color ratio of 255 for complete black color
contours,_ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
res = cv2.bitwise_and(frame,frame,mask=mask)
for cnt in contours:
area = cv2.contourArea(cnt)
if area>1000: #detect for the flashes above 1000pixel
cv2.drawContours(frame, [cnt], -1, (0,255,0),2)
text_date.append(getdate(frame)
I am using following function to extract the text
def getdate(frames):
img = cv2.imread(frames)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))
dilation = cv2.dilate(thresh1, rect_kernel, iterations = 1)
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
im2 = img.copy()
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
# Drawing a rectangle on copied image
rect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Cropping the text block for giving input to OCR
cropped = im2[y:y + h, x:x + w]
text = pytesseract.image_to_string(cropped)
return text