Showing
1 changed file
with
125 additions
and
0 deletions
Prj/main-area.py
0 → 100644
1 | +"""Detects text in the file.""" | ||
2 | +from google.cloud import vision | ||
3 | + | ||
4 | +import io | ||
5 | +import os | ||
6 | +import cv2 | ||
7 | +import numpy as np | ||
8 | + | ||
9 | +smokingZone = 0 | ||
10 | + | ||
11 | +def stackImages(scale,imgArray): #calculate convolution | ||
12 | + rows = len(imgArray) | ||
13 | + cols = len(imgArray[0]) | ||
14 | + rowsAvailable = isinstance(imgArray[0], list) | ||
15 | + width = imgArray[0][0].shape[1] | ||
16 | + height = imgArray[0][0].shape[0] | ||
17 | + if rowsAvailable: | ||
18 | + for x in range ( 0, rows): | ||
19 | + for y in range(0, cols): | ||
20 | + if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]: | ||
21 | + imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale) | ||
22 | + else: | ||
23 | + imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale) | ||
24 | + if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR) | ||
25 | + imageBlank = np.zeros((height, width, 3), np.uint8) | ||
26 | + hor = [imageBlank]*rows | ||
27 | + hor_con = [imageBlank]*rows | ||
28 | + for x in range(0, rows): | ||
29 | + hor[x] = np.hstack(imgArray[x]) | ||
30 | + ver = np.vstack(hor) | ||
31 | + else: | ||
32 | + for x in range(0, rows): | ||
33 | + if imgArray[x].shape[:2] == imgArray[0].shape[:2]: | ||
34 | + imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale) | ||
35 | + else: | ||
36 | + imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale) | ||
37 | + if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR) | ||
38 | + hor= np.hstack(imgArray) | ||
39 | + ver = hor | ||
40 | + return ver | ||
41 | + | ||
42 | +def getContours(img): | ||
43 | + contours,hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) | ||
44 | + for cnt in contours: | ||
45 | + area = cv2.contourArea(cnt) | ||
46 | + print(area) | ||
47 | + if area>500: | ||
48 | + cv2.drawContours(imgContour, cnt, -1, (255, 0, 0), 3) | ||
49 | + peri = cv2.arcLength(cnt,True) | ||
50 | + #print(peri) | ||
51 | + approx = cv2.approxPolyDP(cnt,0.02*peri,True) | ||
52 | + print(len(approx)) | ||
53 | + objCor = len(approx) | ||
54 | + x, y, w, h = cv2.boundingRect(approx) | ||
55 | + | ||
56 | + if objCor ==3: objectType ="Tri" | ||
57 | + elif objCor == 4: | ||
58 | + aspRatio = w/float(h) | ||
59 | + if aspRatio >0.98 and aspRatio <1.03: objectType= "Square" | ||
60 | + else:objectType="Rectangle" | ||
61 | + elif objCor>4: objectType= "Sign" | ||
62 | + else:objectType="None" | ||
63 | + | ||
64 | + | ||
65 | + cv2.rectangle(imgContour,(x,y),(x+w,y+h),(0,255,0),2) | ||
66 | + cv2.putText(imgContour,objectType, | ||
67 | + (x+(w//2)-10,y+(h//2)-10),cv2.FONT_HERSHEY_COMPLEX,0.7, | ||
68 | + (0,0,0),2) | ||
69 | + | ||
70 | + if objectType == "Sign": | ||
71 | + smokingZone = 1 | ||
72 | + | ||
73 | +os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="C:/Users/User/Downloads/SmokeDetection-ab1773dcbc6a.json" | ||
74 | +client = vision.ImageAnnotatorClient() | ||
75 | +path = 'TestPhoto/test08.jpg' | ||
76 | + | ||
77 | +with io.open(path, 'rb') as image_file: | ||
78 | + content = image_file.read() | ||
79 | + | ||
80 | +image = vision.Image(content=content) | ||
81 | + | ||
82 | +price_candidate = [] | ||
83 | +card_number_candidate = [] | ||
84 | +date_candidate = [] | ||
85 | + | ||
86 | +response = client.text_detection(image=image) | ||
87 | +texts = response.text_annotations | ||
88 | +print('Texts:') | ||
89 | + | ||
90 | +for text in texts: | ||
91 | + content = text.description | ||
92 | + content = content.replace(',','') | ||
93 | + print('\n"{}"'.format(content)) | ||
94 | + | ||
95 | + if content == 'SMOKING' or content == "NO": | ||
96 | + smokingZone = 1 | ||
97 | + print("This is No Smoking Zone") | ||
98 | + | ||
99 | + | ||
100 | +if response.error.message: | ||
101 | + raise Exception( | ||
102 | + '{}\nFor more info on error messages, check: ' | ||
103 | + 'https://cloud.google.com/apis/design/errors'.format( | ||
104 | + response.error.message)) | ||
105 | + | ||
106 | + | ||
107 | +img = cv2.imread(path) | ||
108 | +imgContour = img.copy() | ||
109 | + | ||
110 | +imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | ||
111 | +imgBlur = cv2.GaussianBlur(imgGray,(7,7),1) # Gaussian smoothing filter for noise(고주파) reduce | ||
112 | +imgCanny = cv2.Canny(imgBlur,50,50) | ||
113 | +getContours(imgCanny) | ||
114 | + | ||
115 | +if smokingZone == 1: | ||
116 | + imgBlank = cv2.imread('TestPhoto/detected.png') | ||
117 | +else: | ||
118 | + imgBlank = np.zeros_like(img) | ||
119 | + | ||
120 | +imgStack = stackImages(0.6,([img,imgGray,imgBlur], | ||
121 | + [imgCanny,imgContour,imgBlank])) | ||
122 | + | ||
123 | +cv2.imshow("Stack", imgStack) | ||
124 | + | ||
125 | +cv2.waitKey(0) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment