-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_python.py
213 lines (183 loc) · 6.8 KB
/
main_python.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
206
207
208
209
210
211
212
213
import time
import cv2
import TRON
import sys
import functions
from imutils.video import WebcamVideoStream
currentFileVersion = 2
pointsX = []
pointsY = []
pointsZ = []
FPSMeter = TRON.FPS(2)
angleSpeed = 0
openCameras = 0
openVideo = 0
record = 0
countUnlikelyDetections = 0
colorPoints = False
print("Do you wish to open a file? ('y' to open, else 'n')")
workStyle = input()
if workStyle == "y" or workStyle == "Y":
print("Enter file name to open")
else:
print("Creating a new file")
print("Enter file name")
fileName = input()
fileName = "files/" + fileName
if workStyle == "y" or workStyle == "Y":
f = open(fileName + ".3dd", 'r')
f.readline()
tmp = int(f.readline())
if tmp != currentFileVersion:
print("FILE VERSION INVALID!!!")
sys.exit(0)
tmp = int(f.readline())
for i in range(tmp):
tmp = f.readline().split()
pointsX.append(float(tmp[0]))
pointsY.append(float(tmp[1]))
pointsZ.append(float(tmp[2]))
print("Data read successful")
print("Would you like to open cameras for further recording? ('y' to open, else 'n')")
tmp = input()
if tmp == "y" or tmp == "Y":
openCameras = 1
print("Enter new file name to write to")
fileName = input()
fileName = "files/" + fileName
else:
print("Do you wish to use cameras or video files as source? ('y' to open cameras, else 'n')")
tmp = input()
if tmp == "y" or tmp == "Y":
openCameras = 1
print("Do you wish to record? ('y' to open cameras, else 'n')")
tmp = input()
if tmp == "y" or tmp == "Y":
record = 1
print("Enter video set name")
videoFileName = input()
videoWriter1 = cv2.VideoWriter("video_files/" + videoFileName + "1.avi", cv2.VideoWriter_fourcc(*'XVID'), 30, (640, 480))
videoWriter2 = cv2.VideoWriter("video_files/" + videoFileName + "2.avi", cv2.VideoWriter_fourcc(*'XVID'), 30, (640, 480))
else:
print("Enter video set name")
videoFileName = input()
cap1 = cv2.VideoCapture("video_files/" + videoFileName + "1.avi")
cap2 = cv2.VideoCapture("video_files/" + videoFileName + "2.avi")
tmp, frame1 = cap1.read()
tmp, frame2 = cap2.read()
cv2.imshow("Camera 1:", frame1)
cv2.imshow("Camera 2:", frame2)
openVideo = 1
hog = cv2.HOGDescriptor()
hog.load("detector/hog/detector_l.yml")
if openCameras:
print("Please enter camera ID's one per line")
cap1ID = int(input())
cap2ID = int(input())
print("Loading cameras")
cap1 = WebcamVideoStream(src=cap1ID).start()
cap2 = WebcamVideoStream(src=cap2ID).start()
print("Loading complete")
frame1 = cap1.read()
frame2 = cap2.read()
cv2.imshow("Camera 1:", frame1)
cv2.imshow("Camera 2:", frame2)
def display():
global cap1, cap2, frame1, frame2, FPSMeter, pointsX, pointsY, pointsZ, angleSpeed, openCameras, countUnlikelyDetections, openVideo, colorPoints, record
X1 = -1
Y1 = 0
X2 = -1
Y2 = 0
if openCameras or openVideo:
if openCameras:
frame1 = cap1.read()
frame2 = cap2.read()
else:
tmp, frame1 = cap1.read()
tmp, frame2 = cap2.read()
if record == 2:
videoWriter1.write(frame1)
videoWriter2.write(frame2)
record = 1
elif record == 0:
(rects1, weights1) = hog.detectMultiScale(frame1, winStride=(8, 8), padding=(32, 32), scale=1.05, finalThreshold=5)
(rects2, weights2) = hog.detectMultiScale(frame2, winStride=(8, 8), padding=(32, 32), scale=1.05, finalThreshold=5)
for (x, y, w, h) in rects1:
cv2.rectangle(frame1, (x, y), (x + w, y + h), (0, 255, 0), 2)
X1 = x + w / 2
Y1 = y + h / 2
for (x, y, w, h) in rects2:
cv2.rectangle(frame2, (x, y), (x + w, y + h), (0, 255, 0), 2)
X2 = x + w / 2
Y2 = y + h / 2
cv2.imshow("Camera 1:", frame1)
cv2.imshow("Camera 2:", frame2)
if cv2.waitKey(10) == 27:
sys.exit()
if X1 != -1 and X2 != -1 and abs(Y1 - Y2) < 10:
arr = functions.transferCoordinates(X1, (Y1 + Y2) / 2, X2)
if arr is not None:
pointsX.append(arr[0])
pointsY.append(arr[1])
pointsZ.append(arr[2])
elif abs (Y1 - Y2) >= 10:
countUnlikelyDetections += 1
if countUnlikelyDetections >= 50:
print("It seems that cameras are set not well enough")
countUnlikelyDetections = 0
if openVideo:
print("Processed " + str(int(cap1.get(cv2.CAP_PROP_POS_FRAMES))) + " out of " + str(int(cap1.get(cv2.CAP_PROP_FRAME_COUNT))))
if cap1.get(cv2.CAP_PROP_POS_FRAMES) == cap1.get(cv2.CAP_PROP_FRAME_COUNT):
openVideo = 0
cv2.destroyAllWindows()
cap1.release()
cap2.release()
print("FINISHED!!!")
else:
FPSMeter.updateAndPrint()
TRON.cameraAngle1 -= angleSpeed
functions.drawBase()
for i in range(len(pointsX)):
if colorPoints:
if i/len(pointsX) <= 0.34:
TRON.setColorRGB(1, i / len(pointsX) * 3 - 0.05, i / len(pointsX) * 3 - 0.05)
elif i/len(pointsX) <= 0.67:
TRON.setColorRGB(1, 1, 1 - (i - len(pointsX) / 3) / len(pointsX) * 3)
else:
TRON.setColorRGB(1 - (i - 2 * len(pointsX) / 3) / len(pointsX) * 3, 1, (i - 2 * len(pointsX) / 3) / len(pointsX) * 3)
TRON.drawSphere(pointsY[i] - functions.getArrayAverage(pointsY), pointsZ[i], pointsX[i], 0.05, 10)
fileSaveState = 0
def aditionalKeyboardHandle():
global angleSpeed, pointsX, pointsY, pointsZ, fileSaveState, colorPoints, record
if TRON.keyState['r'] == 1:
pointsX = []
pointsY = []
pointsZ = []
if TRON.keyState['i'] == 1 and fileSaveState == 0:
f = open(fileName + ".3dd", 'w')
f.write("File version:\n")
f.write(str(currentFileVersion) + "\n")
f.write(str(len(pointsX)) + "\n")
for i in range(len(pointsX)):
f.write(str(pointsX[i]) + " " + str(pointsY[i]) + " " + str(pointsZ[i]) + "\n")
f.close()
fileSaveState = 1
print("Data saved")
if TRON.keyState['i'] == 0:
fileSaveState = 0
if TRON.keyState['e']:
angleSpeed = 0.1
if TRON.keyState['q']:
angleSpeed = -0.1
if TRON.keyState['f']:
angleSpeed = 0
if TRON.keyState['t']:
TRON.cameraLookAtX = TRON.cameraLookAtY = TRON.cameraLookAtZ = 0
colorPoints = 0
if TRON.keyState['g']:
colorPoints = 1
if TRON.keyState['x']:
record = 2
TRON.Prepare(b"3D Drawer", 1000, 720, display, aditionalKeyboardHandle, None, None, None, 700, 200)
TRON.cameraDistanceToObject = 10
TRON.Launch()