imaneralo commited on
Commit
51ab369
1 Parent(s): 8a75e64

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -108
app.py CHANGED
@@ -119,122 +119,126 @@ RedLine = [173, 0, 173, 600]
119
  totalCountUp = []
120
  #mask=cv2.imread('mask.jpg')
121
  entry_count = 0
122
- while True:
 
 
 
 
123
 
 
 
124
 
125
-
126
- success, img = cap.read()
127
- # imgRegion=cv2.bitwise_and(img,mask)
128
-
129
- results = model(img, stream=True)
130
-
131
- detections = np.empty((0, 5))
132
-
133
- for r in results:
134
- boxes = r.boxes
135
- for box in boxes:
136
- # Bounding Box
137
- x1, y1, x2, y2 = box.xyxy[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
139
- # cv2.rectangle(img,(x1,y1),(x2,y2),(255,0,255),3)
140
  w, h = x2 - x1, y2 - y1
141
-
142
- # Confidence
143
- conf = math.ceil((box.conf[0] * 100)) / 100
144
- # Class Name
145
- cls = int(box.cls[0])
146
- currentClass = classNames[cls]
147
-
148
- if currentClass == "person" and conf > 0.3:
149
- # cvzone.putTextRect(img, f'{currentClass} {conf}', (max(0, x1), max(35, y1)),
150
- # scale=0.6, thickness=1, offset=3)
151
- # cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=5)
152
- currentArray = np.array([x1, y1, x2, y2, conf])
153
- detections = np.vstack((detections, currentArray))
154
-
155
- resultsTracker = tracker.update(detections)
156
-
157
- cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
158
- cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
159
- for result in resultsTracker:
160
- x1, y1, x2, y2, id = result
161
- x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
162
- print(result)
163
- w, h = x2 - x1, y2 - y1
164
- cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=2, colorR=(255, 0, 255))
165
- cvzone.putTextRect(img, f' {int(id)}', (max(0, x1), max(35, y1)),
166
- scale=2, thickness=3, offset=10)
167
-
168
- cx, cy = x1 + w // 2, y1 + h // 2
169
- cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
170
-
171
- if yelloLine[0] - 20 < cx < yelloLine[2] + 20:
172
- if totalCountUp.count(id) == 0:
173
- totalCountUp.append(id)
174
- dict[id] = [False]
175
- cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
176
- elif totalCountUp.count(id) == 1:
177
- if (dict[id].count(False) < 1):
178
- dict[id].append(False)
179
- cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
180
- if RedLine[0] - 20 < cx < RedLine[2] + 30:
181
- if totalCountUp.count(id) == 0:
182
- totalCountUp.append(id)
183
- dict[id] = [True]
184
- dict[id].append(currentCoordinates)
185
- #adding timestamp
186
- current_time = datetime.datetime.now()
187
- dict[id].append(current_time)
188
- cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
189
- elif totalCountUp.count(id) == 1:
190
- if (dict[id].count(True) < 1):
191
- dict[id].append(True)
192
  dict[id].append(currentCoordinates)
193
- #adding timestamp
194
  current_time = datetime.datetime.now()
195
  dict[id].append(current_time)
196
-
197
  cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
198
-
199
- print(totalCountUp)
200
- entry_count=0
201
-
202
- for i in dict.values():
203
- if (len(i) >= 2):
204
- if i[0] == True and i[1] == False:
205
- print('in true/False')
206
- if entry_count > 0:
207
- entry_count -= 1
208
-
209
- if i[0] == False and i[1] == True:
210
- print('in /False/True')
211
- entry_count += 1
212
-
213
-
214
 
215
- # print('count is ', entry_count)
216
- # print(dict)
217
-
218
 
219
- cv2.putText(img, str(entry_count), (110, 245), cv2.FONT_HERSHEY_PLAIN, 5, (50, 50, 230), 7)
220
- print('count is ', entry_count)
221
- print(dict)
 
 
 
 
 
 
 
 
 
 
 
 
222
 
223
- # Create the Gradio interface
224
- # interface = gr.Interface(
225
- # fn=returnRBG(img),
226
- # inputs=gr.Text("enter anything"),
227
- # outputs="image",
228
- # )
229
- # cv2.waitKey(1)
230
- print(entry_count)
231
-
232
- # if currentCoordinates==stationToCoordinates:
233
- # reachedDestination=True
234
- # fareCalculations()
235
- # dict=[]
236
- # break
237
- if entry_count>2:
238
- print('reached destination')
239
- fareCalculations()
240
- break
 
 
 
 
 
 
 
 
119
  totalCountUp = []
120
  #mask=cv2.imread('mask.jpg')
121
  entry_count = 0
122
+
123
+ try:
124
+ while True:
125
+
126
+
127
 
128
+ success, img = cap.read()
129
+ # imgRegion=cv2.bitwise_and(img,mask)
130
 
131
+ results = model(img, stream=True)
132
+
133
+ detections = np.empty((0, 5))
134
+
135
+ for r in results:
136
+ boxes = r.boxes
137
+ for box in boxes:
138
+ # Bounding Box
139
+ x1, y1, x2, y2 = box.xyxy[0]
140
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
141
+ # cv2.rectangle(img,(x1,y1),(x2,y2),(255,0,255),3)
142
+ w, h = x2 - x1, y2 - y1
143
+
144
+ # Confidence
145
+ conf = math.ceil((box.conf[0] * 100)) / 100
146
+ # Class Name
147
+ cls = int(box.cls[0])
148
+ currentClass = classNames[cls]
149
+
150
+ if currentClass == "person" and conf > 0.3:
151
+ # cvzone.putTextRect(img, f'{currentClass} {conf}', (max(0, x1), max(35, y1)),
152
+ # scale=0.6, thickness=1, offset=3)
153
+ # cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=5)
154
+ currentArray = np.array([x1, y1, x2, y2, conf])
155
+ detections = np.vstack((detections, currentArray))
156
+
157
+ resultsTracker = tracker.update(detections)
158
+
159
+ cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
160
+ cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
161
+ for result in resultsTracker:
162
+ x1, y1, x2, y2, id = result
163
  x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
164
+ print(result)
165
  w, h = x2 - x1, y2 - y1
166
+ cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=2, colorR=(255, 0, 255))
167
+ cvzone.putTextRect(img, f' {int(id)}', (max(0, x1), max(35, y1)),
168
+ scale=2, thickness=3, offset=10)
169
+
170
+ cx, cy = x1 + w // 2, y1 + h // 2
171
+ cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
172
+
173
+ if yelloLine[0] - 20 < cx < yelloLine[2] + 20:
174
+ if totalCountUp.count(id) == 0:
175
+ totalCountUp.append(id)
176
+ dict[id] = [False]
177
+ cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
178
+ elif totalCountUp.count(id) == 1:
179
+ if (dict[id].count(False) < 1):
180
+ dict[id].append(False)
181
+ cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
182
+ if RedLine[0] - 20 < cx < RedLine[2] + 30:
183
+ if totalCountUp.count(id) == 0:
184
+ totalCountUp.append(id)
185
+ dict[id] = [True]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  dict[id].append(currentCoordinates)
187
+ #adding timestamp
188
  current_time = datetime.datetime.now()
189
  dict[id].append(current_time)
 
190
  cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
191
+ elif totalCountUp.count(id) == 1:
192
+ if (dict[id].count(True) < 1):
193
+ dict[id].append(True)
194
+ dict[id].append(currentCoordinates)
195
+ #adding timestamp
196
+ current_time = datetime.datetime.now()
197
+ dict[id].append(current_time)
198
+
199
+ cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
 
 
 
 
 
 
 
200
 
201
+ print(totalCountUp)
202
+ entry_count=0
 
203
 
204
+ for i in dict.values():
205
+ if (len(i) >= 2):
206
+ if i[0] == True and i[1] == False:
207
+ print('in true/False')
208
+ if entry_count > 0:
209
+ entry_count -= 1
210
+
211
+ if i[0] == False and i[1] == True:
212
+ print('in /False/True')
213
+ entry_count += 1
214
+
215
+
216
+
217
+ # print('count is ', entry_count)
218
+ # print(dict)
219
 
220
+
221
+ cv2.putText(img, str(entry_count), (110, 245), cv2.FONT_HERSHEY_PLAIN, 5, (50, 50, 230), 7)
222
+ print('count is ', entry_count)
223
+ print(dict)
224
+
225
+ # Create the Gradio interface
226
+ # interface = gr.Interface(
227
+ # fn=returnRBG(img),
228
+ # inputs=gr.Text("enter anything"),
229
+ # outputs="image",
230
+ # )
231
+ # cv2.waitKey(1)
232
+ print(entry_count)
233
+
234
+ # if currentCoordinates==stationToCoordinates:
235
+ # reachedDestination=True
236
+ # fareCalculations()
237
+ # dict=[]
238
+ # break
239
+ if entry_count>2:
240
+ print('reached destination')
241
+ fareCalculations()
242
+ break
243
+ except Exception as e:
244
+ print("error occured",e)