-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFourEyes.py
1565 lines (1440 loc) · 54.7 KB
/
FourEyes.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
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
from Tkinter import *
import Tkinter as tk
import cv2
from PIL import Image, ImageTk
import os
import csv
import webbrowser
"""last updated april 28 at 1:54 PM"""
###########################################################
################# FACE SHAPE ALGORITHM ####################
###########################################################
def areSimilarDimensions(lenA,lenB):
ratio = 1.0*lenA/lenB
#saying that the ratio almost equals 1, with some room for variation
allowedRatioDifference=0.3
return abs(ratio-1.0)<=allowedRatioDifference
def areSimilarRatios(ratioA,ratioB):
allowedRatioDifference=0.2
return abs(ratioA-ratioB)<=allowedRatioDifference
def areSimilarSquareRatios(ratioA,ratioB):
squareMaxDifference=0.1
return ratioA-ratioB<=squareMaxDifference
def isHeartFace(faceDimensions):
forehead,jaw,faceLength,faceWidth=faceDimensions
#A face is heart shaped if the forehead is significantly wider than the jaw.
foreheadJawRatio=1.0*forehead/jaw
print foreheadJawRatio
differentSizeRatio=1.24 #chosen based on tests
#checking if the forehead is significantly the widest part of the face
return foreheadJawRatio>differentSizeRatio
def isOvalFace(faceDimensions):
forehead,jaw,faceLength,faceWidth=faceDimensions
#A face is oval shaped if the face is approx 1.5 times as long as
#it is wide or more, but it was too sensitive so 1.55 gives more accurate
#results
lengthWidthRatio=1.0*faceLength/faceWidth
idealOvalRatio=1.55
return lengthWidthRatio>=idealOvalRatio
def isSquareFace(faceDimensions):
#a face is square if the forehead and jaw are approximately the same width
forehead,jaw,faceLength,faceWidth=faceDimensions
foreheadJawRatio=1.0*forehead/jaw
idealSquareForeheadJawRatio=1.0
return areSimilarSquareRatios(foreheadJawRatio,idealSquareForeheadJawRatio)
def getFaceShape(faceDimensions):
#heart is the next distinctive, if the forehead is the widest, that
#person's face is definitely a heart. therefore, it gets checked first.
if isHeartFace(faceDimensions)==True:
return "heart"
#oval is the next most distinctive, so it gets checked second. If the length
#is about 1.5 times the width, it's an oval.
if isOvalFace(faceDimensions)==True:
return "oval"
#then comes square, with angular features and similar widths everywhere
elif isSquareFace(faceDimensions)==True:
return "square"
#last remaining face shape
else: return "round"
def getFaceDimensions(dots):
#the information about distances is stored in the dots made earlier
(ax,ay)=dots[0].cx,dots[0].cy
(bx,by)=dots[1].cx,dots[1].cy
(cx,cy)=dots[2].cx,dots[2].cy
(dx,dy)=dots[3].cx,dots[3].cy
(ex,ey)=dots[4].cx,dots[4].cy
(fx,fy)=dots[5].cx,dots[5].cy
#distance formula between particular dots
foreheadWidth=((fx-bx)**2+(fy-by)**2)**0.5
jawWidth=((ex-cx)**2+(ey-cy)**2)**0.5
faceLength=((ax-dx)**2+(ay-dy)**2)**0.5
faceWidth=(foreheadWidth+jawWidth)/2.0
return (foreheadWidth,jawWidth,faceLength,faceWidth)
###########################################################
###################### CLASSES ############################
###########################################################
class Struct: pass
class Button(object): #buttons that respond to beind clicked and reset after
def __init__(self,text,font,x0,y0,x1,y1):
#allows for formatting of how button text looks
self.text=text
self.font=font
self.x0=x0
self.x1=x1
self.y0=y0
self.y1=y1
self.fontColor=data.backgroundColor
self.buttonInset=4 #how much it sets into the screen when clicked
#default colors: when it's clicked, it's clickColor, when it's
#unclicked, it's data.highlightColor
self.color=data.highlightColor
self.isClicked=False
self.clickColor=rgbString(115,107,153)
def draw(self,canvas):
#draws the button and puts the text in the middle
offset=self.buttonInset
if self.isClicked==False:
canvas.create_rectangle(self.x0+offset,self.y0+offset,
self.x1+offset,self.y1+offset,fill=self.clickColor,width=0)
canvas.create_rectangle(self.x0,self.y0,self.x1,self.y1,
fill=self.color,width=0,outline=data.highlightColor)
canvas.create_text((self.x0+self.x1)/2,(self.y0+self.y1)/2,
fill=self.fontColor,text=self.text,font=self.font)
def clicked(self):
#when it's clicked, it sets into the screen and changes color
self.isClicked=True
self.color=self.clickColor
self.x0+=self.buttonInset
self.x1+=self.buttonInset
self.y0+=self.buttonInset
self.y1+=self.buttonInset
def unclicked(self):
#once it's unclicked, it undoes what clicking does and sets it back
#and resets the color
self.color=data.highlightColor
self.x0-=self.buttonInset
self.x1-=self.buttonInset
self.y0-=self.buttonInset
self.y1-=self.buttonInset
self.isClicked=False
def setColor(self,color):
self.color=color
class DarkerButton(Button):
def __init__(self,text,font,x0,y0,x1,y1):
#allows for formatting of how button text looks
self.text=text
self.font=font
self.fontColor=rgbString(115,107,153)
self.x0=x0
self.x1=x1
self.y0=y0
self.y1=y1
self.buttonInset=4 #how much it sets into the screen when clicked
#default colors: when it's clicked, it's clickColor, when it's
#unclicked, it's data.highlightColor
self.color=data.backgroundColor
self.isClicked=False
self.clickColor=rgbString(115,107,153)
self.clickFont=data.backgroundColor
def draw(self,canvas):
#draws the button and puts the text in the middle
offset=self.buttonInset
canvas.create_rectangle(self.x0,self.y0,self.x1,self.y1,
fill=self.color,width=2,outline=self.fontColor)
canvas.create_text((self.x0+self.x1)/2,(self.y0+self.y1)/2,
fill=self.fontColor,text=self.text,font=self.font)
def clicked(self):
#when it's clicked, it sets into the screen and changes color
self.isClicked=True
self.color=self.clickColor
self.fontColor=self.clickFont
self.x0+=self.buttonInset
self.x1+=self.buttonInset
self.y0+=self.buttonInset
self.y1+=self.buttonInset
def unclicked(self):
#once it's unclicked, it undoes what clicking does and sets it back
#and resets the color
self.color=data.backgroundColor
self.fontColor=rgbString(115,107,153)
self.x0-=self.buttonInset
self.x1-=self.buttonInset
self.y0-=self.buttonInset
self.y1-=self.buttonInset
self.isClicked=False
class Dot(object):
def __init__(self,x,y,n,color):
#each dot has a location, a radius, and a "number"
self.cx=x
self.cy=y
self.r=6
self.n=n
self.color=color
def draw(self,canvas):
x,y=self.cx,self.cy
r=self.r
canvas.create_oval(x-r,y-r,x+r,y+r,fill=self.color,width=2)
#canvas.create_text(x,y,text=str(self.n))
def clickInside(self,x,y):
r=self.r*2 #gives user extra distance to mess up their click a bit
if (x>self.cx-r and x<self.cx+r
and y>self.cy-r and y<self.cy+r):
return True
return False
def connect(self,other,canvas,clicked=None):
if clicked==None: color="white"
elif self is clicked or other is clicked:
color=rgbString(115,107,153)
else: color="white"
canvas.create_line((self.cx,self.cy),(other.cx,other.cy),fill=color,
width=2,dash=(2,12))
class GlassesDisplay(object):
# A class for the visual representation of a pair of glasses, implemented
# in the browse frames feature
def __init__(self,name,shape,price,image,brand,link):
# Each pair has all of these features
self.name=name
self.shape=shape
self.price=self.getRealPrice(price)
self.image="source/browseglasses/"+image+".gif"
self.brand=brand
self.link=link
self.clicked=False
self.x0=None
self.x1=None
self.y0=None
self.y1=None
self.shadow=8
def __repr__(self):
# Using repr to see information about the current pair
result=("GlassesDisplay(name="+self.name+", shape="+self.shape+
", price="+self.price+", image="+self.image+", brand="+self.brand+
", link="+self.link+")")
return result
def getRealPrice(self,oldPrice):
# In the csv, 150.00 is represented as 150, so this is a fix for that
period=oldPrice.index(".")
while len(oldPrice[period:])<=2:
oldPrice+="0"
return oldPrice
def draw(self,canvas): #draws a white rectangle in the appropriate place
if self.pageNumber!=data.pageNumber: return #if it's not on the page
(left, top, xspacing, yspacing)=(80, 165, 80, 40)
(width, height)=(400, 365/2)
if self.position==3 or self.position==4: rowNum=2 #top half of screen
else: rowNum=1 #bottom half of screen
if self.position==1 or self.position==3: colNum=1 #left half of screen
else: colNum=2 #right half of screen
(x0, y0)=(left+colNum*xspacing+width*(colNum-1),
top+yspacing*rowNum+height*(rowNum-1))
(x1,y1)= (x0+width,y0+height) #spacing
(self.x0, self.x1, self.y0, self.y1)=(x0, x1, y0, y1)
if self.clicked==True: self.drawClicked(canvas) #draw the clicked button
else:
canvas.create_rectangle(x0+self.shadow,y0+self.shadow, #drop shadow
x1+self.shadow,y1+self.shadow, fill=data.backgroundColor,width=0)
canvas.create_rectangle(x0,y0,x1,y1,fill=data.highlightColor,
width=1,outline=data.backgroundColor) #the white box
self.drawImage(canvas,x0,x1,y0,y1) #draw the glasses image
self.drawText(canvas,x0,x1,y0,y1) #draw price and title
def drawClicked(self,canvas):
x0=self.x0
x1=self.x1
y0=self.y0
y1=self.y1
#draws the button in the darker color set into the screen when clicked
canvas.create_rectangle(x0+self.shadow,y0+self.shadow,x1+self.shadow,
y1+self.shadow,fill=data.backgroundColor,width=0)
self.drawClickedText(canvas,x0,x1,y0,y1)
def drawClickedText(self,canvas,x0,x1,y0,y1):
#draws the same text as usual but offset by the shadow and white
textY=y0+30+2*self.shadow
textX=(x0+x1)/2+2*self.shadow
font="Avenir 20 bold"
color=data.highlightColor
text='"'+self.name+'"'+" by "+self.brand
canvas.create_text(textX,textY,font=font,fill=color,text=text)
priceX=x1-10+self.shadow
priceY=y1-5+self.shadow
text="$"+self.price
font="Avenir 25"
canvas.create_text(priceX,priceY,text=text,
anchor="se",font=font,fill=color)
def drawText(self,canvas,x0,x1,y0,y1):
#draws information about price and such
textY=y0+30
textX=(x0+x1)/2
font="Avenir 20 bold"
color=data.backgroundColor
text='"'+self.name+'"'+" by "+self.brand
canvas.create_text(textX,textY,font=font,fill=color,text=text)
priceX=x1-10
priceY=y1-5
text="$"+self.price
font="Avenir 25"
canvas.create_text(priceX,priceY,text=text,anchor="se",font=font,
fill=color)
def drawImage(self,canvas,x0,x1,y0,y1):
#draws the image for the glasses in the right spot
imagepath=self.image
img=ImageTk.PhotoImage(file=imagepath)
#need 4 imagelabels to display 4 images at a time
if self.position==1:
data.imageLabel._image_cache=img
elif self.position==2:
data.imageLabel2._image_cache=img
elif self.position==3:
data.imageLabel3._image_cache=img
else:
data.imageLabel4._image_cache=img
x=(x0+x1)/2
y=y0+110
canvas.create_image(x,y,anchor="c",image=img)
def isClicked(self,x,y):
#checks if the glasses pair has been clicked
return (x>self.x0 and x<self.x1 and y>self.y0 and y<self.y1 and
data.pageNumber==self.pageNumber)
def linkToSite(self):
#opens a link to the glasses pair's link
webbrowser.open_new(self.link)
###########################################################
################## MAKE DOTS/BUTTONS ######################
###########################################################
def sortDots():
#sorts the dots in order based on their dot number (dot.n) so that I can
#know which dot is where
numDots=6
newDots=[]
for i in xrange(numDots):
for dot in data.dots:
if dot.n==i:
newDots.append(dot)
return newDots
def makeDots():
#makes dots at expected coordinates based on the face detection
dotList=[]
x,y,w,h=data.facerect #the rectangle is where the face is detected
babyspc=w/11
smallspc=w/8
medspc=w/5
bigspc=w/4
offset=100
yoffset=200
color="white"
dot1=Dot(x=x+w/2+offset,y=y-smallspc+yoffset,n=0,color=color)
dot2=Dot(x=x+w-smallspc+offset,y=y+bigspc+yoffset,n=1,color=color)
dot3=Dot(x=x+w-smallspc+offset,y=y+h/2+medspc*1.5+yoffset,n=2,color=color)
dot4=Dot(x=x+w/2+offset,y=y+h+yoffset,n=3,color=color)
dot5=Dot(x=x+medspc+offset,y=y+h/2+medspc*1.5+yoffset,n=4,color=color)
dot6=Dot(x=x+smallspc+offset,y=y+bigspc+yoffset,n=5,color=color)
return [dot1,dot2,dot3,dot4,dot5,dot6] #returns array of all the dots
def makeStartButton():
#creates the take a photo button
text="Take a Photo"
font="Helvetica 50 bold"
x0=data.width/3
y0=2.5*data.height/4
x1=2*data.width/3
y1=3*data.height/4
data.startButton=Button(text=text,font=font,x0=x0,y0=y0,x1=x1,y1=y1)
def makeSeeBestGlassesButton():
#creates the "see best style for you" button
x0=290
y0=590
x1=data.width-x0
y1=690
text="See Best Style For You"
font="Helvetica 43 bold"
data.seeBestGlassesButton=Button(text=text,font=font,x0=x0,y0=y0,
x1=x1,y1=y1)
def makeDoneWithDotsButton():
#creates the "done" button for users to press after they're done with dots
midx=(80+720)/2
x0,y0,x1,y1=midx-90,703,midx+90,773
text="DONE"
font="Helvetica 43 bold"
data.doneWithDotsButton=Button(text=text,font=font,x0=x0,y0=y0,x1=x1,y1=y1)
def makeBrowseFramesButton():
#makes the buttons for users to browse frames
x0=360
x1=data.width-x0
y0=615
y1=680
text="Browse These Frames"
font="Helvetica 35 bold"
#button
data.browseFramesButton=Button(text=text,font=font,x0=x0,y0=y0,x1=x1,y1=y1)
def makeTryThemOnButton():
#makes the button for people to try on glasses
x0=360
x1=data.width-x0
y0=695
y1=760
text="Try Them On"
font="Helvetica 35 bold"
data.tryThemOnButton=Button(text=text,font=font,x0=x0,y0=y0,x1=x1,y1=y1)
def makeBrowseFramesBackButton():
#makes the back button for the browse frames screen
x0=80
x1=180
y0=data.height-50
y1=data.height-120
text="<"
font="Helvetica 43 bold"
data.browseFramesBackButton=Button(text=text,font=font,
x0=x0,y0=y0,x1=x1,y1=y1)
def makeTryThemOnBackButton():
#makes the try them on back button
x0=48
x1=138
y0=data.height-33
y1=data.height-103
text="<"
font="Helvetica 43 bold"
data.tryThemOnBackButton=Button(text=text,font=font,x0=x0,y0=y0,x1=x1,y1=y1)
def makeResetButton():
y0=700
y1=765
x0=data.width-222
x1=data.width-82
text="Restart"
font="Helvetica 30 bold"
data.resetButton=DarkerButton(text=text,font=font,x0=x0,
y0=y0,x1=x1,y1=y1)
###########################################################
#################### MISCELLANEOUS ########################
###########################################################
###from course notes
def rgbString(red, green, blue):
return "#%02x%02x%02x" % (red, green, blue)
#returns the suggested frames for each face shape
#sets the glasses types it's looking for into data
def getSuggestedFrames():
if data.faceShape=="oval":
data.glasses=["Square","Rectangular"]
data.glassesType= "Square Frames"
return "Square Frames"
elif data.faceShape=="heart":
data.glasses=["Rimless","Wire"]
data.glassesType= "Thin Frames"
return "Thin Frames"
elif data.faceShape=="square":
data.glasses=["Oval","Round"]
data.glassesType= "Rounded Frames"
return "Rounded Frames"
elif data.faceShape=="round":
data.glasses=["Rectangular"]
data.glassesType= "Rectangular Frames"
return "Rectangular Frames"
def getGlassesList():
#returns a list of all of the glasses that the specific user needs
glassesList=None
if data.faceShape=="heart":
glassesList=data.glassesForHeart
elif data.faceShape=="oval":
glassesList=data.glassesForOval
elif data.faceShape=="square":
glassesList=data.glassesForSquare
else:
glassesList=data.glassesForRound
return glassesList
#modified version of the function in the course notes, returns .txt file as a
#list of lines for my purposes of putting writeups on multiple lines
def readFile(filename, mode="rt"):
# rt = "read text"
alltext=[]
with open(filename, mode) as fin:
for line in fin:
alltext.append(line)
return alltext
###########################################################
############# MOUSE FUNCTIONS/CLICK CHECKS ################
###########################################################
#checks if the start button has clicked
def clickInStartButton(x,y):
x0,y0,x1,y1=(data.startButton.x0,data.startButton.y0,data.startButton.x1,
data.startButton.y1)
data.takeAPhoto=True
return x>x0 and x<x1 and y>y0 and y<y1
#checks if they've taken a photo
def clickInPhotoButton(x,y):
x0=940-100
x1=940+100
y0=600-100
y1=600+100
if x>x0 and x<x1 and y>y0 and y<y1: return True
return False
#checks if they've clicked the "see best glasses" button
def clickSeeBestGlasses(x,y):
x0,y0,x1,y1=(data.seeBestGlassesButton.x0,data.seeBestGlassesButton.y0,
data.seeBestGlassesButton.x1,data.seeBestGlassesButton.y1)
if x>x0 and x<x1 and y>y0 and y<y1:
return True
return False
#checks if they've clicked the "browse frames" button
def clickBrowseFrames(x,y):
x0=360
x1=data.width-x0
y0=615
y1=680
return x>x0 and x<x1 and y>y0 and y<y1
#checks if they've clicked the "done with dots" button
def clickDoneWithDots(x,y):
midx=(80+720)/2
x0,y0,x1,y1=midx-90,703,midx+90,773
if x>x0 and x<x1 and y>y0 and y<y1: return True
#checks if they've clicked the "try them on" button
def clickTryThemOn(x,y):
x0=360
x1=data.width-x0
y0=695
y1=760
return x>x0 and x<x1 and y>y0 and y<y1
#checks if they've clicked the "browse frames" back button
def clickBrowseFramesBackButton(x,y):
x0=80
x1=180
y0=data.height-120
y1=data.height-50
return x>x0 and x<x1 and y>y0 and y<y1
#checks if they've gone between prev pages
def clickPreviousPage(x,y):
if data.pageNumber==1: return False
y0=data.height-110
r=50
xspacing=140
x0=data.width/2-xspacing
return x>x0-r and x<x0+r and y>y0-r and y<y0+r
#checks if they've gone to next page
def clickNextPage(x,y):
if data.pageNumber==data.totalPages: return False
y0=data.height-110
r=50
xspacing=140
x0=data.width/2+xspacing
return x>x0-r and x<x0+r and y>y0-r and y<y0+r
#checks if they've clicked the try them on back button
def clickTryThemOnBackButton(x,y):
x0=48
x1=138
y0=data.height-33
y1=data.height-103
return x>x0 and x<x1 and y<y0 and y>y1
#if they haven't hit start, waits for start button
def haventStartedMouseUp(x,y):
if clickInStartButton(x,y):
data.start=True
#if they haven't paused the image, waits for them to take a photo
def haventPausedMouseUp(x,y):
if clickInPhotoButton(x,y):
data.pause=True
ret,frame=data.cap.read()
frame=cv2.flip(frame,1)
#find the face bounding box on the paused frame
data.facerect=detectFace(frame)
data.dots=makeDots()
def havePausedDotsMouseUp(x,y):
#waits for them to click "done"
if clickDoneWithDots(x,y):
data.doneWithDotsButton.clicked()
faceDimensions=getFaceDimensions(data.dots)
if data.faceShape==None:
data.faceShape=getFaceShape(faceDimensions)
data.glassesList=getGlassesList()
if data.glassesType==None:
getSuggestedFrames()
#moves onto the next phase of the program
data.takeAPhoto=False
data.faceShapeInfo=True
def saveCurrentTryOnImage():
#saves the image they're taking to the current directory
data.numPhotosTaken+=1
#because it's keeping track of numPhotosTaken, they won't save over each
#other
title="TryingGlasses"+str(data.numPhotosTaken)+".png"
currImg=data.savedImage
#an opencv image gets saved
cv2.imwrite(title,currImg)
def clickCameraIcon(x,y):
#checks if they've clicked the camera icon to take a photo
x0=370
y0=645
x1=x0+157
y1=y0+142
return x>x0 and x<x1 and y>y0 and y<y1
def tryThemOnBackButtonClicked():
#changes the phases of the program according to what happens
#when the user clicks the try them on back button
data.showBestGlasses=True
data.tryThemOn=False
data.tryThemOnBackButton.unclicked()
data.tryThemOnButton.unclicked()
data.pausedTryOn=False
data.cameraClickedTryOn=False
data.ableToClick=True
data.canSeeBestGlasses=True
data.browseFramesCantBeClicked=False
def tryThemOnMouseUp(x,y):
#limits the program based on what can and can't be accessed
data.browseFramesCantBeClicked=True
data.canSeeBestGlasses=False
data.browseFrames=False
if clickTryThemOnBackButton(x,y):
tryThemOnBackButtonClicked()
else:
data.ableToClick=False #limits what the program can click
if data.cameraClickedTryOn==True: #if they click the play/camera button
if data.pausedTryOn==False: #does the camera thing
saveCurrentTryOnImage()
data.pausedTryOn=True
data.cameraClickedTryOn=False
elif data.pausedTryOn==True: #does the play button thing
data.pausedTryOn=False
data.cameraClickedTryOn=False
data.shadedPlayButton=False
def browseFramesMouseUp(x,y):
#restricts program from accessing old information
data.canSeeBestGlasses=False
if clickBrowseFramesBackButton(x,y): #resets phases of program
data.showBestGlasses=True
data.browseFrames=False
data.browseFramesBackButton.unclicked()
data.browseFramesButton.unclicked()
data.ableToClick=True
data.canSeeBestGlasses=True
if clickPreviousPage(x,y): #if they click the forward or back arrows
data.pageNumber-=1
if clickNextPage(x,y):
data.pageNumber+=1
#link to website if they click a pair of glasses
for glasses in data.glassesList:
if glasses.isClicked(x,y):
glasses.linkToSite()
glasses.clicked=False
def goToBrowseFramesPhase():
#resets browse frames phase booleans
data.canSeeBestGlasses=False
data.showBestGlasses=False
data.browseFrames=True
data.pageNumber=1
data.ableToClick=False
def goToTryThemOnPhase():
#resets try them on phase booleans
data.showBestGlasses=False
data.tryThemOn=True
data.ableToClick=False
data.browseFramesCantBeClicked=True
def resetProgram():
storePhaseBooleans() #tells us the phase in the program we're at
makeButtons()
initTryOnData()
makeImageLabels(data.root)
resetData()
def doneWithDotsMouseUp(x,y):
if (clickBrowseFrames(x,y) and data.ableToClick==True and
data.browseFramesCantBeClicked==False):
goToBrowseFramesPhase() #should be in browse frames
elif data.ableToClick==True and clickTryThemOn(x,y):
goToTryThemOnPhase() #should be in try them on
elif clickSeeBestGlasses(x,y) and data.canSeeBestGlasses==True:
data.canSeeBestGlasses=True #goes to see best glasses phase
data.showBestGlasses=True
data.doneWithDotsButton.unclicked()
data.ableToClick=True
data.canSeeBestGlasses=True
data.browseFramesCantBeClicked=False
elif clickInResetButton(x,y) and data.canSeeBestGlasses==True:
resetProgram()
if data.browseFrames==True:
data.canSeeBestGlasses=False
browseFramesMouseUp(x,y)
elif data.tryThemOn==True: #goes to next mouse phase
tryThemOnMouseUp(x,y)
#moves the dots around when the mouse is released
def clickedMouseUp(event):
n=data.clickedDot.n
data.dots.remove(data.clickedDot) #removes and redraws clicked dot
data.dots.append(Dot(event.x,event.y,n,data.highlightColor))
data.dots=sortDots() #re-sorts them every time it draws a new one
data.startButton.unclicked()
def onMouseUp(event):
#mouse up functions based on various phases in programs
if data.clicked==True:
data.draggingDots=False
clickedMouseUp(event)
elif data.start==False:
haventStartedMouseUp(event.x,event.y)
elif data.pause==False:
haventPausedMouseUp(event.x,event.y)
elif data.pause==True:
havePausedDotsMouseUp(event.x,event.y)
if data.doneWithDots==True:
doneWithDotsMouseUp(event.x,event.y)
def haventStartedMouse(x,y):
#waits for the start button
if clickInStartButton(x,y):
data.startButton.clicked()
def haventPausedMouse(x,y):
if clickInPhotoButton(x,y):
data.photoIconClicked=True
ret,frame=data.cap.read()
frame=cv2.flip(frame,1)
#find the face bounding box on the paused frame
data.facerect=detectFace(frame)
def havePausedDotsMouse(x,y):
#if dots aren't None
if data.dots:
for dot in data.dots:
if dot.clickInside(x,y): #checks every dot
clickColor=rgbString(115,107,153)
data.clicked=True
data.draggingDots=True #it's moving the dots and they're clicked
data.clickedDot=dot
n=dot.n
data.dots.remove(dot) #takes out dot, puts a new one
newDot=Dot(x,y,n,clickColor)
data.dots.append(newDot)
data.clickedDot=newDot
data.dots=sortDots() #sorts dots every time
return
data.clicked=False #unclicked
if clickDoneWithDots(x,y):
data.doneWithDotsButton.clicked() #if they're done, clicks the button
def checkClickGlassesPair(x,y):
#This is where it draws the purple rectangles around the current
#pair of glasses on the try-on screen
x0=900
x1=1130
ya1,ya2=165,273 #boundaries for where the rectangles go
yb1,yb2=296,402
yc1,yc2=431,539
yd1,yd2=573,680
if x>x0 and x<x1: #changes current glasses pair depending on click
if y>ya1 and y<ya2:
data.glassesPair=0
elif y>yb1 and y<yb2:
data.glassesPair=1
elif y>yc1 and y<yc2:
data.glassesPair=2
elif y>yd1 and y<yd2:
data.glassesPair=3
def tryThemOnMouse(x,y):
data.ableToClick=True
if clickTryThemOnBackButton(x,y): #checks for the back button
data.tryThemOnBackButton.clicked()
checkClickGlassesPair(x,y) #checks if they've clicked different glasses
if clickCameraIcon(x,y): #checks if they've taken a photo
if data.pausedTryOn==False: #if it's the camera, do the camera thing
data.cameraClickedTryOn=True
data.shadedPlayButton=False
elif data.pausedTryOn==True: #if it's the play button, do that thing
data.shadedPlayButton=True
data.cameraClickedTryOn=True
def browseFramesMouse(x,y):
#sets up limits for what can and can't be clicked
data.canHitBrowseFrames=False
if clickBrowseFramesBackButton(x,y):
#clicks button and does button things
data.browseFramesBackButton.clicked()
data.canHitBrowseFrames=True
for glasses in data.glassesList:
#checks for glasses clicks and animates them accordingly
if glasses.isClicked(x,y):
glasses.clicked=True
def clickInResetButton(x,y):
x0=data.width-222
x1=data.width-82
y0=700
y1=765
return x>x0 and x<x1 and y>y0 and y<y1
def doneWithDotsMouse(x,y):
#checks for various clicked buttons and acts accordingly
if data.showBestGlasses==True and clickBrowseFrames(x,y):
data.browseFramesButton.clicked()
if data.showBestGlasses==True and clickTryThemOn(x,y):
data.tryThemOnButton.clicked()
if clickSeeBestGlasses(x,y):
data.seeBestGlassesButton.clicked()
if clickInResetButton(x,y):
data.resetButton.clicked()
if data.browseFrames==True:
browseFramesMouse(x,y)
if data.tryThemOn==True:
tryThemOnMouse(x,y)
def onMouseDown(event):
#checks the phase and does the appropriate mouse function
if data.start==False:
haventStartedMouse(event.x,event.y)
elif data.pause==False:
haventPausedMouse(event.x,event.y)
elif data.pause==True:
havePausedDotsMouse(event.x,event.y)
if data.doneWithDots==True:
doneWithDotsMouse(event.x,event.y)
#this function is used for the click and drag dots feature
def clickAndDrag(event):
clickColor=rgbString(115,107,153)
if data.draggingDots==True:
n=data.clickedDot.n
data.dots.remove(data.clickedDot)
newDot=Dot(event.x,event.y,n,clickColor)
data.dots.append(newDot)
data.clickedDot=newDot
data.dots=sortDots()
###########################################################
#################### DRAW FUNCTIONS #######################
###########################################################
#these are my draw functions, listed in order they appear in my program,
#with drawAll at the bottom
def drawBackground(canvas):
#draws the purple area in the back of each screen
data.backgroundColor=rgbString(66,51,98)
rectW,rectH=1200,800
data.offset=50
offset=data.offset
canvas.create_rectangle(0,0,rectW*2,rectH*2,
fill=data.backgroundColor,width=0)
def drawFrame(canvas,img):
#draws the webcam feed
xoffset=100
yoffset=209
canvas.create_image(xoffset,yoffset,anchor=NW,image=img)
def drawStartScreen(canvas):
#sets up size of the button
#draws the background
#canvas.create_rectangle(0,0,data.width*2,data.height*2,
# fill=data.backgroundColor,width=0)
mainText="FourEyes"
font="Avenir 125 bold"
#draws the title
canvas.create_text(data.width/2,data.height/4,anchor="c",
fill=data.highlightColor,text=mainText,font=font)
filler1="Learn which glasses frames will look"
filler2="the best with your face shape."
font="Avenir 34"
#draws the descriptions
canvas.create_text(data.width/2,1.65*data.height/4-5,anchor="c",
fill=data.accentColor,text=filler1,font=font)
canvas.create_text(data.width/2,1.9*data.height/4-5,anchor="c",
fill=data.accentColor,text=filler2,font=font)
data.startButton.draw(canvas)
#take a photo screen
def drawTakeAPhotoScreen(canvas):
text="Take a Photo"
font="Helvetica 90 bold"
canvas.create_text(data.width/2,data.height/8,text=text,font=font,
fill="white")
xoffset=80
yoffset=188
x2=720
y2=680
canvas.create_rectangle(xoffset,yoffset,x2,y2,fill=data.accentColor,width=7,
outline=data.highlightColor)
drawInstructionText(canvas)
if data.photoIconClicked==False:
drawPhotoIcon(canvas)
else:
drawClickedPhotoIcon(canvas)
def drawInstructionText(canvas):
#draws take a photo instructions
rule1="1. Hair pushed back"
rule2="2. Face in the frame"
rule3="3. Blank expression"
rules=[rule1,rule2,rule3]
color=data.accentColor
spc=100
font="Avenir 40"
x0,y0=763, 229
for i in xrange(len(rules)):
canvas.create_text(x0,y0+spc*i,anchor="nw",font=font,text=rules[i],
fill=data.highlightColor)
def drawClickedPhotoIcon(canvas):
#inset clicked photo icon
img=ImageTk.PhotoImage(file="source/clickedcamera.gif")
data.imageLabel._image_cache=img
x=940+5
y=600+5
canvas.create_image(x,y, anchor="c",image=img)
def drawPhotoIcon(canvas):
#the button to take a photo
img=ImageTk.PhotoImage(file="source/camera.gif")
data.imageLabel._image_cache=img
x=940
y=600
canvas.create_image(x,y, anchor="c",image=img)
#the screen for dragging the dots
def drawDotScreen(canvas):
drawBackground(canvas)
text="Drag the Dots"
font="Helvetica 90 bold"
color=data.highlightColor
canvas.create_text(data.width/2,data.height/8,text=text,font=font,
fill=color)
xoffset=80
yoffset=188
x2=720
y2=680
canvas.create_rectangle(xoffset,yoffset,x2,y2,fill=data.accentColor,width=7,
outline=data.highlightColor)
drawDotInstructions(canvas)
data.doneWithDotsButton.draw(canvas)
#draws the instrctions for the dot screen
def drawDotInstructions(canvas):
text2="For the best results,"
text3="match the example as"
text4="precisely as you can."
color=data.accentColor
font="Avenir 35"
x=70+880
y0=575
spacing=45
canvas.create_text(x,y0,anchor="c",text=text2,font=font,fill=color)
canvas.create_text(x,y0+spacing,anchor="c",text=text3,font=font,fill=color)
canvas.create_text(x,y0+2*spacing,anchor="c",text=text4,font=font,
fill=color)
drawInstructionImage(canvas)
#draws the little picture of the dude I drew to show people how to
#drag dots
def drawInstructionImage(canvas):
img=ImageTk.PhotoImage(file="source/tpFace.gif")
data.imageLabel._image_cache=img
x=90+715
y=190
canvas.create_image(x,y,anchor="nw",image=img)
#next screen: tell them their face shape
def drawFaceShapeScreen(canvas):
drawBackground(canvas)
data.pause=False
text1="Your face shape is"
text2=data.faceShape[0].upper()+data.faceShape[1:]
filename=str(data.faceShape)+".txt"
writeup=readFile("source/shapes/"+filename)
font1="Helvetica 50 "
font2="Avenir 170 bold"
y0=100
spacing=125
canvas.create_text(data.width/2,y0,anchor="c",text=text1,font=font1,
fill=data.highlightColor)