Repository navigation
Expand file tree
/
Copy pathTesting111.py
More file actions
114 lines (89 loc) · 3.8 KB
/
Copy pathTesting111.py
File metadata and controls
114 lines (89 loc) · 3.8 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import cv2
import numpy as np
import pytesseract
#import argparse
#import os
#import urllib
from PIL import Image
#from google.cloud import vision
#from google.cloud.vision import types
img= cv2.imread('frame12.jpg')
#cv2.namedWindow('Original Image',cv2.WINDOW_NORMAL)
#cv2.imshow('Original Image',img)
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#cv2.namedWindow('Gray Converted Image',cv2.WINDOW_NORMAL)
#cv2.imshow('Gray converted Image',img_gray)
noise_removal=cv2.bilateralFilter(img_gray,9,75,75)
#cv2.namedWindow('Noise Removed image',cv2.WINDOW_NORMAL)
#cv2.imshow('Noise removed image',noise_removal)
equal_histogram = cv2.equalizeHist(noise_removal)
#cv2.namedWindow('After histogram ',cv2.WINDOW_NORMAL)
#cv2.imshow('After histogram',equal_histogram)
kernel= cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
morph_image=cv2.morphologyEx(equal_histogram,cv2.MORPH_OPEN,kernel,iterations=15)
#cv2.namedWindow("Morphological opening", cv2.WINDOW_NORMAL)
#cv2.imshow("Morphological opening",morph_image)
sub_morph_image=cv2.subtract(equal_histogram,morph_image)
#cv2.namedWindow("Subtraction image",cv2.WINDOW_NORMAL)
#cv2.imshow("Subtract image",sub_morph_image)
ret,thresh_image=cv2.threshold(sub_morph_image,0,255,cv2.THRESH_OTSU)
#cv2.namedWindow("Image after thresholding",cv2.WINDOW_NORMAL)
#cv2.imshow("Image after thresholding",thresh_image)
canny_image= cv2.Canny(thresh_image,250,255)
#cv2.namedWindow("Image after applying canny",cv2.WINDOW_NORMAL)
canny_image= cv2.convertScaleAbs(canny_image)
kernel=np.ones((3,3),np.uint8)
dilated_image=cv2.dilate(canny_image,kernel,iterations=1)
#cv2.namedWindow("Dilation",cv2.WINDOW_NORMAL)
#cv2.imshow("Dilation",dilated_image)
new,contours,hierarchy =cv2.findContours(dilated_image,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
contours=sorted(contours,key =cv2.contourArea, reverse = True)[:10]
screenCnt = None
for c in contours:
peri = cv2.arcLength(c,True)
approx =cv2.approxPolyDP(c,0.06*peri,True)
if len(approx)==4:
screenCnt = approx
break
final=cv2.drawContours(img,[screenCnt],-1,(0,255,0),3)
#cv2.namedWindow("Image with selected contour",cv2.WINDOW_NORMAL)
#cv2.imshow("Image with selected contour",final)
mask=np.zeros(img_gray.shape,np.uint8)
new_image=cv2.drawContours(mask,[screenCnt],0,255,-1)
new_image=cv2.bitwise_and(img,img,mask=mask)
#cv2.namedWindow("Final Image,",cv2.WINDOW_NORMAL)
#cv2.imshow("Final_image",new_image)
y,cr,cb = cv2.split(cv2.cvtColor(new_image,cv2.COLOR_RGB2YCrCb))
y=cv2.equalizeHist(y)
final_image=cv2.cvtColor(cv2.merge([y,cr,cb]),cv2.COLOR_YCrCb2RGB)
cv2.namedWindow("Enhanced Number Plate",cv2.WINDOW_NORMAL)
cv2.imshow("Enhanced Number plate",final_image)
cv2.imwrite('final.png',final_image)
"""
client=vision.ImageAnnotatorClient()
with io.open(final_image,'rb') as image_file:
content=image_file.read()
image=types.Image(content=content)
response =client.text_detection(image=image)
texts=response.text_annotations
print('Texts:')
for text in texts:
print('\n"{}"'.format(text.description))
vetices =(['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
"""
"""ap=argparse.ArgumentParser()
ap.add_argument('-i',"--image",required=True, help="path to input image to be OCR'd")
ap.add_argument("-p","--preprocess", type=str,default="thresh",
help ="type of preprocessing to be done")
agrs =vars(ap.parse_args())
image=final_image
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
if args["preprocess"]=="thresh":
gray = cv2.threshold(gray,0,255,cv2.THRESH_BINARY |cv2.THRESH_OTSU)[1]
elif args["preprocess"]=="blur":
gray=cv2.medianBlur(gray,3)
filename="{}.png".format(os.getpid())
cv2.imwrite(file.txt,gray)
"""
cv2.waitKey()