Skip to content

Commit 0283336

Browse files
committed
image average data
1 parent 938d952 commit 0283336

File tree

9 files changed

+60
-13
lines changed

9 files changed

+60
-13
lines changed

camera.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,13 @@ def exit(self):
197197
def calibrate(self):
198198
img = self._camera.getImage()
199199
self._background = img.hueHistogram()[-1]
200-
200+
201+
def get_average(self):
202+
self._image_lock.acquire()
203+
avg = self.get_image(0).get_average()
204+
self._image_lock.release()
205+
return avg
206+
201207
def find_line(self):
202208
self._image_lock.acquire()
203209
img = self.get_image(0).binarize()

data/program_img_average_test.data

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"dom_code": "<xml xmlns=\"http://www.w3.org/1999/xhtml\"><block type=\"controls_whileUntil\" x=\"9\" y=\"103\"><field name=\"MODE\">WHILE</field><value name=\"BOOL\"><block type=\"logic_boolean\"><field name=\"BOOL\">TRUE</field></block></value><statement name=\"DO\"><block type=\"text_print\"><value name=\"TEXT\"><block type=\"coderbot_cam_average\"><field name=\"RETVAL\">V</field></block></value></block></statement></block></xml>", "code": "while True:\n get_prog_eng().check_end()\n get_cam().set_text(get_cam().get_average()[2])\n", "name": "img_average_test"}

static/js/blockly/blocks.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,34 @@ Blockly.Python['coderbot_adv_findColor'] = function(block) {
574574
return [code, Blockly.Python.ORDER_ATOMIC];
575575
};
576576

577+
Blockly.Blocks['coderbot_cam_average'] = {
578+
/**
579+
* Block for image.get_average() function.
580+
* @this Blockly.Block
581+
*/
582+
init: function() {
583+
this.setHelpUrl(Blockly.Msg.LOGIC_BOOLEAN_HELPURL);
584+
this.setColour(290);
585+
this.appendDummyInput()
586+
.appendField(Blockly.Msg.CODERBOT_SENSOR_AVERAGE)
587+
.appendField(new Blockly.FieldDropdown([[Blockly.Msg.CODERBOT_SENSOR_AVERAGE_HUE, 'H'],
588+
[Blockly.Msg.CODERBOT_SENSOR_AVERAGE_SATURATION, 'S'],
589+
[Blockly.Msg.CODERBOT_SENSOR_AVERAGE_VALUE, 'V'],
590+
[Blockly.Msg.CODERBOT_SENSOR_AVERAGE_ALL,'ALL']]), 'RETVAL')
591+
this.setInputsInline(true);
592+
this.setOutput(true, ['Number', 'Array']);
593+
this.setTooltip(Blockly.Msg.LOGIC_BOOLEAN_TOOLTIP);
594+
}
595+
};
596+
597+
Blockly.Python['coderbot_cam_average'] = function(block) {
598+
// Boolean values true and false.
599+
var retval = block.getFieldValue('RETVAL');
600+
var ret_code = {'H': '[0]', 'S': '[1]', 'V': '[2]', 'ALL': ''}[retval];
601+
var code = 'get_cam().get_average()' + ret_code;
602+
return [code, Blockly.Python.ORDER_ATOMIC];
603+
};
604+
577605
Blockly.Blocks['coderbot_adv_findText'] = {
578606
/**
579607
* Block for findText function.

static/js/blockly/bot_en.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ Blockly.Msg.CODERBOT_SENSOR_FINDFACE_Y = "y coord";
4545
Blockly.Msg.CODERBOT_SENSOR_FINDFACE_SIZE = "size";
4646
Blockly.Msg.CODERBOT_SENSOR_FINDFACE_ALL = "x, y, size (as list)";
4747
Blockly.Msg.CODERBOT_SENSOR_FINDLOGO = "find logo";
48+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE = "get image average";
49+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_HUE = "Hue";
50+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_SATURATION = "Saturation";
51+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_VALUE = "Value (brightness)";
52+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_ALL = "HSV (as list)";
4853
Blockly.Msg.CODERBOT_SENSOR_FINDTEXT_FIND = "find text of kind";
4954
Blockly.Msg.CODERBOT_SENSOR_FINDTEXT_ACCEPT_ALPHA = "Alpha (A..Z)";
5055
Blockly.Msg.CODERBOT_SENSOR_FINDTEXT_ACCEPT_NUM = "Numeric (0..9)";

static/js/blockly/bot_fr.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ Blockly.Msg.CODERBOT_SENSOR_FINDFACE_X = "abscisse";
4444
Blockly.Msg.CODERBOT_SENSOR_FINDFACE_Y = "ordonnée";
4545
Blockly.Msg.CODERBOT_SENSOR_FINDFACE_SIZE = "taille";
4646
Blockly.Msg.CODERBOT_SENSOR_FINDFACE_ALL = "x, y, taille (sous forme de liste)";
47+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE = "get image average";
48+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_HUE = "Hue";
49+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_SATURATION = "Saturation";
50+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_VALUE = "Value (brightness)";
51+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_ALL = "HSV (as list)";
4752
Blockly.Msg.CODERBOT_SENSOR_FINDLOGO = "trouve le logo";
4853
Blockly.Msg.CODERBOT_SENSOR_FINDTEXT_FIND = "find text of kind";
4954
Blockly.Msg.CODERBOT_SENSOR_FINDTEXT_ACCEPT_ALPHA = "Alpha (A..Z)";

static/js/blockly/bot_it.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ Blockly.Msg.CODERBOT_SENSOR_FINDFACE_Y = "y (ordinata)";
4545
Blockly.Msg.CODERBOT_SENSOR_FINDFACE_SIZE = "dimensione";
4646
Blockly.Msg.CODERBOT_SENSOR_FINDFACE_ALL = "x, y, dimensione (come lista)";
4747
Blockly.Msg.CODERBOT_SENSOR_FINDLOGO = "trova logo";
48+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE = "valore medio immagine";
49+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_HUE = "Tinta";
50+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_SATURATION = "Saturazione";
51+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_VALUE = "Luminosità";
52+
Blockly.Msg.CODERBOT_SENSOR_AVERAGE_ALL = "HSV (come lista)";
4853
Blockly.Msg.CODERBOT_SENSOR_FINDTEXT_FIND = "trova testo formato da";
4954
Blockly.Msg.CODERBOT_SENSOR_FINDTEXT_ACCEPT_ALPHA = "Lettere (A..Z)";
5055
Blockly.Msg.CODERBOT_SENSOR_FINDTEXT_ACCEPT_NUM = "Numeri (0..9)";

templates/blocks_adv.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
<block type="coderbot_camera_videoStop"></block>
233233
</category>
234234
<category name="{% trans %}Sensor{% endtrans %}" colour="290">
235+
<block type="coderbot_cam_average"></block>
235236
<block type="coderbot_adv_pathAhead"></block>
236237
<block type="coderbot_adv_findLine"></block>
237238
<block type="coderbot_adv_findSignal"></block>

viz/camera.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ def video_stop(self):
7070

7171
# pack in mp4 container
7272
params = " -fps 12 -add " + self.video_filename + self.VIDEO_FILE_EXT_H264 + " " + self.video_filename + self.VIDEO_FILE_EXT
73-
#avconv_params = " -r 30 -i " + self.video_filename + self.VIDEO_FILE_EXT_H264 + " -vcodec copy " + self.video_filename + self.VIDEO_FILE_EXT
7473
os.system(self.FFMPEG_CMD + params)
7574
# remove h264 file
7675
os.remove(self.video_filename + self.VIDEO_FILE_EXT_H264)
@@ -88,34 +87,23 @@ def grab(self):
8887
self.camera._encoders[0] = self.jpeg_encoder
8988
self.camera._encoders[1] = self.rgb_encoder
9089

91-
#print "g.2: " + str(ts - time.time())
92-
#ts = time.time()
93-
9490
self.out_jpeg.seek(0)
9591
self.out_rgb.seek(0)
9692

9793
self.jpeg_encoder.start(self.out_jpeg)
9894
self.rgb_encoder.start(self.out_rgb)
9995

100-
#print "g.3: " + str(ts - time.time())
101-
#ts = time.time()
102-
10396
if not self.jpeg_encoder.wait(10):
10497
raise picamera.PiCameraError('Timed out')
10598
if not self.rgb_encoder.wait(10):
10699
raise picamera.PiCameraError('Timed out')
107100

108-
#print "g.4: " + str(ts - time.time())
109-
#ts = time.time()
110-
111101
with self.camera._encoders_lock:
112102
del self.camera._encoders[0]
113103
del self.camera._encoders[1]
114104
self.jpeg_encoder.close()
115105
self.rgb_encoder.close()
116106

117-
#print "g.5: " + str(ts - time.time())
118-
119107
def grab_start(self):
120108
logging.debug("grab_start")
121109

viz/image.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ def binarize(self, threshold = -1):
143143
ret, data = cv2.threshold(data, threshold, 255, cv2.THRESH_BINARY_INV)
144144
return Image(data)
145145

146+
def get_average(self):
147+
data = cv2.cvtColor(self._data, cv2.COLOR_BGR2HSV)
148+
logging.info("shape: " + str(data.shape))
149+
h = np.average(data[:,:,0])
150+
s = np.average(data[:,:,1])
151+
v = np.average(data[:,:,2])
152+
return [h, s, v]
153+
146154
def find_blobs(self, minsize=0, maxsize=10000000):
147155
blobs = []
148156
image = contours = hyerarchy = None

0 commit comments

Comments
 (0)