-
Notifications
You must be signed in to change notification settings - Fork 5
/
ogplab.py
276 lines (239 loc) · 7.93 KB
/
ogplab.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
#!/usr/bin/env python2
# ogplab2.py
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import time
import serial
import os
import ircam
import picamera
from fractions import Fraction
from SimpleCV import Camera, Image
mySet = set()
brightpixels = 0
darkpixels = 0
l = int(4)
s = serial.Serial('/dev/ttyUSB0', 9600)
class so(object):
def __init__(self, side, js, wsh, wsh2, c2, cam_mode, c):
self.cam_mode=cam_mode
self.c2=c2
self.js = js
self.wsh = wsh
self.wsh2 = wsh2
self.l = side
self.x = 12
self.y = -12
self.w = 0
self.p = 1
c2=self.c2
l = self.l
x = -1
y = -1
self.countdownA = int(self.l)
self.countdownB = 1
self.countdownC = 0
self.left = 0
self.right = 1
self.up = 0
self.down=0
i = 0
brightpixels=0
darkpixels =0
blobs = 0
def histo(self): ## this def is the "light meter" part---
cam_mode = self.cam_mode ## the pic gets cataloged if true ---
js = self.js
w = self.w
cent = 0
rgb1 = 0
c2 = self.c2
wsh = self.wsh
wsh2 = self.wsh2
i=0
brightpixels=0
darkpixels=0
blobs = 0
if cam_mode == 3: ## sort out the confusing cam modes
img1 = c2.getImage()
time.sleep(.25)
if cam_mode==1:
with picamera.PiCamera() as camera:
camera.resolution = (544, 288)
camera.capture('imagesmall.jpg')
img1 = Image('imagesmall.jpg')
if cam_mode==2:
with picamera.PiCamera() as camera:
camera.resolution = (544, 288)
camera.capture('imagesmall.jpg')
img1 = Image('imagesmall.jpg')
blobs = img1.findBlobs()
time.sleep(0.5)
if blobs:
##find the blob centroid and cut it out 20x20
crop1 = blobs[-1].x
crop2 = blobs[-1].y
crop3 = crop1 - 10
crop4 = crop2 - 10
thumbnail = img1.crop(crop3,crop4,20,20)
img2 = thumbnail
hist = img2.histogram(20)
## split the thumb into 20 levels of darkness
brightpixels = hist[10]
## 10 is where the darkest of the light pixels accumulate
print brightpixels
##while i < 20:
##old code for if you want to split the histogram in two
##if (i < 10):
##darkpixels = darkpixels + hist[i]
##self.darkpixels = darkpixels
##print hist[i]
##else:
##brightpixels = brightpixels + hist[i]
##self.brightpixels = brightpixels
##print hist[i]
##i = i + 1
if (brightpixels<400): ## heres where it decides to catalog the pic or not...
wsh.write_message(wsh2, "histo_" + str(darkpixels) + "_" + str(brightpixels))
print "blob"
x = self.x
y = self.y
p = self.p
p = p + 1
thumb1 = "/var/www/html/images/thumbs/thumb"
thumb3 = ".png"
thumbpath = thumb1 + str(p) + thumb3
print thumbpath
thumbnail.save(thumbpath)
img1.drawText("blob = True", 10, 35, color=(255,255,255),fontsize=30)
img1.drawText("search_mode", 10, 5, color=(0,0,255),fontsize=40)
img1.drawText("blob centroid", blobs[-1].x,blobs[-1].y, color=(255,255,255),fontsize=20)
img1.drawCircle((blobs[-1].x,blobs[-1].y),30,color=(255,255,0))
img1.drawCircle((blobs[0].centroid()),10,color=(255,255,255))
print blobs[-1].meanColor()
rgb1 = blobs[-1].meanColor()
cent = blobs[-1].centroid()
pth1 = "/var/www/html/images/image"
pth3 = ".png"
pth = pth1 + str(p) + pth3
print pth
img1.save(pth)
time.sleep(0.5)
self.p = p
mySet.add((p,x,y,w,cent,rgb1))
self.mySet = mySet
wshx = str(self.x)
wshy = str(self.y)
centroidx = int(cent[0])
centroidy=int(cent[1])
rcolor=rgb1[0]
gcolor=rgb1[1]
bcolor=rgb1[2]
rcolor=int(rcolor)
gcolor=int(gcolor)
bcolor=int(bcolor)
wsh.write_message(wsh2, "rgb_" + str(rcolor)+"_"+str(gcolor)+"_"+str(bcolor))
wsh.write_message(wsh2, "x_" + str(centroidx)+"_"+str(centroidy))
img1.save(js.framebuffer)
time.sleep(0.5)
wsh.write_message(wsh2, "d_" + wshx + "_" + wshy + "_" + str(p) )
else:
print "senosor dark"
print darkpixels,'_', brightpixels
blobs = 0
wsh.write_message(wsh2, "histo_" + str(darkpixels) + "_" + str(brightpixels))
else:
wshx = str(self.x)
wshy = str(self.y)
wsh.write_message(wsh2, wshx + " " + wshy + "dark")
img1.save(js.framebuffer)
print "no blob"
def run(self): ## this def is the movement iterator
wsh = self.wsh
wsh2 = self.wsh2
countdownA = self.countdownA
countdownB = self.countdownB
countdownC = self.countdownC
left = self.left
right = self.right
up = self.up
down = self.down
x = self.x
y = self.y
wsh2 = self.wsh2
if (countdownB < countdownA):
print "ctb",countdownB
print right
if (right > 0):
print "right- 0"
print right
s.write('p')
time.sleep(1)
x = x + 1
self.x = x
elf = self.histo()
wsh.write_message(wsh2, "m" )
right = right - 1
self.right = right
if (right == 0):
down = countdownB
self.down = down
if (down>0):
print "ctb",countdownB
print "down"
print down
s.write('l')
time.sleep(1)
y = y - 1
self.y = y
elf = self.histo()
wsh.write_message(wsh2, "m" )
down = down - 1
self.down = down
if (down == 0):
countdownB += 1
self.countdownB = countdownB
left = countdownB
self.left = left
print "ctb", countdownB
if (left>0):
print "left"
print left
print "ctb",countdownB
s.write('k')
time.sleep(1)
x = x - 1
self.x = x
elf = self.histo()
wsh.write_message(wsh2, "m" )
left = left - 1
self.left = left
if (left == 0):
print "ctb",countdownB
up = countdownB
self.up = up
if (up>0):
print "up"
print "ctb",countdownB
print up
s.write('o')
time.sleep(1)
y = y + 1
self.y = y
elf = self.histo()
wsh.write_message(wsh2, "m" )
up = up - 1
self.up = up
if (up == 0):
countdownB += 1
self.countdownB = countdownB
right = countdownB
self.right = right
print "ctb",countdownB
else:
wsh = self.wsh
wsh2 = self.wsh2
wsh.write_message(wsh2, "map complete" )
print "done"