-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_detection_lpr.py
186 lines (151 loc) · 6.48 KB
/
hand_detection_lpr.py
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
import cv2
import argparse
import orien_lines
import datetime
from imutils.video import VideoStream
from utils import detector_utils as detector_utils
import pandas as pd
from datetime import date
import xlrd
from xlwt import Workbook
from xlutils.copy import copy
import numpy as np
from truck_lpr_demo import detect_license_plate
lst1=[]
lst2=[]
ap = argparse.ArgumentParser()
ap.add_argument('-d', '--display', dest='display', type=int,
default=1, help='Display the detected images using OpenCV. This reduces FPS')
args = vars(ap.parse_args())
detection_graph, sess = detector_utils.load_inference_graph()
def save_data(no_of_time_hand_detected, no_of_time_hand_crossed):
try:
today = date.today()
today=str(today)
#loc = (r'C:\Users\rahul.tripathi\Desktop\result.xls')
rb = xlrd.open_workbook('result.xls')
sheet = rb.sheet_by_index(0)
sheet.cell_value(0, 0)
#print(sheet.nrows)
q=sheet.cell_value(sheet.nrows-1,1)
rb = xlrd.open_workbook('result.xls')
#rb = xlrd.open_workbook(loc)
wb=copy(rb)
w_sheet=wb.get_sheet(0)
if q==today:
w=sheet.cell_value(sheet.nrows-1,2)
e=sheet.cell_value(sheet.nrows-1,3)
w_sheet.write(sheet.nrows-1,2,w+no_of_time_hand_detected)
w_sheet.write(sheet.nrows-1,3,e+no_of_time_hand_crossed)
wb.save('result.xls')
else:
w_sheet.write(sheet.nrows,0,sheet.nrows)
w_sheet.write(sheet.nrows,1,today)
w_sheet.write(sheet.nrows,2,no_of_time_hand_detected)
w_sheet.write(sheet.nrows,3,no_of_time_hand_crossed)
wb.save('result.xls')
except FileNotFoundError:
today = date.today()
today=str(today)
# Workbook is created
wb = Workbook()
# add_sheet is used to create sheet.
sheet = wb.add_sheet('Sheet 1')
sheet.write(0, 0, 'Sl.No')
sheet.write(0, 1, 'Date')
sheet.write(0, 2, 'Number of times hand detected')
sheet.write(0, 3, 'Number of times hand crossed')
m=1
sheet.write(1, 0, m)
sheet.write(1, 1, today)
sheet.write(1, 2, no_of_time_hand_detected)
sheet.write(1, 3, no_of_time_hand_crossed)
wb.save('result.xls')
def count_no_of_times(lst):
x=y=cnt=0
for i in lst:
x=y
y=i
if x==0 and y==1:
cnt=cnt+1
return cnt
if __name__ == '__main__':
# Detection confidence threshold to draw bounding box
score_thresh = 0.75
#vs = cv2.VideoCapture('rtsp://192.168.1.64')
vs = VideoStream(1).start()
#Oriendtation of machine
Orientation=input("Enter the orientation of hand progression ~ lr,rl,bt,tb :")
#For Machine
#Line_Perc1=float(input("Enter the percent of screen the line of machine :"))
Line_Perc1=float(15)
#For Safety
#Line_Perc2=float(input("Enter the percent of screen for the line of safety :"))
Line_Perc2=float(30)
# max number of hands we want to detect/track
num_hands_detect = 2
# Used to calculate fps
start_time = datetime.datetime.now()
num_frames = 0
im_height, im_width = (None, None)
cv2.namedWindow('Detection', cv2.WINDOW_NORMAL)
count=0
lic = ''
cv2.namedWindow('Detection', cv2.WINDOW_NORMAL)
try:
while True:
# Read Frame and process
count +=1
frame = vs.read()
frame = np.array(frame)
#frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if im_height == None:
im_height, im_width = frame.shape[:2]
# Convert image to rgb since opencv loads images in bgr, if not accuracy will decrease
try:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
except:
print("Error converting to RGB")
#cv2.line(img=frame, pt1=(0, Line_Position1), pt2=(frame.shape[1], Line_Position1), color=(255, 0, 0), thickness=2, lineType=8, shift=0)
#cv2.line(img=frame, pt1=(0, Line_Position2), pt2=(frame.shape[1], Line_Position2), color=(255, 0, 0), thickness=2, lineType=8, shift=0)
# Run image through tensorflow graph
boxes, scores, classes = detector_utils.detect_objects(
frame, detection_graph, sess)
Line_Position2=orien_lines.drawsafelines(frame,Orientation,Line_Perc1,Line_Perc2)
# Draw bounding boxeses and text
a,b=detector_utils.draw_box_on_image(
num_hands_detect, score_thresh, scores, boxes, classes, im_width, im_height, frame,Line_Position2,Orientation)
if (count % 20 ==0):
license_p = detect_license_plate(frame)
lic = str(license_p)
lpr = "License Plate : "
cv2.putText(frame, lpr+lic , (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0, 255), 1, cv2.LINE_AA)
lst1.append(a)
lst2.append(b)
no_of_time_hand_detected=no_of_time_hand_crossed=0
# Calculate Frames per second (FPS)
num_frames += 1
elapsed_time = (datetime.datetime.now() -
start_time).total_seconds()
fps = num_frames / elapsed_time
if args['display']:
# Display FPS on frame
detector_utils.draw_text_on_image("FPS : " + str("{0:.2f}".format(fps)), frame)
cv2.imshow('Detection', cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
vs.stop()
break
no_of_time_hand_detected=count_no_of_times(lst2)
#no_of_time_hand_detected=b
no_of_time_hand_crossed=count_no_of_times(lst1)
#print(no_of_time_hand_detected)
#print(no_of_time_hand_crossed)
save_data(no_of_time_hand_detected, no_of_time_hand_crossed)
print("Average FPS: ", str("{0:.2f}".format(fps)))
except KeyboardInterrupt:
no_of_time_hand_detected=count_no_of_times(lst2)
no_of_time_hand_crossed=count_no_of_times(lst1)
today = date.today()
save_data(no_of_time_hand_detected, no_of_time_hand_crossed)
print("Average FPS: ", str("{0:.2f}".format(fps)))