Created
February 27, 2024 11:27
-
-
Save lMortimerl/4fb782533a151c6e30d8d790ab0d5804 to your computer and use it in GitHub Desktop.
Python Image to Text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dependencies: pip install opencv-python-headless pytesseract numpy | |
import cv2 | |
import pytesseract | |
import matplotlib.pyplot as plt | |
# Path to Tesseract executable (change this according to your installation) | |
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe' | |
def capture_and_parse_text(): | |
while True: | |
# Activate camera | |
cap = cv2.VideoCapture(0) | |
if not cap.isOpened(): | |
print("Error: Could not open camera.") | |
return | |
# Capture a frame | |
ret, frame = cap.read() | |
if not ret: | |
print("Error: Failed to capture image.") | |
cap.release() | |
return | |
# Show preview of the captured image using matplotlib | |
plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) | |
plt.axis('off') | |
plt.title('Captured Image') | |
plt.show() | |
# Save the captured image | |
image_path = "captured_image.jpg" | |
cv2.imwrite(image_path, frame) | |
print("Image captured successfully.") | |
# Release the camera | |
cap.release() | |
# Parse text from the captured image | |
parsed_text = parse_text_from_image(image_path) | |
print("Parsed text from image:") | |
print(parsed_text) | |
# Ask user if they want to retry parsing | |
retry = input("Do you want to retry parsing the image? (y/n): ") | |
if retry.lower() != 'y': | |
break | |
def parse_text_from_image(image_path): | |
# Read the image using OpenCV | |
img = cv2.imread(image_path) | |
# Convert image to grayscale | |
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
# Use pytesseract to perform OCR | |
parsed_text = pytesseract.image_to_string(img, lang="deu") | |
return parsed_text.replace("\n\n", "").replace("\n", " ") | |
if __name__ == "__main__": | |
capture_and_parse_text() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment