-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCOP.py
205 lines (158 loc) · 8.1 KB
/
PCOP.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import cv2
import time
import argparse
import torch
import numpy as np
import face_recognition
from detector import build_detector
from deep_sort import build_tracker
from utils.draw import draw_boxes
from utils.parser import get_config
import dlib #--------------------------------------------
import threading
import time
#from numba import jit,cuda
nv = []
count=0
temp=[]
total_p=[]
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') #-------------------------------------------
OUTPUT_SIZE_WIDTH = 720
OUTPUT_SIZE_HEIGHT = 720 #--------------------------------------------
#@jit
def FindPoint(left, top,right, bottom, cx, cy) :
if (cx > left and cx < right and
cy > top and cy < bottom) :
return True
else :
return False
#@jit
def centre(top,right,bottom,left):
y = bottom + int((top-bottom)*0.5)
x = left + int((right - left)*0.5)
return [x,y]
class VideoTracker(object):
def __init__(self, cfg):
use_cuda = torch.cuda.is_available()
if not use_cuda:
raise UserWarning("Running in cpu mode!")
self.detector = build_detector(cfg, use_cuda=True)
self.deepsort = build_tracker(cfg, use_cuda=True)
self.class_names = self.detector.class_names
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
if exc_type:
print(exc_type, exc_value, exc_traceback)
def doRecognizePerson(faceNames, fid): #------------------------------------------------------------------------------
time.sleep(2)
faceNames[ fid ] = "Person " + str(fid) #-----------------------------------------------------------------------------
#Start the window thread for the two windows we are using
cv2.startWindowThread()
#The color of the rectangle we draw around the face
rectangleColor = (0,165,255)
#variables holding the current frame number and the current faceid
frameCounter = 0
currentFaceID = 0
#Variables holding the correlation trackers and the name per faceid
faceTrackers = {}
faceNames = {}
def track(self):
count=0
global temp
#import url
import numpy as np
import cv2
#Create two opencv named windows
#cv2.namedWindow("base-image", cv2.WINDOW_AUTOSIZE) #--------------------------------------------------------------
# cv2.namedWindow("result-image", cv2.WINDOW_AUTOSIZE) #--------------------------------------------------------------
#Position the windows next to eachother
#cv2.moveWindow("base-image",0,100) #--------------------------------------------------------------
#cv2.moveWindow("result-image",400,100) #---------------------------------------------------------------
#cap = cv2.VideoCapture('http://192.168.137.61:80/')#.dtype('uint32')
cap = cv2.VideoCapture(0)
#Start the window thread for the two windows we are using
cv2.startWindowThread()
#The color of the rectangle we draw around the face
rectangleColor = (0,165,255)
#variables holding the current frame number and the current faceid
frameCounter = 0
currentFaceID = 0
#Variables holding the correlation trackers and the name per faceid
faceTrackers = {}
faceNames = {}
while True:
ret,frame = cap.read()
frame=cv2.flip(frame,1)
frame=cv2.resize(frame,(1080,1080))
#baseImage = cv2.resize( frame, ( 1080, 1080)) #-------------------------------------------------------
#frame = baseImage[:, :, ::-1]
face_locations = face_recognition.face_locations(frame)
#print("Face location: ",face_locations)
#for top, right, bottom, left in face_locations:
# cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
#resultImage = baseImage.copy() #----------------------------------------------------------------
frameCounter += 1 #------------------------------------------------------------------------------
print("frame counter ",frameCounter)
bbox_xywh, cls_conf, cls_ids = self.detector(frame)
#cv2.putText(frame,str(bbox_xywh),(0,15), cv2.FONT_HERSHEY_PLAIN,1,(255,255,255) ,2)
if bbox_xywh is not None:
#print("No of live viewers: ",len(face_locations))
count=0
#print("Centre of face",temp)
#print("Face loactions "+str(face_locations)+"Person: "+str(bbox_xywh))
#flag=1
mask = cls_ids==0
bbox_xywh[:,3:] *= 1.2 # bbox dilation just in case bbox too small
cls_conf = cls_conf[mask]
outputs = self.deepsort.update(bbox_xywh, cls_conf, frame) #left,top,right,bottom
## if len(outputs) > 0:
## bbox_xyxy = outputs[:, :-1]
## identities = outputs[:,-1]
## frame = draw_boxes(frame, bbox_xyxy, identities)
##
face_locations = face_recognition.face_locations(frame)
## for top, right, bottom, left in face_locations:
## cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
print("No of live viewers: ",len(face_locations))
temp1=[]
for i in range(len(face_locations)):
t1,t2,t3,t4=face_locations[i]
if frameCounter%6 == 0:
temp=centre(t1,t2,t3,t4)
temp1.append(temp)
for i in range(len(outputs)):
if outputs[i][4] not in total_p:
total_p.append(outputs[i][4])
print("Total No of people: ",len(outputs))
if len(outputs) > 0:
bbox_xyxy = outputs[:, :-1]
identities = outputs[:,-1]
frame = draw_boxes(frame, bbox_xyxy, identities)
for i in range(len(outputs)):
if(outputs[i][4] not in nv):
for j in range(len(temp1)):
a,b,c,d,e= outputs[i]
flag=FindPoint(a,b,c,d,temp1[j][0],temp1[j][1])
#print(flag)
if flag:
if e not in nv:
nv.append(e)
#print("nv",nv)
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#print(count)
cap.release()
cv2.destroyAllWindows()
#print("No of People detected : ",len(temp))
#print(temp)
return [nv,total_p]
if __name__=="__main__":
cfg = get_config()
cfg.merge_from_file("./configs/yolov3.yaml")
cfg.merge_from_file("./configs/deep_sort.yaml")
with VideoTracker(cfg) as vdo_trk:
p = vdo_trk.track()
print("Total no of person who viewed advertisement "+str(len(nv))+" \n Total no of persons who passed by the advertisement board "+str(len(total_p)))