forked from sd18spring/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (101 loc) · 4.18 KB
/
main.py
File metadata and controls
111 lines (101 loc) · 4.18 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
from fingerTrack import *
from canvas import *
import cv2
from optparse import OptionParser
import time
def init_opts():
"""
This function runs the different modes of our program with input in the
command line
"""
parser = OptionParser()
parser.add_option("-d", action="store_false",
dest="disappr", default=True,
help="To run the program in the drawing mode")
parser.add_option("-t", action="store_true",
dest="disappr", default=True,
help="To run the program in the tailing mode where the lines disappear after a period of time")
parser.add_option("-g", action="store_true",
dest="game", default=False,
help="To run the program in the gaming mode where you try to hit boxes")
parser.add_option("-l", "--length", action="store", type='int',
dest="length", default=3,
help="The starting length of the line")
options, args = parser.parse_args()
return options, args
def main():
""" This function puts together the methods from classes in other files to
run the program
"""
font = cv2.FONT_HERSHEY_SIMPLEX
options, args = init_opts()
track = finger_track()
cap = cv2.VideoCapture(0)
# this makes the canvas larger for all modes except for the game because the
# game needs a smaller canvas to be less frustrating to play
scaler = 2
if options.game:
scaler = 1
newCanvas = canvas(cap.get(3), cap.get(4), scaler)
disappr = options.disappr
track.pathlength = options.length
game_time = 30
current_time = 1
start = time.time()
while True:
if time.time() - start > 1 and options.game:
current_time += 1
start = time.time()
# Display the page that shows your score when the time is up until you
# press the q key
if current_time == game_time+1:
while True:
cv2.putText(newCanvas.new_canvas, 'Yay!!!', (int(newCanvas.width/2-100), int(newCanvas.height/2)), font, 3, (255, 0, 0), 2)
cv2.putText(newCanvas.new_canvas, 'Your final score is:', (int(newCanvas.width/2-300), int(newCanvas.height/2+50)), font, 2, (0, 0, 255), 2)
cv2.putText(newCanvas.new_canvas, str(newCanvas.points)+'!!!', (int(newCanvas.width/2-75), int(newCanvas.height/2+50)+75), font, 3, (0, 255, 0), 2)
newCanvas.show_canvas()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
break
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
# This section tracks the color red that is in the frame
hsv = track.BGR2HSV(frame)
redMask = track.red_mask(hsv)
mask = cv2.bilateralFilter(redMask, 10, 40, 40)
mask = cv2.blur(mask, (5, 5))
res = cv2.bitwise_and(frame, frame, mask=redMask)
cv2.imshow('original', res)
mask = cv2.blur(mask, (20, 20))
track.find_center(mask, frame, newCanvas, disappr=disappr)
track.draw(newCanvas)
# This section runs the game option if it was selected
if options.game:
if newCanvas.points == 0:
if newCanvas.run == False:
newCanvas.make_rect()
newCanvas.show_rect()
flag = newCanvas.in_rect(track.cx, track.cy)
if flag == True:
newCanvas.addpoints(track)
newCanvas.clear()
newCanvas.show_rect()
newCanvas.show_rect()
cv2.putText(newCanvas.new_canvas, 'Time left: '+str(game_time-current_time), (0, 15), font, .5, (255, 255, 255), 1)
newCanvas.show_canvas()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('s'):
newCanvas.save_drawing()
break
if cv2.waitKey(1) & 0xFF == ord('d'):
if disappr:
disappr = False
else:
disappr = True
if cv2.waitKey(1) & 0xFF == ord('c'):
newCanvas.clear()
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()