-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathveresys.py
More file actions
228 lines (151 loc) · 6.32 KB
/
veresys.py
File metadata and controls
228 lines (151 loc) · 6.32 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import threading, time, sys
import cv2
import os
from os import path
import requests
import base64
import json
#--------------------------------OpenALPR.py------------------------------------------
def recog(name):
IMAGE_PATH = name
#print('Reading: ' + name)
SECRET_KEY = 'sk_DEMODEMODEMODEMODEMODEMO'
with open(IMAGE_PATH, 'rb') as image_file:
img_base64 = base64.b64encode(image_file.read())
print("Sending to API: " + name)
url = 'https://api.openalpr.com/v2/recognize_bytes?recognize_vehicle=1&country=us&secret_key=%s' % (SECRET_KEY)
r = requests.post(url, data = img_base64)
mydict = json.dumps(r.json(), indent=2)
d = json.loads(mydict)
vehicles = []
for i in range(0,len(d["results"])):
print("Details for: " + name)
print("Car#" + str(i+1))
print("Plate No: " + d["results"][i]["plate"])
print("Color: " + d["results"][i]["vehicle"]["color"][0]["name"])
print("Make: " + d["results"][i]["vehicle"]["make"][0]["name"])
print("Body Type: " + d["results"][i]["vehicle"]["body_type"][0]["name"])
print("Year: " + d["results"][i]["vehicle"]["year"][0]["name"])
print("Model: " + d["results"][i]["vehicle"]["make_model"][0]["name"])
print("\n")
vehicles.append((d["results"][i]["plate"],
d["results"][i]["vehicle"]["make"][0]["name"],
d["results"][i]["vehicle"]["body_type"][0]["name"],
d["results"][i]["vehicle"]["year"][0]["name"],
d["results"][i]["vehicle"]["make_model"][0]["name"]))
print(vehicles)
#--------------------------------VidCam.py------------------------------------------
def make_and_send_frame1(fileName, frame_rate):
cam = cv2.VideoCapture(fileName)
currentframe = 0
while(True):
for i in range(frame_rate):
ret,frame = cam.read()
if ret:
# if video is still left continue creating images
name = './data/frame' + str(currentframe) + '.jpg'
#print ('Creating...' + name)
# writing the extracted images
cv2.imwrite(name, frame)
threading.Thread(target = recog, args=(name,)).start()
currentframe += 1
else:
break
cam.release()
cv2.destroyAllWindows()
def startVidCam(fileName, frame_rate):
try:
# creating a folder named data
if not os.path.exists('data'):
os.makedirs('data')
# if not created then raise error
except OSError:
print ('Error: Creating directory of data')
threading.Thread(target = make_and_send_frame1, args=(fileName, frame_rate,)).start()
#--------------------------------PhoneCam.py------------------------------------------
def make_and_send_frame2(url, delay):
currentframe = 0
while(True):
imgResp = urllib.request.urlopen(url)
imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8)
frame = cv2.imdecode(imgNp, -1)
# reading from frame
name = './data/frame' + str(currentframe) + '.jpg'
#print('Creating...' + name)
# writing the extracted images
cv2.imwrite(name, frame)
threading.Thread(target = recog, args=(name,)).start()
currentframe += 1
time.sleep(delay)
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
def startPhoneCam(url, delay):
cam = cv2.VideoCapture(0)
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
try:
# creating a folder named data
if not os.path.exists('data'):
os.makedirs('data')
# if not created then raise error
except OSError:
print ('Error: Creating directory of data')
threading.Thread(target = make_and_send_frame2, args=(url,delay,)).start()
#--------------------------------WebCam.py------------------------------------------
'''
# Read the video from specified path
cam = cv2.VideoCapture(0)
try:
# creating a folder named data
if not os.path.exists('data'):
os.makedirs('data')
# if not created then raise error
except OSError:
print ('Error: Creating directory of data')
# frame
currentframe = 0
while(True):
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# reading from frame
ret,frame = cam.read()
if ret:
# if video is still left continue creating images
name = './data/frame' + str(currentframe) + '.jpg'
print ('Creating...' + name)
# writing the extracted images
cv2.imwrite(name, frame)
# increasing counter so that it will
# show how many frames are created
currentframe += 1
o(name)
else:
break
time.sleep(1)
# Release all space and windows once done
cam.release()
cv2.destroyAllWindows()
'''
#--------------------------------main.py-----------------------------------------
def action(option,arg1,arg2):
if option == 0:
fileName = arg1
threading.Thread(target = recog, args=(fileName,)).start()
elif option == 1:
fileName = arg1
frame_rate = arg2 #in fps
#startVidCam(fileName,frame_rate)
threading.Thread(target = startVidCam, args=(fileName,frame_rate,)).start()
elif option == 2:
url = arg1
delay = arg2 #in seconds
#startPhoneCam(url, delay)
threading.Thread(target = startPhoneCam, args=(url,delay,)).start()
elif option == 3:
delay = arg1 #in seconds
threading.Thread(target = startWebCam, args=(delay,)).start()
else:
print("Enter correct arguments!")
#action(1, "sample vid.mp4", 60)