-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseatbelt.py
More file actions
81 lines (57 loc) · 2.24 KB
/
seatbelt.py
File metadata and controls
81 lines (57 loc) · 2.24 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
import cv2
from cv2 import waitKey
import numpy as np
import imutils
import messege
#Slope of line
def Slope(a,b,c,d):
return (d - b)/(c - a)
# Reading Image
#beltframe = cv2.imread("C:\\Users\\soura\\Desktop\\aryan\\seat-belt\\image.JPG")
beltframe = cv2.imread("C:\\Users\\soura\\Desktop\\aryan\\seat-belt\\CarSeat_boy_seatbelt_green.JPG")
#beltframe = cv2.imread("C:\\Users\\soura\\Desktop\\aryan\\seat-belt\\Capture.JPG")
# Resizing The Image
beltframe = imutils.resize(beltframe, height=800)
#Converting To GrayScale
beltgray = cv2.cvtColor(beltframe, cv2.COLOR_BGR2GRAY)
# No Belt Detected Yet
belt = False
# Bluring The Image For Smoothness
blur = cv2.blur(beltgray, (1, 1))
# Converting Image To Edges
edges = cv2.Canny(blur, 50, 400)
# Previous Line Slope
ps = 0
# Previous Line Co-ordinates
px1, py1, px2, py2 = 0, 0, 0, 0
# Extracting Lines
lines = cv2.HoughLinesP(edges, 1, np.pi/270, 30, maxLineGap = 20, minLineLength = 170)
# If "lines" Is Not Empty
if lines is not None:
# Loop line by line
for line in lines:
# Co-ordinates Of Current Line
x1, y1, x2, y2 = line[0]
# Slope Of Current Line
s = Slope(x1,y1,x2,y2)
# If Current Line's Slope Is Greater Than 0.7 And Less Than 2
if ((abs(s) > 0.7) and (abs (s) < 2)):
# And Previous Line's Slope Is Within 0.7 To 2
if((abs(ps) > 0.7) and (abs(ps) < 2)):
# And Both The Lines Are Not Too Far From Each Other
if(((abs(x1 - px1) > 5) and (abs(x2 - px2) > 5)) or ((abs(y1 - py1) > 5) and (abs(y2 - py2) > 5))):
# Plot The Lines On "beltframe"
cv2.line(beltframe, (x1, y1), (x2, y2), (0, 0, 255), 3)
cv2.line(beltframe, (px1, py1), (px2, py2), (0, 0, 255), 3)
# Belt Is Detected
print ("Belt Detected")
belt = True
# Otherwise Current Slope Becomes Previous Slope (ps) And Current Line Becomes Previous Line (px1, py1, px2, py2)
ps = s
px1, py1, px2, py2 = line[0]
if belt == False:
print("No Seatbelt detected")
messege.Response()
# Show The "beltframe"
cv2.imshow("Seat Belt", beltframe)
#waitKey(0)