-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py.save
87 lines (73 loc) · 2.44 KB
/
test.py.save
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
import numpy as np
import cv2
import pickle
import serial
import time
#############################################
#ser = serial.Serial('/dev/ttyACM1',9600)
ser=serial.Serial('/dev/rfcomm1
',9600)
ser.flush()
time.sleep(4)
frameWidth= 640 # CAMERA RESOLUTION
frameHeight = 480
brightness = 180
threshold = 0.75 # PROBABLITY THRESHOLD
font = cv2.FONT_HERSHEY_SIMPLEX
##############################################
# SETUP THE VIDEO CAMERA
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, brightness)
# IMPORT THE TRANNIED MODEL
pickle_in=open("model_trained.p","rb") ## rb = READ BYTE
model=pickle.load(pickle_in)
def grayscale(img):
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
return img
def equalize(img):
img =cv2.equalizeHist(img)
return img
def preprocessing(img):
img = grayscale(img)
img = equalize(img)
img = img/255
return img
def getCalssName(classNo):
if classNo == 0: return 20
elif classNo == 1: return 30
elif classNo == 2: return 50
elif classNo == 3: return 60
elif classNo == 4: return 70
elif classNo == 5: return 80
#elif classNo == 6: return 'End of Speed Limit 80 km/h'
elif classNo == 7: return 100
elif classNo == 8: return 120
while True:
# READ IMAGE
success, imgOrignal = cap.read()
# PROCESS IMAGE
img = np.asarray(imgOrignal)
img = cv2.resize(img, (32, 32))
img = preprocessing(img)
cv2.imshow("Processed Image", img)
img = img.reshape(1, 32, 32, 1)
cv2.putText(imgOrignal, "CLASS: " , (20, 35), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
cv2.putText(imgOrignal, "PROBABILITY: ", (20, 75), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
# PREDICT IMAGE
predictions = model.predict(img)
classIndex = model.predict_classes(img)
probabilityValue =np.amax(predictions)
if probabilityValue > threshold:
#print(getCalssName(classIndex))
cv2.putText(imgOrignal,str(classIndex)+" "+ "Speed Limit" + str(getCalssName(classIndex)) +" Km/h", (120, 35), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
cv2.putText(imgOrignal, str(round(probabilityValue*100,2) )+"%", (180, 75), font, 0.75, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow("Result", imgOrignal)
if(probabilityValue*100 > 80.00):
k=getCalssName(classIndex)
ser.write(str(k).encode()+str('\n').encode())
print(k)
time.sleep(2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break