Created
May 29, 2022 21:38
-
-
Save Stankye/f18a39bd46f962ed24d7efe344e01b1e to your computer and use it in GitHub Desktop.
Tesserocr Pillow Opencv
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
import cv2 as cv | |
from tesserocr import PyTessBaseAPI | |
from PIL import Image | |
def main(): | |
CAPTURE_SOURCE = 1 | |
FRAME_WIDTH = 911 | |
FRAME_HEIGHT = 1621 | |
SAVE_PATH = "../roi" | |
cap = cv.VideoCapture(1) | |
cap.set(cv.CAP_PROP_FRAME_WIDTH, FRAME_WIDTH) | |
cap.set(cv.CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT) | |
if not cap.isOpened(): | |
print("[x] Cannot open VirtualCamera") | |
exit() | |
try: | |
while True: | |
ret, frame = cap.read() | |
if not ret: | |
print("[x] Can't receive frame. Exiting ...") | |
exit() | |
cv.namedWindow("ROI", cv.WINDOW_NORMAL) | |
roi = cv.selectROI("ROI", frame) | |
print(f"[+] ROI: {roi}") | |
pillow_image_frame = Image.fromarray(frame) | |
pillow_image_frame.save("./frame_x.png", format="PNG") | |
with PyTessBaseAPI(path="C:\\Program Files\\Tesseract-OCR\\tessdata") as api: | |
x1=roi[0] | |
y1=roi[1] | |
x2=roi[2] | |
y2=roi[3] | |
img_crop = frame[y1:y1+y2,x1:x1+x2] | |
pillow_image_crop = Image.fromarray(img_crop) | |
pillow_image_crop.save("./frame_roi_x.png", format="PNG") | |
api.SetImage(pillow_image_crop) | |
print("[+] OCR: \n") | |
print(api.GetUTF8Text()) | |
print("\n -------------------") | |
except KeyboardInterrupt: | |
cap.release() | |
cv.destroyAllWindows() | |
if __name__ == "__main__": | |
main() | |
cv.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment