Amazing-Python-Scripts

Форк
0
/
Realtime Text Extraction.py 
51 строка · 1.4 Кб
1
# importing required libraries
2

3
import cv2
4
import pytesseract
5
from PIL import Image
6

7
# Put the path where you stored the tesseract.exe file in your machine
8
pytesseract_file = input(
9
    "Enter the path where you stored the tesseract.exe file\n")
10
pytesseract.pytesseract.tesseract_cmd = pytesseract_file
11

12

13
cam = cv2.VideoCapture(0)  # Starting your webcam
14

15
while True:
16
    ret, img = cam.read()  # Capturing the image
17
    # Can convert colour image to grayscale
18
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
19

20
    cv2.imshow("Original", img)
21
    # Removing noise from the image
22
    ret, thresh = cv2.threshold(
23
        gray, 1010, 200, cv2.THRESH_OTSU, cv2.THRESH_BINARY)
24
    cv2.imshow("After removing noise", thresh)
25

26
    if not ret:
27
        break
28

29
    k = cv2.waitKey(1)  # Taking input from you
30

31
    if k % 256 == 27:  # Press Esc for exit
32
        # For Esc key
33
        print("Close")
34
        break
35

36
    elif k % 256 == 32:  # Press Space for capture the image
37
        # For Space key
38
        print("Image saved")
39
        # Put the path where you want to store the captured image in your machine
40
        path = input("Enter the path where you want to store the image\n ")
41
        path = path+'\img.jpg'
42
        cv2.imwrite(path, thresh)
43
        break
44

45
src = Image.open(path)
46

47
text = pytesseract.image_to_string(src)  # Extracting the text from image
48
print(text)
49

50
cam.release
51
cv2.destroyAllWindows
52

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.