-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinalProject.py
More file actions
110 lines (76 loc) · 2.07 KB
/
finalProject.py
File metadata and controls
110 lines (76 loc) · 2.07 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
from myro import *
from line_following import *
from time import sleep
from dominantColor import getColor
from PIL import Image
from tesserwrap import Tesseract
from playnote import bp
from time import time, strftime
from makeSong import makeFile
import math
initialize("/dev/tty.IPRE6-193907-DevB")
RUN_OVER_TIME = 2
OBSTACLE_THRESHOLD = 600
INTERVAL_BETWEEN_TURNS= 4
def getPILImage(should_save=True):
pic = takePicture("grey")
savePicture(pic, "current.png")
if should_save:
savePicture(pic, "photos/"+strftime("%Y-%m-%d %H:%M:%S")+".png")
img = Image.open("current.png")
return img
def sanitizeInput(inputStr):
valid_notes = ["A", "B", "I", "O", "Z", "E"]
inputStr = inputStr.upper()
for note in valid_notes:
if note in inputStr:
return note
def playNotes(notes):
startTime = 0
for k in sorted(notes):
bp(int(int(k - startTime)/16), notes[k])
startTime = k
sleep(4)
def turnRight90():
turnRight(.5, 1.31)
def turnLeft90():
turnLeft(.5, 1.32)
goStraight()
firstTime = time()
lastTurnTime = time()
notes = {}
while 1:
correctionTime = time()
if not correctYourself():
stop()
if len(notes) > 0:
print notes
playNotes(notes)
makeFile(notes, firstTime, filename=("sounds/" + strftime("%Y-%m-%d %H:%M:%S")+".wav"))
break
correctionTime = time() - correctionTime
lastTurnTime = lastTurnTime + math.floor(correctionTime) # Move the last turn time forward
curTime = time()
print "correctionTime = %d, lastTurnTime = %d, curTime=%d" % (correctionTime, lastTurnTime, curTime)
if (curTime - lastTurnTime) > INTERVAL_BETWEEN_TURNS:
stop()
turnLeft90()
image = getPILImage()
ocr = Tesseract().ocr_image(image)
ocr = sanitizeInput(ocr)
if ocr is None:
image = getPILImage(False)
ocr = Tesseract().ocr_image(image)
ocr = sanitizeInput(ocr)
if ocr is None:
image = getPILImage(False)
ocr = Tesseract().ocr_image(image)
ocr = sanitizeInput(ocr)
print "note: %s" % ocr
if ocr is not None:
notes[int(time() - firstTime)] = ocr
turnRight90()
lastTurnTime = time()
correctYourself()
goStraight()
sleep(6)