-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr.py
48 lines (34 loc) · 1.08 KB
/
ocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import cv2
import numpy as np
import os
from pytesseract import pytesseract
class OCR:
def __init__(self):
self.path = "C:\\Users\\HP\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe"
def extract(self,filename):
try:
pytesseract.tesseract_cmd=self.path
text = pytesseract.image_to_string(filename)
return text
except Exception as e:
print(e)
return "Error"
ocr = OCR()
# ---------OCR-------------
pic = cv2.imread("photos0.jpg")
kernel = np.ones((1,1),np.uint8)
pic= cv2.dilate(pic, kernel, iterations=1)
pic = cv2.erode(pic, kernel, iterations=1)
plate_gray = cv2.cvtColor(pic,cv2.COLOR_BGR2GRAY)
(thresh,imgRoi) = cv2.threshold(plate_gray,127,255,cv2.THRESH_BINARY)
text = ocr.extract(pic)
print(text)
num = []
j = 0
for i in text:
if (ord(i) >= 48 and ord(i) <= 57) or (ord(i) >= 65 and ord(i) <= 90) or (ord(i) >= 97 and ord(i) <= 122):
num.append(i)
j += 1
NumberPlate = ''.join(num)
print(NumberPlate)
#---------