Skip to content

Commit eabfc91

Browse files
committed
wip #70
1 parent 40a72ab commit eabfc91

File tree

7 files changed

+55
-11
lines changed

7 files changed

+55
-11
lines changed

.camera.py.swp

-16 KB
Binary file not shown.

camera.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,19 @@ def __init__(self):
9494
self._cnn_classifier_default = self._cnn_classifiers[cnn_model]
9595

9696
self._camera.grab_start()
97+
self._image_cv = self.get_image()
9798

9899
super(Camera, self).__init__()
99100

100101
def get_image(self):
101102
return image.Image(self._camera.get_image_bgr())
102103

104+
def get_image_cv_jpeg(self):
105+
return self._image_cv.to_jpeg()
106+
107+
def set_image_cv(self, image):
108+
self._image_cv = image
109+
103110
def get_image_jpeg(self):
104111
return self._camera.get_image_jpeg()
105112

@@ -218,20 +225,26 @@ def get_average(self):
218225
return avg
219226

220227
def find_line(self):
221-
img = self.get_image().binarize()
222-
#img = img.erode().dilate()
228+
avg = self.get_image().get_average()
229+
img = self.get_image().binarize(int((avg[0]+avg[2])/2))
230+
img = img.erode().dilate()
223231
slices = [0,0,0]
224232
blobs = [0,0,0]
225233
slices[0] = img.crop(0, int(self._camera.out_rgb_resolution[1]/1.2), self._camera.out_rgb_resolution[0], self._camera.out_rgb_resolution[1])
226234
slices[1] = img.crop(0, int(self._camera.out_rgb_resolution[1]/1.5), self._camera.out_rgb_resolution[0], int(self._camera.out_rgb_resolution[1]/1.2))
227235
slices[2] = img.crop(0, int(self._camera.out_rgb_resolution[1]/2.0), self._camera.out_rgb_resolution[0], int(self._camera.out_rgb_resolution[1]/1.5))
236+
y_offset = [int(self._camera.out_rgb_resolution[1]/1.2),
237+
int(self._camera.out_rgb_resolution[1]/1.5),
238+
int(self._camera.out_rgb_resolution[1]/2.0)]
228239
coords = [-1, -1, -1]
229240
for idx, slice in enumerate(slices):
230-
blobs[idx] = slice.find_blobs(minsize=300/(self._cv_image_factor * self._cv_image_factor), maxsize=8000/(self._cv_image_factor * self._cv_image_factor))
241+
blobs[idx] = slice.find_blobs(minsize=2000/(self._cv_image_factor * self._cv_image_factor), maxsize=8000/(self._cv_image_factor * self._cv_image_factor))
231242
if len(blobs[idx]):
232243
coords[idx] = (blobs[idx][0].center[0] * 100) / self._camera.out_rgb_resolution[0]
233-
logging.info("line coord: " + str(idx) + " " + str(coords[idx])+ " area: " + str(blobs[idx][0].area()))
234-
244+
#logging.info("line coord: " + str(idx) + " " + str(coords[idx])+ " area: " + str(blobs[idx][0].area()))
245+
blob = blobs[idx][0]
246+
img.draw_rect(blob.left, y_offset[idx] + blob.top, blob.right, y_offset[idx] + blob.bottom, (0,255,0), 5)
247+
self.set_image_cv(img)
235248
return coords
236249

237250
def find_signal(self):

cv/blob.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def area(self):
5050
def minAreaRect(self):
5151
return cv2.minAreaRect(self._contour)
5252

53+
def contour(self):
54+
return self._contour
55+
5356
@classmethod
5457
def sort_distance(cls, point, blobs):
5558
return sorted(blobs, key=lambda blob: (point[0] - blob.bottom))

cv/image.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,16 @@ def grayscale(self):
137137
data = cv2.cvtColor(self._data, cv2.COLOR_BGR2GRAY)
138138
return Image(data)
139139

140+
def blackwhite(self):
141+
data = cv2.threshold(self._data, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
142+
return Image(data)
143+
140144
def invert(self):
141145
data = cv2.bitwise_not(self._data)
142146
return Image(data)
143147

144148
def binarize(self, threshold = -1):
145149
data = cv2.cvtColor(self._data, cv2.COLOR_BGR2GRAY)
146-
#data = cv2.adaptiveThreshold(data, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 0)
147150
if threshold < 0:
148151
data = cv2.adaptiveThreshold(data, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, max((self._kernel.shape[0]/2*2)+1, 3), 3)
149152
else:
@@ -161,10 +164,7 @@ def get_average(self):
161164
def find_blobs(self, minsize=0, maxsize=10000000):
162165
blobs = []
163166
image = contours = hyerarchy = None
164-
if "2.4" in cv2.__version__:
165-
contours, hyerarchy = cv2.findContours(self._data, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
166-
else:
167-
image, contours, hyerarchy = cv2.findContours(self._data, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
167+
image, contours, hyerarchy = cv2.findContours(self._data, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
168168

169169
for c in contours:
170170
area = cv2.contourArea(c)
@@ -280,6 +280,12 @@ def find_ar_code(self):
280280
(rect[0][1]+rect[1][1]+rect[2][1]+rect[3][1])/4])
281281
return {"codes": codes, "positions": positions}
282282

283+
def draw_blob(self, blob):
284+
cv2.drawContours(self._data, blob.contour(), -1, (0,255,0))
285+
286+
def draw_rect(self, x1, y1, x2, y2, color, thickness):
287+
cv2.rectangle(self._data, (x1,y1), (x2,y2), color, thickness)
288+
283289
def to_jpeg(self):
284290
ret, jpeg_array = cv2.imencode('.jpeg', self._data)
285291
return np.array(jpeg_array).tostring()

main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ def handle_video_stream():
204204
return Response(video_stream(cam), headers=h, mimetype="multipart/x-mixed-replace; boundary=--BOUNDARYSTRING")
205205
except: pass
206206

207+
def video_stream_cv(cam):
208+
while not app.shutdown_requested:
209+
frame = cam.get_image_cv_jpeg()
210+
yield ("--BOUNDARYSTRING\r\n" +
211+
"Content-type: image/jpeg\r\n" +
212+
"Content-Length: " + str(len(frame)) + "\r\n\r\n" +
213+
frame + "\r\n")
214+
215+
@app.route("/video/stream/cv")
216+
def handle_video_stream_cv():
217+
try:
218+
h = Headers()
219+
h.add('Age', 0)
220+
h.add('Cache-Control', 'no-cache, private')
221+
h.add('Pragma', 'no-cache')
222+
return Response(video_stream_cv(cam), headers=h, mimetype="multipart/x-mixed-replace; boundary=--BOUNDARYSTRING")
223+
except: pass
224+
225+
226+
207227
@app.route("/photos", methods=["GET"])
208228
def handle_photos():
209229
logging.info("photos")

photos/metadata.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

program.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ def run(self):
168168
get_event().wait_event_generators()
169169
except RuntimeError as re:
170170
logging.info("quit: " + str(re))
171+
raise
171172
except Exception as e:
172173
logging.info("quit: " + str(e))
174+
raise
173175

174176
finally:
175177
try:

0 commit comments

Comments
 (0)