import cv2 # Load the image img = cv2.imread('image.png') # Convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply thresholding to the grayscale image thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # Find contours in the thresholded image contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Initialize an empty list to store the detected text detected_text = [] # Loop over the contours for contour in contours: # Compute the bounding box of the contour x, y, w, h = cv2.boundingRect(contour) # Extract the ROI from the image roi = img[y:y + h, x:x + w] # Apply OCR to the ROI text = pytesseract.image_to_string(roi) # Add the detected text to the list detected_text.append(text) # Print the detected text print(detected_text)