Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
123 |
|
|
|
|
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
139 |
-
|
140 |
w, h = x2 - x1, y2 - y1
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
if
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
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 |
-
|
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 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
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 |
-
|
216 |
-
|
217 |
-
|
218 |
|
219 |
-
|
220 |
-
|
221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
fareCalculations()
|
240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|