Amazing-Python-Scripts
51 строка · 1.4 Кб
1# importing required libraries
2
3import cv2
4import pytesseract
5from PIL import Image
6
7# Put the path where you stored the tesseract.exe file in your machine
8pytesseract_file = input(
9"Enter the path where you stored the tesseract.exe file\n")
10pytesseract.pytesseract.tesseract_cmd = pytesseract_file
11
12
13cam = cv2.VideoCapture(0) # Starting your webcam
14
15while True:
16ret, img = cam.read() # Capturing the image
17# Can convert colour image to grayscale
18gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
19
20cv2.imshow("Original", img)
21# Removing noise from the image
22ret, thresh = cv2.threshold(
23gray, 1010, 200, cv2.THRESH_OTSU, cv2.THRESH_BINARY)
24cv2.imshow("After removing noise", thresh)
25
26if not ret:
27break
28
29k = cv2.waitKey(1) # Taking input from you
30
31if k % 256 == 27: # Press Esc for exit
32# For Esc key
33print("Close")
34break
35
36elif k % 256 == 32: # Press Space for capture the image
37# For Space key
38print("Image saved")
39# Put the path where you want to store the captured image in your machine
40path = input("Enter the path where you want to store the image\n ")
41path = path+'\img.jpg'
42cv2.imwrite(path, thresh)
43break
44
45src = Image.open(path)
46
47text = pytesseract.image_to_string(src) # Extracting the text from image
48print(text)
49
50cam.release
51cv2.destroyAllWindows
52