-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqr_identification .py
244 lines (228 loc) · 10.1 KB
/
qr_identification .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
# -*- coding: utf-8 -*-
import time
import picamera
import math
import threading
from SimpleCV import *
import RPi.GPIO as GPIO
rwheelback = 16
rwheelfor= 18
lwheelback = 13
lwheelfor = 15
GPIO.setmode(GPIO.BOARD)
GPIO.cleanup()
GPIO.setup(22,GPIO.OUT)
p=GPIO.PWM(22,50)
CycleValue=0
p.start(3.5)
p.ChangeDutyCycle(7.5)
tries=0
flag=0
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',
)
#saving captured images from stream
def filenames(lock):
print('Waiting for lock')
lock.acquire()
try:
print('Acquired lock')
yield 'image.jpg'
print("True")
finally:
lock.release()
print("released")
time.sleep(0.3)
#Image capturing with 604x480 resolution and 30 fps
def capture(lock):
with picamera.PiCamera(resolution=(640,480), framerate=30) as camera:
camera.start_preview()
time.sleep(1)
start = time.time()
camera.capture_sequence(filenames(lock), use_video_port=True)
finish = time.time()
#function for acquiring thread and passing it to processing function
def acquire(lock):
num_tries = 0
num_acquires = 0
logging.debug('Trying to acquire')
have_it = lock.acquire()
try:
num_tries += 1
if have_it:
logging.debug('Iteration %d: Acquired', num_tries)
s=time.time()
processing()
print("Third function time",(time.time()-s))
num_acquires += 1
else:
logging.debug('Iteration %d: Not acquired', num_tries)
finally:
if have_it:
lock.release()
time.sleep(1)
logging.debug('Done after %d iterations', num_tries)
#function for image processing,qr code recognition and following
def processing():
delay=0
delayt=0
indelay=0
global flag
global tries
global CycleValue
global p
ok=0
try:
GPIO.setmode(GPIO.BOARD)
GPIO.setup(rwheelback,GPIO.OUT)
GPIO.setup(rwheelfor,GPIO.OUT)
GPIO.setup(lwheelfor,GPIO.OUT)
GPIO.setup(lwheelback,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
try:
if(Image("image.jpg")):
im=Image("image.jpg")
print("Got an image!")
if(im.findBarcode()):
bar=im.findBarcode()
for i in range(len(bar)):
#Here we check the data below with the one the QR code has
if(bar[i].data=="https://weather.com"):
center=bar.center()
center=center[0][0]
center_im=im.width/2
bound=im.width
area=bar.area()
if(area<(0.05*im.area())):
delay=(im.area()/area)*0.02
delayt=delay/10
elif(area>(25/100*im.area()) and area<(im.area())):
delay=(im.area()/area)*0.1
delayt=delay/15
else:
delay=0
delayt=0
print "Delay", delay
print "Image area: ", im.area()
print "Barcode area: ", area
print "Center is: ", center
print "Bound is: ", bound
print("\n")
if(flag==1):
if((CycleValue>9 and CycleValue<10)or (CycleValue<6 and CycleValue>5)):
indelay=0.35
if(center<((40*bound)/100) ):
indelay=0.5
elif(center>((60*bound)/100)):
indelay=0.5
elif(CycleValue>11 or CycleValue<4):
indelay=0.8
delayt=indelay*delay*0.65
print "Turn delay", delayt
print "CycleValue: ",CycleValue
ok=0
if((CycleValue==7.5 and flag==1) or (flag==0 and center>=((40*bound)/100) and center<=((60*bound)/100))):
if(area>(im.area()*15/100) and area<(im.area())):
print("Going back!")
GPIO.output(rwheelback,GPIO.HIGH)
GPIO.output(lwheelback,GPIO.HIGH)
time.sleep(delay)
GPIO.output(rwheelback,GPIO.LOW)
GPIO.output(lwheelback,GPIO.LOW)
elif(area<(im.area()*5/100)):
print("Going forward!")
GPIO.output(rwheelfor,GPIO.HIGH)
GPIO.output(lwheelfor,GPIO.HIGH)
time.sleep(delay)
GPIO.output(rwheelfor,GPIO.LOW)
GPIO.output(lwheelfor,GPIO.LOW)
else:
print "It's fine"
elif((CycleValue>9 and CycleValue<20 and flag==1) or (center<((40*bound)/100) and flag==0)):
flag=0
if(area>(im.area()*15/100) and area<(im.area())):
print ("Back Right")
GPIO.output(lwheelback,GPIO.HIGH)
time.sleep(delayt)
GPIO.output(lwheelback,GPIO.LOW)
elif(area<(im.area()*5/100)):
print("Going Left!")
GPIO.output(rwheelfor,GPIO.HIGH)
time.sleep(delayt)
GPIO.output(rwheelfor,GPIO.LOW)
else:
print("It's fine.")
ok=1
elif((CycleValue<6 and CycleValue>0 and flag==1) or (center>((60*bound)/100) and flag==0)):
flag=0
if(area>(im.area()*15/100) and area<(im.area())):
print ("Back Left")
GPIO.output(rwheelback,GPIO.HIGH)
time.sleep(delayt)
GPIO.output(rwheelback,GPIO.LOW)
elif(area<(im.area()*5/100)):
print("Going Right!")
GPIO.output(lwheelfor,GPIO.HIGH)
time.sleep(delayt)
GPIO.output(lwheelfor,GPIO.LOW)
else:
print("It's fine.")
ok=1
time.sleep(0.1)
if(ok==1 and CycleValue>0 and CycleValue<12):
p.ChangeDutyCycle(CycleValue)
flag=1
elif(ok==0 and CycleValue>0 and CycleValue<12 and (CycleValue<7.5 or CycleValue>7.5)):
if(center<((30*bound/100))):
if(CycleValue>7.5 and CycleValue<12):
CycleValue=CycleValue-2
elif(CycleValue<7.5 and CycleValue>0):
CycleValue=CycleValue+2
elif(center>((70*bound)/100)):
if(CycleValue>7.5 and CycleValue<12):
CycleValue=CycleValue-2
elif(CycleValue<7.5 and CycleValue>0):
CycleValue=CycleValue+2
p.ChangeDutyCycle(CycleValue)
flag=1
elif(ok==0 and (CycleValue==0 or CycleValue==7.5)):
p.ChangeDutyCycle(7.5)
flag=0
tries=0
print("\n")
else:
print ("Nothing found. Looking around...")
tries=tries+1
print tries
if(tries>=5):
tries=0
if(CycleValue==0):
CycleValue=1.5
p.start(CycleValue)
flag=1
CycleValue = CycleValue+2
if(CycleValue>12):
print("Nothing to be found!")
CycleValue=0
p.ChangeDutyCycle(7.5)
elif(CycleValue>1 and CycleValue<12):
p.ChangeDutyCycle(CycleValue)
print "Val:", CycleValue
time.sleep(1)
except IOError:
print("No image file to open")
if(GPIO.setmode()):
print("True")
GPIO.cleanup()
else:
print("No setmode!")
except:
print("No setmode trying again")
if __name__ == '__main__':
lock=threading.Lock()
while True:
t = threading.Thread(target=first,args=(lock,))
t.start()
time.sleep(2)
t1= threading.Thread(target=second,args=(lock,))
t1.start()