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
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]
0from 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]
1from 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]
2from 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]
3from 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]
4from 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]
5from 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]
7from 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]
4from 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'
0File "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'
2File "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'
3File "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'
4from 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'
6File "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'
3File "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'
9from 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.
1def 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.
2def 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.
3def 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.
4def 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.
5def 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.
6def 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.
7def 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.
8def 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.
4def 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.
5def 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 -12
-1 -1 0 string exists in file line Number: 4 -1 -13
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]
0from 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]
1from 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]
2string does not exist0
string does not exist1
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.
3File "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'
3string does not exist4
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 exist1
string does not exist7
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'
3File "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 file0
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 file2
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.
4def 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.
5def 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.
6string does not exist in a file6
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.
8File "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'
3string does not exist in a file9
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.
3def 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.
4def 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.
5def 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
4def 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.
8Output:
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
[r
with
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]
1from 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]
2open
2from 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]
4from 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 open
5File "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 open
7open
8File "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'
3File "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 [r
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'
1 [r
3def 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.
4def 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.
5def 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[r
7[r
8def 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.
4from 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]
00from 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]
4def 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.
5def 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.
6from 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]
04def 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.
8Output:
string does not exist in a file