-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetReal.py
More file actions
1104 lines (842 loc) · 32.3 KB
/
GetReal.py
File metadata and controls
1104 lines (842 loc) · 32.3 KB
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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
#GetReal
Viewing CAD Models in 3D space before you fabricate
Software Design Final Project Spring 2016
Kevin Zhang, Cedric Kim, Daniel Daugherty, Kevin Guo
Augmented Reality, allows one to project any CAD stl file into 3D space onto a marker, and be able to view it in Virtual Reality
"""
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import cv2
import numpy as np
from threading import Thread
from PIL import Image
from collections import deque
import argparse
import imutils
import math
from stl import mesh
import glob
import random
import os
import struct
import sys
class Contours(object):
"""
class for detecting contours of tracker
"""
def __init__(self):
self.contour_list = []
self.centers = []
# creates contours sorted by area size (biggest to smallest) from contour_information
def update_contours(self, contour_information):
"""
updates the contours of the tracker, maintains orientation and location of tracker
"""
# grab all the contour information and store the actual contours into contours
contours = contour_information[0]
self.contour_list = []
# if the number of contours is atleast 4,
if len(contours) >= 4:
for contour in contours:
# store the area of the contours and the contours in the same list
self.contour_list.append((cv2.contourArea(contour),contour))
# sort the contours by area
self.contour_list.sort(key = lambda x: x[0], reverse=True)
class Centers(object):
"""
class for detecting the center of each blue square on the tracker
"""
def __init__(self):
self.corners = []
self.main_corner = (0,0)
self.main_corner_index = -1
self.vectors = []
self.final_corners = []
self.distances = []
self.threshold = 5
self.num_black_corners = 0
self.is_tracking = True
def update_centers(self, contour_list, mask_black):
"""
takes in a list of contours and a masked black frame to creates a tuple of (x,y) coordinates for the center of each contour
"""
# if there are contours in the list,
if len(contour_list) > 0:
self.corners = []
for i in range(4):
# create a moment (used to find center of contour)
M = cv2.moments(contour_list[i][1])
if M["m00"] != 0 and M["m00"] != 0:
# creates a (x, y) tuple for the contour
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
# add the center to a list
self.corners.append(center)
self.num_black_corners = 0
for i, center in enumerate(self.corners):
# grab the color at the center of the black mask,
color = mask_black[center[1], center[0]]
# if the color at the center is black
if color == 255:
self.num_black_corners += 1
# store that information
self.main_corner_index = i
self.main_corner = center
if self.num_black_corners == 1:
self.is_tracking = True
else:
self.is_tracking = False
def update_vectors(self):
"""
creates vectors (x,y) reference tuples from the main corner (black corner)
"""
self.vectors = []
# for each corner in corners,
for i, corner in enumerate(self.corners):
main_corner_x = self.main_corner[0]
main_corner_y = self.main_corner[1]
corner_x = corner[0]
corner_y = corner[1]
# if the main corner isnt the current index,
if self.main_corner_index != i:
# create a vector and add it to a list
self.vectors.append((corner_x - main_corner_x, corner_y - main_corner_y))
def reorganize_centers(self):
"""
reorganizes the centers so that they are in correct relation to each other
"""
# final_corners is the final list of organized corners
self.final_corners = []
self.final_corners.append(self.main_corner)
corners = []
# for each corner, only add the corner if it isnt the main corner
if len(self.corners) == 4:
for i, corner in enumerate(self.corners):
if not(self.main_corner[0] == corner[0] and self.main_corner[1] == corner[1]):
corners.append(corner)
# save the fourth corner
corner_4 = return_point_4(self.main_corner, corners[0], corners[1], corners[2])
# find the quadrant clockwise to the quadrant the corners are occupying
quadrant = return_closest_quadrant(self.main_corner, corners[0], corners[1], corners[2])
potential_points = []
# for each corner (this finds potential points for corner_2)
for corner in corners:
# if the corner is not corner 4,
if not (corner[0] == corner_4[0] and corner[1] == corner_4[1]):
check_quadrant = quadrant + 1
if check_quadrant == 5:
check_quadrant = 1
# if the corner is within the quadrant after check quadrant,
if check_quadrant == return_quadrant((corner[0] - self.main_corner[0], corner[1] - self.main_corner[1])):
## add this to potential_points
potential_points.append(corner)
corner_2 = (0,0)
corner_3 = (0,0)
# pass potential points into return_point_2
if len(potential_points) > 0:
corner_2 = return_point_2(quadrant, self.main_corner, potential_points)
for corner in corners:
if not (corner[0] == corner_4[0] and corner[1] == corner_4[1]):
if not (corner[0] == corner_2[0] and corner[1] == corner_2[1]):
corner_3 = corner
self.final_corners.append(corner_2)
self.final_corners.append(corner_3)
self.final_corners.append(corner_4)
def distance_of_corners(self, final_corners):
"""
sets self.distances to have all the distances between each corner
"""
self.distances = []
for i in range(len(final_corners)):
if i == 3:
self.distances.append(get_distance(final_corners[i], final_corners[0]))
else:
self.distances.append(get_distance(final_corners[i], final_corners[i+1]))
def bool_is_tracking(self):
"""
determines whether the tracker is still tracking the markers based on a threshold
"""
self.distance_of_corners(self.final_corners)
self.distances.sort()
test_value = (self.distances[3] + self.distances[2])/ self.distances[0]
if test_value > self.threshold:
self.is_tracking = False
def return_point_2(quadrant, main_corner, potential_points):
"""
checks the angle in order to find corner_2
"""
# if the length is one, return it
if len(potential_points) == 1:
return potential_points[0]
# create reference points in order to find the angle
else:
if quadrant == 1:
reference_point = (main_corner[0], main_corner[1] - 10)
elif quadrant == 2:
reference_point = (main_corner[0] - 10, main_corner[1])
elif quadrant == 3:
reference_point = (main_corner[0], main_corner[1] + 10)
else:
reference_point = (main_corner[0] + 10, main_corner[1])
angle_1 = get_angle(main_corner, reference_point, potential_points[0])
angle_2 = get_angle(main_corner, reference_point, potential_points[1])
# if the angle is smaller, return that point
if angle_1 < angle_2:
return potential_points[0]
else:
return potential_points[1]
def return_closest_quadrant(main_corner, point_1, point_2, point_3):
"""
creates empty quadrants and uses return_most_clockwise_quadrant
"""
reference_point_1 = (point_1[0] - main_corner[0], point_1[1] - main_corner[1])
reference_point_2 = (point_2[0] - main_corner[0], point_2[1] - main_corner[1])
reference_point_3 = (point_3[0] - main_corner[0], point_3[1] - main_corner[1])
quadrants = []
quadrants.append(return_quadrant(reference_point_1))
quadrants.append(return_quadrant(reference_point_2))
quadrants.append(return_quadrant(reference_point_3))
empty_quadrants = []
# find quadrants the points are not in
for i in range(1, 5):
if i not in quadrants:
empty_quadrants.append(i)
return return_most_clockwise_quadrant(empty_quadrants)
def return_most_clockwise_quadrant(empty_quadrants):
"""
returns the most clockwise quadrant the points are not in
"""
if len(empty_quadrants) == 1:
return empty_quadrants[0]
elif len(empty_quadrants) == 2:
if empty_quadrants[0] == 1 and empty_quadrants[1] == 4:
return 1
else:
return empty_quadrants[1]
elif len(empty_quadrants) == 3:
if empty_quadrants[0] == 1 and empty_quadrants[1] == 3 and empty_quadrants[2] == 4:
return 1
elif empty_quadrants[0] == 1 and empty_quadrants[1] == 2 and empty_quadrants[2] == 4:
return 2
else:
return empty_quadrants[2]
def return_quadrant(point):
"""
returns the quadrant that the inputted point is in
"""
if is_positive(point[0]) and not is_positive(point[1]):
return 1
elif not is_positive(point[0]) and not is_positive(point[1]):
return 2
elif not is_positive(point[0]) and is_positive(point[1]):
return 3
else:
return 4
def return_point_4(main_corner, point_1, point_2, point_3):
"""
returns the fourth corner
"""
angle_1 = get_angle(main_corner, point_1, point_2)
angle_2 = get_angle(main_corner, point_1, point_3)
angle_3 = get_angle(main_corner, point_2, point_3)
# find the angle between the corners, only one is greater that the other
if(angle_1 > angle_2 and angle_1 > angle_3):
return point_3
elif(angle_2 > angle_3):
return point_2
else:
return point_1
def get_distance(point_1, point_2):
"""
the distance formula
"""
return math.sqrt((point_1[0] - point_2[0])**2 + (point_1[1] - point_2[1])**2)
def get_angle(main_corner, point_1, point_2):
"""
the law of cosines
"""
c = get_distance(point_1, point_2)
b = get_distance(main_corner, point_2)
a = get_distance(main_corner, point_1)
return math.fabs(math.acos((c**2 - a**2 - b**2)/(-2*a*b)))
def is_positive(x):
"""
checks if x is positive
"""
return (x >= 0)
class Camera(object):
"""
class to calibrate camera for OpenCV tracking
"""
def __init__(self):
self.objpoints = [] #3d point in real world space
self.imgpoints = [] #2d points in image plane.
self.criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
self.objp = np.zeros((2*2,3), np.float32)
self.objp[:,:2] = np.mgrid[0:2,0:2].T.reshape(-1,2)
self.ret = None
self.mtx = None
self.dist = None
self.rvecs = None
self.tvecs = None
self.draw_axis = False
self.view_matrix = False
def grab_frame_information(self, frame, corners):
"""
arrays to store object points and image points from all the images.
"""
self.objpoints.append(self.objp)
self.imgpoints.append(np.array(corners, dtype = np.float32))
def calibrate_camera(self, gray):
"""
gets the information for calibrating the camera
"""
self.ret, self.mtx, self.dist, self.rvecs, self.tvecs = cv2.calibrateCamera(self.objpoints, self.imgpoints, gray.shape[::-1],None,None)
class createpoint:
"""
class for a 3d point
"""
def __init__(self,p,c=(1,0,0)):
self.point_size=0.5
self.color=c
self.x=p[0]
self.y=p[1]
self.z=p[2]
def glvertex(self):
glVertex3f(self.x,self.y,self.z)
class createtriangle:
"""
class for a 3d face on a model
"""
points=None
normal=None
def __init__(self,p1,p2,p3,n=None):
# 3 points of the triangle
self.points=createpoint(p1),createpoint(p2),createpoint(p3)
# triangles normal
self.normal=createpoint(self.calculate_normal(self.points[0],self.points[1],self.points[2]))#(0,1,0)#
def calculate_vector(self,p1,p2):
"""
calculate vector / edge
"""
return -p1.x+p2.x,-p1.y+p2.y,-p1.z+p2.z
def calculate_normal(self,p1,p2,p3):
"""
calculate the cross product returns a vector
"""
a=self.calculate_vector(p3,p2)
b=self.calculate_vector(p3,p1)
return self.cross_product(a,b)
def cross_product(self,p1,p2):
"""
returns the cross_product of the triangle
"""
return (p1[1]*p2[2]-p2[1]*p1[2]) , (p1[2]*p2[0])-(p2[2]*p1[0]) , (p1[0]*p2[1])-(p2[0]*p1[1])
class loader:
"""
class to load stl file
"""
model=[]
def get_triangles(self):
"""
return the faces of the triangles
"""
if self.model:
for face in self.model:
yield face
def draw(self):
"""
draw the models faces
"""
global color
# draws each stl triangle as an OpenGL triangle
glBegin(GL_TRIANGLES)
for tri in self.get_triangles():
if color == 0:
glColor3f(.73, .74, .8)
elif color == 1:
glColor3f(212.0/255,175.0/255,55.0/255)
elif color == 2:
glColor3f(220.0/255,20.0/255,60.0/255)
elif color == 3:
glColor3f(0,191.0/255,255/255)
elif color == 4:
glColor3f(50.0/255,205.0/255,50.0/255)
elif color == 5:
glColor3f(138.0/255,43.0/255,226.0/255)
elif color == 6:
r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)
glColor3f(r/255.0, g/255.0, b/255.0)
glNormal3f(tri.normal.x,tri.normal.y,tri.normal.z)
glVertex3f(tri.points[0].x,tri.points[0].y,tri.points[0].z)
glVertex3f(tri.points[1].x,tri.points[1].y,tri.points[1].z)
glVertex3f(tri.points[2].x,tri.points[2].y,tri.points[2].z)
glColor3f(1.0, 1.0, 1.0)
glEnd()
def load_stl(self,filename):
"""
detects if the file is a text file or binary file
"""
# read start of file to determine if its a binay stl file or a ascii stl file
fp=open(filename,'rb')
h=fp.read(80)
type=h[6:13]
print type
fp.close()
if type=='Default':
print "reading text file"+str(filename)
self.load_text_stl(filename)
else:
print "reading binary stl file "+str(filename,)
self.load_binary_stl(filename)
def load_text_stl(self,filename):
"""
read text stl match keywords to grab the points to build the model
"""
fp=open(filename,'r')
for line in fp.readlines():
words=line.split()
if len(words)>0:
if words[0]=='solid':
self.name=words[1]
if words[0]=='facet':
center=[0.0,0.0,0.0]
triangle=[]
normal=(eval(words[2]),eval(words[3]),eval(words[4]))
if words[0]=='vertex':
triangle.append((eval(words[1]),eval(words[2]),eval(words[3])))
if words[0]=='endloop':
#make sure we got the correct number of values before storing
if len(triangle)==3:
self.model.append(createtriangle(triangle[0],triangle[1],triangle[2],normal))
fp.close()
def load_binary_stl(self,filename):
"""
loads binary stl file using the struct library to read in and convert binary data into a format we can use
"""
fp=open(filename,'rb')
h=fp.read(80)
l=struct.unpack('I',fp.read(4))[0]
count=0
while True:
try:
p=fp.read(12)
if len(p)==12:
n=struct.unpack('f',p[0:4])[0],struct.unpack('f',p[4:8])[0],struct.unpack('f',p[8:12])[0]
p=fp.read(12)
if len(p)==12:
p1=struct.unpack('f',p[0:4])[0],struct.unpack('f',p[4:8])[0],struct.unpack('f',p[8:12])[0]
p=fp.read(12)
if len(p)==12:
p2=struct.unpack('f',p[0:4])[0],struct.unpack('f',p[4:8])[0],struct.unpack('f',p[8:12])[0]
p=fp.read(12)
if len(p)==12:
p3=struct.unpack('f',p[0:4])[0],struct.unpack('f',p[4:8])[0],struct.unpack('f',p[8:12])[0]
new_tri=(n,p1,p2,p3)
if len(new_tri)==4:
tri=createtriangle(p1,p2,p3,n)
self.model.append(tri)
count+=1
fp.read(2)
if len(p)==0:
break
except EOFError:
break
fp.close()
class Webcam:
"""
class for streaming webcam images using OpenCV
"""
def __init__(self):
# set webcam video feed
self.video_capture = cv2.VideoCapture(1)
self.current_frame = self.video_capture.read()[1]
def start(self):
"""
create thread for capturing images
"""
Thread(target=self._update_frame, args=()).start()
def _update_frame(self):
"""
callback function for updating the next frame
"""
while(True):
self.current_frame = self.video_capture.read()[1]
def get_current_frame(self):
"""
get the current frame
"""
return self.current_frame
class AugmentedReality():
"""
class for integrating OpenCV tracking with OpenGL rendering
"""
# constants
INVERSE_MATRIX = np.array([[ 1.0, 1.0, 1.0, 1.0],
[-1.0,-1.0,-1.0,-1.0],
[-1.0,-1.0,-1.0,-1.0],
[ 1.0, 1.0, 1.0, 1.0]])
def __init__(self):
# initialise webcam and start thread
self.webcam = Webcam()
self.webcam.start()
self.model1=loader()
file_name = raw_input("enter stl file name\n")
exact_file_name = glob.glob(os.path.abspath('')+'/STL/'+ file_name+'*')
print exact_file_name
self.model1.load_stl(exact_file_name[0])
# textures
self.texture_background = None
self.is_window_1 = True
def _init_gl(self, Width, Height):
"""
calibrate the webcam to detect tracker and initialize OpenGL
"""
# global variables to handle OpenCV tracking
global contour
global center
global camera
# initialize objects to handle OpenCV tracking
contour = Contours()
center = Centers()
camera = Camera()
# varibales to handle tracking
blueLower = np.array([90,100,10])
blueUpper = np.array([150,255,255])
blackLower = np.array([0,0,0])
blackUpper = np.array([180, 255, 150])
# calibrate webcam to detect tracker
images = glob.glob(os.path.abspath('') + '/Pictures/*cut*.png')
for fname in images:
img = cv2.imread(fname)
img = cv2.flip(img, 1)
hsv_frame = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# construct a mask for the color "blue", then remove any imperfections
mask_blue = cv2.inRange(hsv_frame, blueLower, blueUpper)
mask_blue = cv2.erode(mask_blue, None, iterations=1)
mask_blue = cv2.dilate(mask_blue, None, iterations=1)
# create black mask for tracking corner
mask_black = cv2.inRange(hsv_frame, blackLower, blackUpper)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# creates information about the contours
contour_information = cv2.findContours(mask_blue.copy(), cv2.RETR_CCOMP,
cv2.CHAIN_APPROX_SIMPLE)
# updates each of the elements in the classes
contour.update_contours(contour_information)
center.update_centers(contour.contour_list, mask_black)
center.reorganize_centers()
camera.grab_frame_information(img, center.final_corners)
camera.calibrate_camera(gray)
self._set_textures()
def _reload_stl(self):
"""
Allows for the user to re-input a new stl on the spot
"""
self.model1.model = []
file_name = raw_input("enter stl file name\n")
exact_file_name = glob.glob(os.path.abspath('')+'/STL/'+ file_name+'*')
self.model1.load_stl(exact_file_name[0])
def _set_textures(self):
"""
makes the textures for both the background and the 3D rendered object
"""
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(33.7, 1.3, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
# enable textures
glEnable(GL_TEXTURE_2D)
self.texture_background = glGenTextures(1)
# initialize OpenGL lighting functions
glEnable(GL_LIGHTING)
glEnable(GL_NORMALIZE)
glShadeModel(GL_FLAT)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_LIGHT0)
glEnable(GL_LIGHT1)
# Ambient Light
ambientColor = [0.2, 0.2, 0.2, 1.0]
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor)
# Light 1
lightColor0 = [0.5, 0.5, 0.5, 1.0]
lightPos0 = [30.0, -30.0, -30.0, 1.0]
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0)
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0)
# Light 2
lightColor1 = [0.5, 0.5, 0.5, 1.0]
lightPos1 = [-1.0, 0.5, 0.5, 0.0]
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1)
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1)
def _draw_scene(self):
"""
function continuously called by Glut to display the webcam feed and render the stl
"""
glutSetWindow(self.window_id)
# clear Screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
# get image from webcam
image = self.webcam.get_current_frame()
image = cv2.flip(image,0)
#image = cv2.flip(image,1)
image_array = Image.fromarray(image)
ix = image_array.size[0]
iy = image_array.size[1]
image_array = image_array.tostring("raw", "BGRX", 0, -1)
# disable lighting and enable textures for background
glDisable(GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
# create background texture
glBindTexture(GL_TEXTURE_2D, self.texture_background)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_array)
# draw background
glBindTexture(GL_TEXTURE_2D, self.texture_background)
glPushMatrix()
glTranslatef(0.0,0.0,-30.0)
self._draw_background()
glPopMatrix()
# enable Lighting and disable textures for stl
glEnable(GL_LIGHTING)
glDisable(GL_TEXTURE_2D)
# handle glyph
self._handle_glyph(image)
glutSwapBuffers()
glutSetWindow(self.window_id_2)
# clear Screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
# get image from webcam
image = self.webcam.get_current_frame()
image = cv2.flip(image,0)
#image = cv2.flip(image,1)
# disable lighting and enable textures for background
glDisable(GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
# create background texture
glBindTexture(GL_TEXTURE_2D, self.texture_background)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_array)
# draw background
glBindTexture(GL_TEXTURE_2D, self.texture_background)
glPushMatrix()
glTranslatef(0.0,0.0,-30.0)
self._draw_background()
glPopMatrix()
# enable Lighting and disable textures for stl
glEnable(GL_LIGHTING)
glDisable(GL_TEXTURE_2D)
# handle glyph
self._handle_glyph(image)
# display window
glutSwapBuffers()
def _handle_glyph(self, image):
"""
draws stl file on tracker
"""
# attempt to detect tracker from OpenCV
rvecs = None
tvecs = None
try:
rvecs, tvecs = self.detect_glyph(image)
except Exception as ex:
print(ex)
# if there is no tracker, do not render anything
if rvecs == None or tvecs == None:
return
# build reference matrix from OpenCV
rmtx = cv2.Rodrigues(rvecs)[0]
view_matrix = np.array([[rmtx[0][0],rmtx[0][1],rmtx[0][2],tvecs[0]],
[rmtx[1][0],rmtx[1][1],rmtx[1][2],tvecs[1]],
[rmtx[2][0],rmtx[2][1],rmtx[2][2],tvecs[2]],
[0.0 ,0.0 ,0.0 ,1.0 ]])
view_matrix = view_matrix * self.INVERSE_MATRIX
view_matrix = np.transpose(view_matrix)
# load reference matrix from OpenCV as drawing space for OpenGL
glMatrixMode(GL_PROJECTION)
glLoadIdentity();
gluPerspective(45, 1.3, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW);
glPushMatrix()
glLoadMatrixd(view_matrix)
# rotates and scales the stl
glRotatef(angle, 0, 0, 1.0)
glScale(size, size, size)
# draws stl
self.model1.draw()
glPopMatrix()
def _draw_background(self):
"""
streams webcam feed onto background of OpenGL window
"""
# draws webcam image as a texture on an OpenGL quad
glMatrixMode(GL_PROJECTION)
glLoadIdentity();
gluPerspective(33.7, 1.3, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW);
glBegin(GL_QUADS)
glTexCoord2f(0.0, 1.0); glVertex3f(-12.0, -9.0, 0.0)
glTexCoord2f(1.0, 1.0); glVertex3f( 12.0, -9.0, 0.0)
glTexCoord2f(1.0, 0.0); glVertex3f( 12.0, 9.0, 0.0)
glTexCoord2f(0.0, 0.0); glVertex3f(-12.0, 9.0, 0.0)
glEnd( )
def update(self, dt):
"""
update function to handle the rotation of the stl file
"""
# global variables for rotation of stl file
global angle
global turning
global size
global size_direction
global restart
# if the spacebar is pressed, the stl will start to rotate
if turning:
angle += 2.0
if angle > 360.0:
angle -= 360.0
if size_direction == 1:
size += .001
size_direction = 0
if size_direction == -1:
size -= .001
size_direction = 0
if restart:
self._reload_stl()
restart = False
# update position with global position
glutPostRedisplay()
glutTimerFunc(5, self.update, 0)
def detect_glyph(self, image):
"""
returns the rvecs and the tvecs of an image
"""
# global variables for OpenCV tracking
global camera
global contour
global center
# format image for tracking
frame = imutils.resize(image, width = 750)
frame = cv2.flip(frame, 0)
#frame = cv2.flip(frame, 1)
# color space
blueLower = np.array([90,100,10])
blueUpper = np.array([150,255,255])
blackLower = np.array([0,0,0])
blackUpper = np.array([180, 255, 150])
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# construct a mask for the color "blue", then remove any imperfections
mask_blue = cv2.inRange(hsv_frame, blueLower, blueUpper)
mask_blue = cv2.erode(mask_blue, None, iterations=1)
mask_blue = cv2.dilate(mask_blue, None, iterations=1)
## create black mask for tracking corner
mask_black = cv2.inRange(hsv_frame, blackLower, blackUpper)
# creates information about the contours`
contour_information = cv2.findContours(mask_blue.copy(), cv2.RETR_CCOMP,
cv2.CHAIN_APPROX_SIMPLE)
# updates each of the elements in the classes
contour.update_contours(contour_information)
center.update_centers(contour.contour_list, mask_black)
if len(center.corners) == 4:
center.reorganize_centers()
center.update_vectors()
center.bool_is_tracking()
# if the camera detects our tracker, it will return the rvecs and tvecs of the image
if center.is_tracking: