AkshatJain1402 commited on
Commit
4b9a23d
1 Parent(s): f39257d

renamed app2

Browse files
Files changed (3) hide show
  1. app.py +162 -6
  2. cam.py +9 -0
  3. dummy.py +0 -165
app.py CHANGED
@@ -1,9 +1,165 @@
1
- import cv2 as cv
2
- import numpy as n
 
 
 
 
3
 
4
- cap=cv.VideoCapture(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  while True:
7
- success,img=cap.read()
8
- cv.imshow("Video",img)
9
- cv2.waitKey(1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ import cvzone
5
+ import math
6
+ from sort import *
7
 
8
+
9
+
10
+ # MongoDB connection URI with a default database (replace with your actual values)
11
+
12
+ # def connectMongo() -> pymongo.database.Database:
13
+ # try:
14
+
15
+
16
+ # try:
17
+
18
+ # client = pymongo.MongoClient('mongodb+srv://INFINIX:INFINIX@cluster0.rubyoda.mongodb.net/?retryWrites=true&w=majority')
19
+ # db = client["INFINIX"]
20
+ # print(db)
21
+ # if db != None:
22
+ # print("connected to db")
23
+ # return db
24
+ # except Exception as e:
25
+ # print(e)
26
+
27
+
28
+ # except Exception as e:
29
+ # return "Error in Connecting to MongoDB" + str(e)
30
+
31
+ # db=connectMongo()
32
+
33
+ # collection=db['BUS_DETS']
34
+
35
+ # Create a collection to store entry count
36
+ # entry_count_collection = db.entry_count
37
+
38
+ cap = cv2.VideoCapture('TrialFootage.mp4')
39
+
40
+ model = YOLO("../Yolo-Weights/yolov8n.pt")
41
+ occupancy = 0
42
+ coming = 0
43
+ goin = 0
44
+ ListPeople = []
45
+ dict = {}
46
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
47
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
48
+ print(f"Video Resolution: {width}x{height}")
49
+ classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
50
+ "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
51
+ "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
52
+ "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
53
+ "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
54
+ "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
55
+ "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
56
+ "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
57
+ "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
58
+ "teddy bear", "hair drier", "toothbrush"
59
+ ]
60
+
61
+ # Tracking
62
+
63
+ tracker = Sort(max_age=20, min_hits=3, iou_threshold=0.3)
64
+
65
+ yelloLine = [270, 0, 270, 600]
66
+
67
+ RedLine = [173, 0, 173, 600]
68
+
69
+ totalCountUp = []
70
+ mask=cv2.imread('mask.jpg')
71
 
72
  while True:
73
+
74
+
75
+ success, img = cap.read()
76
+ # imgRegion=cv2.bitwise_and(img,mask)
77
+
78
+ results = model(img, stream=True)
79
+
80
+ detections = np.empty((0, 5))
81
+
82
+ for r in results:
83
+ boxes = r.boxes
84
+ for box in boxes:
85
+ # Bounding Box
86
+ x1, y1, x2, y2 = box.xyxy[0]
87
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
88
+ # cv2.rectangle(img,(x1,y1),(x2,y2),(255,0,255),3)
89
+ w, h = x2 - x1, y2 - y1
90
+
91
+ # Confidence
92
+ conf = math.ceil((box.conf[0] * 100)) / 100
93
+ # Class Name
94
+ cls = int(box.cls[0])
95
+ currentClass = classNames[cls]
96
+
97
+ if currentClass == "person" and conf > 0.3:
98
+ # cvzone.putTextRect(img, f'{currentClass} {conf}', (max(0, x1), max(35, y1)),
99
+ # scale=0.6, thickness=1, offset=3)
100
+ # cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=5)
101
+ currentArray = np.array([x1, y1, x2, y2, conf])
102
+ detections = np.vstack((detections, currentArray))
103
+
104
+ resultsTracker = tracker.update(detections)
105
+
106
+
107
+ for result in resultsTracker:
108
+ x1, y1, x2, y2, id = result
109
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
110
+ print(result)
111
+ w, h = x2 - x1, y2 - y1
112
+ cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=2, colorR=(255, 0, 255))
113
+ cvzone.putTextRect(img, f' {int(id)}', (max(0, x1), max(35, y1)),
114
+ scale=2, thickness=3, offset=10)
115
+
116
+ cx, cy = x1 + w // 2, y1 + h // 2
117
+ cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
118
+
119
+ if yelloLine[0] - 20 < cx < yelloLine[2] + 20:
120
+ if totalCountUp.count(id) == 0:
121
+ totalCountUp.append(id)
122
+ dict[id] = [False]
123
+ cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
124
+ elif totalCountUp.count(id) == 1:
125
+ if (dict[id].count(False) < 1):
126
+ dict[id].append(False)
127
+ cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
128
+ if RedLine[0] - 20 < cx < RedLine[2] + 30:
129
+ if totalCountUp.count(id) == 0:
130
+ totalCountUp.append(id)
131
+ dict[id] = [True]
132
+ cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
133
+ elif totalCountUp.count(id) == 1:
134
+ if (dict[id].count(True) < 1):
135
+ dict[id].append(True)
136
+ cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
137
+
138
+ print(totalCountUp)
139
+ entry_count = 0
140
+
141
+ for i in dict.values():
142
+ if (len(i) == 2):
143
+ if i[0] == True and i[1] == False:
144
+ if entry_count > 0:
145
+ entry_count -= 1
146
+ if i[0] == False and i[1] == True:
147
+ entry_count += 1
148
+
149
+
150
+ print('count is ', entry_count)
151
+ print(dict)
152
+ # # Update the MongoDB collection with the current count
153
+ # entry_count_collection.update_one({}, {"$set": {"count":entry_count}}, upsert=True)
154
+ # collection.update_one({"id": "your_document_id"}, {"$set": {"entry_count": entry_count}})
155
+
156
+ cv2.putText(img, str(entry_count), (110, 245), cv2.FONT_HERSHEY_PLAIN, 5, (50, 50, 230), 7)
157
+ print('count is ', entry_count)
158
+ print(dict)
159
+ cv2.imshow("Image", img)
160
+ cv2.waitKey(1)
161
+ print(entry_count)
162
+ # collection.update_one({"id":"826587"},
163
+ # {"$set": {
164
+ # "entry_count":entry_count,
165
+ # }})
cam.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import cv2 as cv
2
+ import numpy as n
3
+
4
+ cap=cv.VideoCapture(0)
5
+
6
+ while True:
7
+ success,img=cap.read()
8
+ cv.imshow("Video",img)
9
+ cv2.waitKey(1)
dummy.py DELETED
@@ -1,165 +0,0 @@
1
- import numpy as np
2
- from ultralytics import YOLO
3
- import cv2
4
- import cvzone
5
- import math
6
- from sort import *
7
-
8
-
9
-
10
- # MongoDB connection URI with a default database (replace with your actual values)
11
-
12
- # def connectMongo() -> pymongo.database.Database:
13
- # try:
14
-
15
-
16
- # try:
17
-
18
- # client = pymongo.MongoClient('mongodb+srv://INFINIX:INFINIX@cluster0.rubyoda.mongodb.net/?retryWrites=true&w=majority')
19
- # db = client["INFINIX"]
20
- # print(db)
21
- # if db != None:
22
- # print("connected to db")
23
- # return db
24
- # except Exception as e:
25
- # print(e)
26
-
27
-
28
- # except Exception as e:
29
- # return "Error in Connecting to MongoDB" + str(e)
30
-
31
- # db=connectMongo()
32
-
33
- # collection=db['BUS_DETS']
34
-
35
- # Create a collection to store entry count
36
- # entry_count_collection = db.entry_count
37
-
38
- cap = cv2.VideoCapture('TrialFootage.mp4')
39
-
40
- model = YOLO("../Yolo-Weights/yolov8n.pt")
41
- occupancy = 0
42
- coming = 0
43
- goin = 0
44
- ListPeople = []
45
- dict = {}
46
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
47
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
48
- print(f"Video Resolution: {width}x{height}")
49
- classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
50
- "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
51
- "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
52
- "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
53
- "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
54
- "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
55
- "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
56
- "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
57
- "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
58
- "teddy bear", "hair drier", "toothbrush"
59
- ]
60
-
61
- # Tracking
62
-
63
- tracker = Sort(max_age=20, min_hits=3, iou_threshold=0.3)
64
-
65
- yelloLine = [270, 0, 270, 600]
66
-
67
- RedLine = [173, 0, 173, 600]
68
-
69
- totalCountUp = []
70
- mask=cv2.imread('mask.jpg')
71
-
72
- while True:
73
-
74
-
75
- success, img = cap.read()
76
- # imgRegion=cv2.bitwise_and(img,mask)
77
-
78
- results = model(img, stream=True)
79
-
80
- detections = np.empty((0, 5))
81
-
82
- for r in results:
83
- boxes = r.boxes
84
- for box in boxes:
85
- # Bounding Box
86
- x1, y1, x2, y2 = box.xyxy[0]
87
- x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
88
- # cv2.rectangle(img,(x1,y1),(x2,y2),(255,0,255),3)
89
- w, h = x2 - x1, y2 - y1
90
-
91
- # Confidence
92
- conf = math.ceil((box.conf[0] * 100)) / 100
93
- # Class Name
94
- cls = int(box.cls[0])
95
- currentClass = classNames[cls]
96
-
97
- if currentClass == "person" and conf > 0.3:
98
- # cvzone.putTextRect(img, f'{currentClass} {conf}', (max(0, x1), max(35, y1)),
99
- # scale=0.6, thickness=1, offset=3)
100
- # cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=5)
101
- currentArray = np.array([x1, y1, x2, y2, conf])
102
- detections = np.vstack((detections, currentArray))
103
-
104
- resultsTracker = tracker.update(detections)
105
-
106
-
107
- for result in resultsTracker:
108
- x1, y1, x2, y2, id = result
109
- x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
110
- print(result)
111
- w, h = x2 - x1, y2 - y1
112
- cvzone.cornerRect(img, (x1, y1, w, h), l=9, rt=2, colorR=(255, 0, 255))
113
- cvzone.putTextRect(img, f' {int(id)}', (max(0, x1), max(35, y1)),
114
- scale=2, thickness=3, offset=10)
115
-
116
- cx, cy = x1 + w // 2, y1 + h // 2
117
- cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
118
-
119
- if yelloLine[0] - 20 < cx < yelloLine[2] + 20:
120
- if totalCountUp.count(id) == 0:
121
- totalCountUp.append(id)
122
- dict[id] = [False]
123
- cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
124
- elif totalCountUp.count(id) == 1:
125
- if (dict[id].count(False) < 1):
126
- dict[id].append(False)
127
- cv2.line(img, (yelloLine[0], yelloLine[1]), (yelloLine[2], yelloLine[3]), (0, 0, 255), 5)
128
- if RedLine[0] - 20 < cx < RedLine[2] + 30:
129
- if totalCountUp.count(id) == 0:
130
- totalCountUp.append(id)
131
- dict[id] = [True]
132
- cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
133
- elif totalCountUp.count(id) == 1:
134
- if (dict[id].count(True) < 1):
135
- dict[id].append(True)
136
- cv2.line(img, (RedLine[0], RedLine[1]), (RedLine[2], RedLine[3]), (0, 255, 200), 5)
137
-
138
- print(totalCountUp)
139
- entry_count = 0
140
-
141
- for i in dict.values():
142
- if (len(i) == 2):
143
- if i[0] == True and i[1] == False:
144
- if entry_count > 0:
145
- entry_count -= 1
146
- if i[0] == False and i[1] == True:
147
- entry_count += 1
148
-
149
-
150
- print('count is ', entry_count)
151
- print(dict)
152
- # # Update the MongoDB collection with the current count
153
- # entry_count_collection.update_one({}, {"$set": {"count":entry_count}}, upsert=True)
154
- # collection.update_one({"id": "your_document_id"}, {"$set": {"entry_count": entry_count}})
155
-
156
- cv2.putText(img, str(entry_count), (110, 245), cv2.FONT_HERSHEY_PLAIN, 5, (50, 50, 230), 7)
157
- print('count is ', entry_count)
158
- print(dict)
159
- cv2.imshow("Image", img)
160
- cv2.waitKey(1)
161
- print(entry_count)
162
- # collection.update_one({"id":"826587"},
163
- # {"$set": {
164
- # "entry_count":entry_count,
165
- # }})