-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcamTrackingTestGSMP.py
148 lines (99 loc) · 3.41 KB
/
webcamTrackingTestGSMP.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
import cv2
import numpy
import time
import concurrent.futures
def isDifferent(currPixel, lastPixel, DELTA):
diff=abs(int(currPixel)-int(lastPixel))
if (diff>DELTA):
return True
else:
return False
def showFrame(frame):
cv2.imshow('Feed', frame)
DELTA = 50
SHOWFEED = False
SHOWFPS =True
NUM_CORES =4
# def readRow(gRow, lRow, r):
# avgTarget=[[0,0],0]
# for c in range(len(gRow)):
# if (isDifferent(gRow[c], lRow[c], DELTA)):
# lRow[c]=gRow[c]
# if (SHOWFEED):
# gRow[c]=0
# avgTarget=[[avgTarget[0][0]+r, avgTarget[0][1]+c], avgTarget[1]+1]
# else:
# lRow[c]=gRow[c]
# return (avgTarget, lRow)
def readRows(gFrame, lFrame):
rT=0
cT=0
total=0
for r in range(len(gFrame)):
for c in range(len(gFrame[0])):
if (isDifferent(gFrame[r][c], lFrame[r][c], DELTA)):
lFrame[r][c]=gFrame[r][c]
if (SHOWFEED):
gFrame[r][c]=0
rT+=r
cT+=c
total+=1
else:
lFrame[r][c]=gFrame[r][c]
return [[rT, cT], total, lFrame, gFrame]
if __name__ == '__main__':
print("code initiated")
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 64)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,48)
ret, lf = cap.read()
lastFrame=cv2.cvtColor(lf, cv2.COLOR_BGR2GRAY)
lastFrame = numpy.array_split(lastFrame,4)
#print(lastFrame)
# print(lastFrame[0])
# print(lastFrame[1])
print(len(lastFrame))
rows=len(lastFrame)
cols=len(lastFrame[0])
print(len(lastFrame[0]))
#shows video
targetPoint: list
isTarget=False
numFrames=0
lastTime = time.time()
avgTarget: list
while(True):
totalAvgTarget = [[0,0],0]
ret, frame = cap.read()
gFrame=numpy.array_split(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY),NUM_CORES)
with concurrent.futures.ProcessPoolExecutor() as executor:
#hard coded for 4 cores
results = executor.map(readRows, gFrame, lastFrame)
f=0
for res in results:
totalAvgTarget[0][0]+=res[0][0]
totalAvgTarget[0][1]+=res[0][1]
totalAvgTarget[1]+=res[1]
lastFrame[f]=res[2]
gFrame[f]=res[3]
f+=1
totalAvgTarget[0][0]+=rows*1.5
totalAvgTarget[0][1]+=cols*1.5
if (totalAvgTarget[1]>100):
isTarget=True
targetPoint=[totalAvgTarget[0][0]/totalAvgTarget[1],totalAvgTarget[0][1]/totalAvgTarget[1]]
if (isTarget):
if (SHOWFEED):
sFrame=numpy.append(numpy.append(gFrame[0], gFrame[1]),numpy.append(gFrame[2],gFrame[3]))
for r in range(max(int(targetPoint[0])-5,0), min(int(targetPoint[0])+5, len(gFrame))):
for c in range(max(int(targetPoint[1])-5,0), min(int(targetPoint[1]+5),len(gFrame[0]))):
sFrame[r][c]=255
showFrame(sFrame)
if(SHOWFPS):
numFrames+=1
if (time.time()-lastTime>=1):
print(numFrames)
lastTime=time.time()
numFrames=0
if cv2.waitKey(1) & 0xFF == ord('q'):
break