Hướng dẫn how do you search text on screen in python? - làm thế nào để bạn tìm kiếm văn bản trên màn hình trong python?

Tôi đang sử dụng MSS kết hợp với pytesseract để thử và đọc trên màn hình để xác định một chuỗi các ký tự từ khu vực được theo dõi. Mã của tôi như sau:

import Image
import pytesseract
import cv2
import os
import mss
import numpy as np

with mss.mss() as sct:
    mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}

    im = sct.grab(mon)
    im = np.asarray(im)
    im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    #im_gray = plt.imshow(im_gray, interpolation='nearest')

    cv2.imwrite("test.png", im_gray)
    #cur_dir = os.getcwd()
    text = pytesseract.image_to_string(Image.open(im_gray))
    print(text)

    cv2.imshow("Image", im)
    cv2.imshow("Output", im_gray)
    cv2.waitKey(0)

Và tôi được trả về với lỗi sau: AttributionError: 'numpy.ndarray' đối tượng không có thuộc tính 'đọc'

Tôi cũng đã thử chuyển đổi nó trở lại một hình ảnh bằng pyplot như được chỉ ra bởi dòng nhận xét trong mẫu mã. Tuy nhiên, in lại lỗi: Kiểu hàng: IMG không phải là một mảng numpy, không phải là vô hướng

Tôi hơi mới với Python (chỉ bắt đầu say mê nó vào Chủ nhật). Tuy nhiên, tôi đã khá thành công với những nỗ lực khác của mình trong việc phát hiện hình ảnh. Nhưng, để đạt được mục tiêu cuối cùng của tôi, tôi sẽ cần có thể đọc các nhân vật trên màn hình. Chúng sẽ là cùng một phông chữ và cùng kích thước, nhất quán vì vậy tôi không phải lo lắng về việc mở rộng các vấn đề, nhưng trong thời gian tôi đang cố gắng hiểu nó hoạt động như thế nào bằng cách lưu trữ hình ảnh trong bộ nhớ (mà không lưu vào tệp) Từ biểu tượng thùng rác trên máy tính để bàn và cố gắng lấy chuỗi "Bin tái chế" từ hình ảnh.

Cập nhật Tôi nghĩ rằng tôi có thể có một số bước đột phá, nhưng nếu tôi cố gắng hiển thị luồng cùng một lúc, có một số vấn đề. Tuy nhiên, tôi có thể xử lý luồng đủ nhanh bằng cách sử dụng các tệp tạm thời. I think I may have some breakthrough, but if I am tryin to display the stream at the same time, there is some issues. However, I may be able to process the stream fast enough by using temporary files.

Mã cập nhật của tôi như sau:

from PIL import Image
from PIL import ImageGrab
import pytesseract
import cv2
import os
import mss
import numpy as np
from matplotlib import pyplot as plt
import tempfile

png = tempfile.NamedTemporaryFile(mode="wb")

with mss.mss() as sct:
    #while True:
    mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
    im = sct.grab(mon)
    im_array = np.asarray(im)
    #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    #with tempfile.NamedTemporaryFile(mode="wb") as png:
    png.write(im_array)
    im_name = png.name
    print(png.name)

    #cv2.imwrite("test.png", im_gray)
    #cur_dir = os.getcwd()
    #text = pytesseract.image_to_string(Image.open(im_name))
    #print(text)
    cv2.imshow("Image", im_array)
#cv2.imshow("Output", im_gray)
cv2.waitKey(0)

Điều này hiện đang phun ra một sự cho phép bị từ chối lỗi, như sau:

File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
    text = pytesseract.image_to_string(Image.open(im_name))
  File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
    fp = builtins.open(filename, "rb")
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'

Tôi hoài nghi rằng điều này là bình thường, và tôi sẽ thử bản cập nhật này trên máy tính xách tay của mình ở nhà. Nó có thể là do những hạn chế đối với máy tính xách tay làm việc mà tôi không có thời gian để làm việc xung quanh.

Tôi khá bối rối tại sao hiển thị hình ảnh mà không có sự thật: vòng lặp hoạt động tốt, như một ảnh chụp màn hình. Tuy nhiên, đặt nó trong một thời gian đúng: Vòng lặp khiến cửa sổ đóng băng.

Sử dụng CV2. ....

def find_image_text_location(text='photosynthesis', 
                             img='science-textbook-page-101.jpg'):
    # Function would do something here
    return x, y #coodinates where text is found on the screen.

Tôi thậm chí sẽ không biết nơi để tìm thông tin này và Google không hữu ích.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Example:

    Bàn luận = “GEEK FOR GEEKS”
    Input: “FOR” 
    Output: Yes, FOR is present in the given string.

    Tệp văn bản để trình diễn:

    myfile.txt

    Phương pháp 1: & nbsp; Tìm chỉ mục của chuỗi trong tệp văn bản bằng readline ()

    Trong phương thức này, chúng tôi đang sử dụng hàm readline () và kiểm tra hàm find (), phương thức này trả về -1 nếu không tìm thấy giá trị và nếu thấy nó trả về 0.

    Python3

    with open(r

    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    0
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    1
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    2
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    3

    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    4
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    5
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    6
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    7

    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    4
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    9
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    0
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    1
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    2

    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    3
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    4
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    6
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    6

    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    3
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    8
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    9
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    6
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    1
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    2
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    3

    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    4
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    5
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    6
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    7
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    8

    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    4
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    5
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    6
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    2
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    3

    Output:

    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1

    Phương pháp 2: Tìm chuỗi trong tệp văn bản bằng Read ()

    Chúng tôi sẽ tìm kiếm chuỗi từng dòng nếu chuỗi được tìm thấy thì chúng tôi sẽ in số chuỗi và số đó bằng hàm read ().

    Python3

    with open(r

    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    0
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    1
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    2
    string does not exist
    0
    string does not exist
    1
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    3

    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    3
    string does not exist
    4
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    6
    string does not exist
    1
    string does not exist
    7

    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    3
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    8
    string does not exist in a file
    0
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    1
    string does not exist in a file
    2

    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    4
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    5
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    6
    string does not exist in a file
    6
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    8

    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    3
    string does not exist in a file
    9
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    3

    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    4
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    5
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    6with 4
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    8

    Output:

    string does not exist

    Phương pháp 3: Tìm kiếm một chuỗi trong các tệp văn bản bằng cách sử dụng Enumerate ()Search for a String in Text Files using enumerate()

    Chúng tôi chỉ tìm thấy chuỗi có mặt trong tệp hoặc không sử dụng Enumerate () trong Python.

    Python3

    with open(rwith 9

    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    1
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    2open2

    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    4
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    9 open5
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    1 open7open8

    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    3
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    8 (r1
    File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in 
        text = pytesseract.image_to_string(Image.open(im_name))
      File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
        fp = builtins.open(filename, "rb")
    PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'
    
    1 (r3

    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    4
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    5
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    6(r7(r8

    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    4
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    00

    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    4
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    5
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    6
    from PIL import Image
    from PIL import ImageGrab
    import pytesseract
    import cv2
    import os
    import mss
    import numpy as np
    from matplotlib import pyplot as plt
    import tempfile
    
    png = tempfile.NamedTemporaryFile(mode="wb")
    
    with mss.mss() as sct:
        #while True:
        mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
        im = sct.grab(mon)
        im_array = np.asarray(im)
        #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        #with tempfile.NamedTemporaryFile(mode="wb") as png:
        png.write(im_array)
        im_name = png.name
        print(png.name)
    
        #cv2.imwrite("test.png", im_gray)
        #cur_dir = os.getcwd()
        #text = pytesseract.image_to_string(Image.open(im_name))
        #print(text)
        cv2.imshow("Image", im_array)
    #cv2.imshow("Output", im_gray)
    cv2.waitKey(0)
    
    04
    def find_image_text_location(text='photosynthesis', 
                                 img='science-textbook-page-101.jpg'):
        # Function would do something here
        return x, y #coodinates where text is found on the screen.
    
    8

    Output:

    string does not exist in a file

    Làm thế nào để bạn tìm kiếm văn bản trong Python màn hình?

    ImageGrab là một mô -đun Python giúp chụp nội dung của màn hình.PyTesseract là một công cụ nhận dạng ký tự quang học (OCR) cho Python. Cùng nhau, chúng có thể được sử dụng để đọc nội dung của một phần của màn hình. PyTesseract is an Optical Character Recognition(OCR) tool for Python. Together they can be used to read the contents of a section of the screen.

    Làm cách nào để tìm một từ cụ thể trong một tệp văn bản python?

    Phương pháp 1: Tìm chỉ mục của chuỗi trong tệp văn bản bằng cách sử dụng readline () trong phương thức này, chúng tôi đang sử dụng hàm readline () và kiểm tra hàm find (), phương thức này trả về -1 nếu không tìm thấy giá trịvà nếu thấy nó trả về 0.Finding the index of the string in the text file using readline() In this method, we are using the readline() function, and checking with the find() function, this method returns -1 if the value is not found and if found it returns 0.

    Làm thế nào để bạn tìm kiếm một dòng trong một tệp văn bản trong Python?

    Tải tệp văn bản vào chương trình Python để tìm chuỗi đã cho trong tệp.Yêu cầu người dùng nhập chuỗi mà bạn muốn tìm kiếm trong tệp.Đọc dòng tệp văn bản từng dòng bằng cách sử dụng hàm readlines () và tìm kiếm chuỗi.Sau khi tìm thấy chuỗi, in toàn bộ dòng đó và tiếp tục tìm kiếm.Read the text file line by line using readlines() function and search for the string. After finding the string, print that entire line and continue the search.

    Làm thế nào để bạn sử dụng pytesseract trong python?

    Mở một hình ảnh đơn giản..
    Nhập CV2 ..
    Nhập pytesseract ..
    Lưu hình ảnh thử nghiệm trong cùng một thư mục ..
    Tạo một biến để lưu trữ hình ảnh bằng CV2.hàm imread () và chuyển tên của hình ảnh làm tham số ..
    Để thay đổi kích thước hình ảnh, sử dụng CV2.Thay đổi kích thước () hàm và vượt qua độ phân giải cần thiết ..
    Sử dụng CV2.....
    Thêm CV2 ..