-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
45 lines (42 loc) · 1.5 KB
/
main.py
File metadata and controls
45 lines (42 loc) · 1.5 KB
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
import cv2
import imutils
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\\tesseract'
image = cv2.imread('21.jpg')
image = imutils.resize(image, width=300)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_image = cv2.bilateralFilter(gray_image, 11, 17, 17)
edged = cv2.Canny(gray_image, 20, 200)
cv2.imshow("edged image", edged)
cv2.waitKey(0)
cnts,new = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
image1=image.copy()
cv2.drawContours(image1,cnts,-1,(0,255,0),3)
cv2.imshow("contours",image1)
cv2.waitKey(0)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True) [:10]
screenCnt = None
image2 = image.copy()
cv2.drawContours(image2,cnts,-1,(0,255,0),3)
cv2.imshow("Top 30 contours",image2)
cv2.waitKey(0)
i=7
for c in cnts:
perimeter = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.016 * perimeter, True)
if len(approx) == 4:
screenCnt = approx
x, y, w, h = cv2.boundingRect(c)
new_img = image[y:y + h, x:x + w]
cv2.imwrite('./' + str(i) + '.png', new_img)
i += 1
break
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("image with detected license plate", image)
cv2.waitKey(0)
Cropped_loc = './7.png'
cv2.imshow("cropped", cv2.imread(Cropped_loc))
plate = pytesseract.image_to_string(Cropped_loc,config='--psm 11', lang='rus')
print("Number plate is:", plate)
cv2.waitKey(0)
cv2.destroyAllWindows()