|
1 | | -''' |
2 | | -For using this code your machine had already installed OpenCV, pytesseract and PIL. |
3 | | -For capturing a image press 'SPACE' when web-cam is on. |
4 | | -''' |
5 | 1 | #importing required libraries
|
6 | 2 |
|
7 | 3 | import cv2
|
8 | 4 | import pytesseract
|
9 | 5 | from PIL import Image
|
10 | 6 |
|
11 | 7 | #Put the path where you stored the tesseract.exe file in your machine
|
12 | | -pytesseract.pytesseract.tesseract_cmd = r"C:\Users91769円\AppData\Local\Programs\Tesseract-OCR\tesseract.exe" |
| 8 | +pytesseract_file=input("Enter the path where you stored the tesseract.exe file\n") |
| 9 | +pytesseract.pytesseract.tesseract_cmd = pytesseract_file |
13 | 10 |
|
14 | 11 |
|
15 | 12 | cam = cv2.VideoCapture(0) #Starting your webcam
|
16 | 13 |
|
17 | 14 | while True:
|
18 | 15 | ret, img = cam.read() #Capturing the image
|
19 | | - #img= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) Can convert colour image to grayscale |
| 16 | + gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Can convert colour image to grayscale |
20 | 17 |
|
21 | 18 | cv2.imshow("Original",img)
|
22 | 19 | #Removing noise from the image
|
23 | | - ret, thresh = cv2.threshold(img,1010, 200, cv2.THRESH_OTSU, cv2.THRESH_BINARY) |
| 20 | + ret, thresh = cv2.threshold(gray,1010, 200, cv2.THRESH_OTSU, cv2.THRESH_BINARY) |
24 | 21 | cv2.imshow("After removing noise", thresh)
|
25 | 22 |
|
26 | 23 | if not ret:
|
|
37 | 34 | #For Space key
|
38 | 35 | print("Image saved")
|
39 | 36 | #Put the path where you want to store the captured image in your machine
|
40 | | - path=r'C:\Users91769円\Desktop\images'+'\img.jpg' |
| 37 | + path=input("Enter the path where you want to store the image\n ") |
| 38 | + path=path+'\img.jpg' |
41 | 39 | cv2.imwrite(path, thresh)
|
42 | 40 | break
|
43 | 41 |
|
44 | | -src= Image.open(r"C:\Users91769円\Desktop\images\img.jpg") |
| 42 | +src= Image.open(path) |
45 | 43 |
|
46 | 44 | text= pytesseract.image_to_string(src) #Extracting the text from image
|
47 | 45 | print(text)
|
|
0 commit comments