Changeset 490
- Timestamp:
- 05/14/09 01:04:25 (3 years ago)
- Location:
- trunk/software/rb
- Files:
-
- 8 modified
-
auto.py (modified) (2 diffs)
-
controller.py (modified) (2 diffs)
-
vision/__init__.py (modified) (1 diff)
-
vision/_vision.c (modified) (5 diffs)
-
vision/classify_dtree.cpp (modified) (1 diff)
-
vision/main.c (modified) (2 diffs)
-
vision/makefile (modified) (1 diff)
-
vision/vision.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/software/rb/auto.py
r488 r490 20 20 import time 21 21 from math import pi 22 from collections import defaultdict 22 23 23 24 from rb.core.logging import log_debug, log_info, log_warn, log_error, log_die 24 25 from rb.utils import points_to_vec, rad_delta 25 26 26 27 27 from time import sleep … … 44 44 def __init__(self, controller): 45 45 self.controller = controller 46 self.running = False 47 self.points = [] 46 48 47 49 # self.nav_func = navigate_points 48 self.nav_func = navigate_course50 self.nav_func = self.navigate_course 49 51 50 52 def _main(self): 51 if self.controller.navigator == 'auto': 52 self.nav_func() 53 # update motors 53 print "Auto._main: starting main loop" 54 self.running = True 55 while (self.running): 56 if self.controller.navigator == 'auto': 57 print "Auto._main: Running autonomous code" 58 self.nav_func() 59 # update motors 60 sleep(0.5) 61 print "Auto._main: Finished" 54 62 55 63 def process(self, type, data): 56 64 if type == 'vision_points': 65 print "Auto.process: Storing points" 57 66 self.points = data[0] 58 67 59 68 def navigate_course(self): 60 for point in self.points(): 61 # count points `in front' of us 62 # If it's `mostly' clear 63 # go forward 64 # else 65 # count points on left/right 66 # turn in most clear direction 69 # TODO: import these from the C enum 70 RB_GRASS = 0 71 RB_OBSTICLE = 1 72 RB_LINE = 2 73 RB_IGNORE = 3 74 values = [0.1, 0.1, 0.1, 0.1] 75 76 # count points `in front' of us 77 for point in self.points: 78 if (-10 < point[0] and point[0] < 10 and point[0] < 10): 79 values[point[2]] += 1 80 81 if (values[RB_GRASS] < 10): 82 print "Auto.navigate_course: Not enough data, aborting" 83 return False; 84 85 # If it's `mostly' clear 86 print "Auto.navigate_course: values = ", values 87 if (values[RB_GRASS]/values[RB_OBSTICLE]) > 2: 88 # go forward 89 print "Auto.navigate_course: Moving forward" 90 self.controller.drive.setspeed(0.5, 0.5) 91 else: 92 print "Auto.navigate_course: Obsticle detected" 93 # TODO: 94 # count points on left/right 95 # turn in most clear direction 67 96 68 97 def navigate_points(self): -
trunk/software/rb/controller.py
r488 r490 69 69 from rb.auto import Auto 70 70 self.auto = Auto(self) 71 self.auto._main() 71 72 72 73 def _run_wiimote(self): … … 99 100 return False 100 101 elif nav == 'auto': 101 print "Not implemented" 102 return False 102 pass 103 103 elif nav == 'stop': 104 104 self.drive.setspeed(0, 0) -
trunk/software/rb/vision/__init__.py
r489 r490 9 9 self.controller = controller 10 10 _vision._init_classifier() 11 _vision._init_capture()11 #_vision._init_capture() 12 12 start_new_thread(self._main, ()) 13 13 -
trunk/software/rb/vision/_vision.c
r489 r490 102 102 _get_points(PyObject *self, PyObject *args) 103 103 { 104 if (classifier == NULL) {105 PyErr_SetString(PyExc_RuntimeError, "_init_classifier must be called first");106 return NULL;107 } else if (cam == NULL) {108 PyErr_SetString(PyExc_RuntimeError, "_init_capture must be called first");109 return NULL;110 }104 //if (classifier == NULL) { 105 // PyErr_SetString(PyExc_RuntimeError, "_init_classifier must be called first"); 106 // return NULL; 107 //} else if (cam == NULL) { 108 // PyErr_SetString(PyExc_RuntimeError, "_init_capture must be called first"); 109 // return NULL; 110 //} 111 111 112 112 // TODO: start with a larger list 113 113 printf("generating new pixbuf\n"); 114 PyObject *p oints = PyList_New(0);114 PyObject *py_points = PyList_New(0); 115 115 116 116 IplImage *input; … … 124 124 Py_BEGIN_ALLOW_THREADS 125 125 126 input = capture(); 126 //input = capture(); 127 input = get_image("rb/vision/data/classes/input.jpeg"); 127 128 transformed = transform_image(input, 0.25); 128 129 classes = classifier_predict(classifier, transformed); … … 135 136 int i; 136 137 for(i = 0; i < seq->total; i++) { 137 int data[3];138 CV_READ_SEQ_ELEM( data, reader);139 PyObject *p oint = Py_BuildValue("iii",140 data[0], data[1], data[2]);141 PyList_Append(p oints,point);138 point_t point; 139 CV_READ_SEQ_ELEM(point, reader); 140 PyObject *py_point = Py_BuildValue("iii", 141 point.x, point.y, point.value); 142 PyList_Append(py_points, py_point); 142 143 } 143 144 … … 151 152 cvReleaseMat(&classes); 152 153 153 PyObject *rval = Py_BuildValue("(OO)", p oints, py_pixbuf);154 Py_DecRef(p oints);154 PyObject *rval = Py_BuildValue("(OO)", py_points, py_pixbuf); 155 Py_DecRef(py_points); 155 156 Py_DecRef(py_pixbuf); 156 157 return rval; … … 162 163 {"_init_classifier", _init_classifier, METH_VARARGS, 163 164 "Initialize the classifier using predefined training images"}, 164 {"_init_capture", _init_capture, METH_VARARGS,165 "Initialize the camera capture device"},165 {"_init_capture", _init_capture, METH_VARARGS, 166 "Initialize the camera capture device"}, 166 167 {NULL, NULL, 0, NULL} // sentinel 167 168 }; -
trunk/software/rb/vision/classify_dtree.cpp
r484 r490 32 32 dtree->train(input, CV_ROW_SAMPLE, desired, 0, 0, var_type, NULL, 33 33 CvDTreeParams( 34 5, // 15,// Max depth35 10, // 50,// min sample count34 15, // 5, // Max depth 35 50, // 10, // min sample count 36 36 0, // regression accuracy: N/A here 37 37 false, // no not compute surrogate split 38 38 RB_NUM_CLASSES, // max number of categories (use sub-optimal algorithm for larger numbers) 39 4, // 20,// the number of cross-validation folds39 20, // 4 // the number of cross-validation folds 40 40 false, // use 1SE rule => smaller tree 41 41 true, // throw away the pruned tree branches -
trunk/software/rb/vision/main.c
r486 r490 42 42 /* Process */ 43 43 print_time("Training classifier"); 44 classifier_t *classifier = classifier_new(RB_DTREE); 44 //classifier_t *classifier = classifier_new(RB_DTREE); 45 classifier_t *classifier = classifier_new(RB_HARD); 45 46 classifier_add_data_from_files(classifier, 46 47 "data/classes/input.jpeg", … … 59 60 cvStartReadSeq(seq, &reader, 0); 60 61 for(int i = 0; i < seq->total; i++) { 61 int data[3];62 CV_READ_SEQ_ELEM( data, reader);62 point_t point; 63 CV_READ_SEQ_ELEM(point, reader); 63 64 cvCircle(transformed, 64 cvPoint(data[1], data[2]), 65 2, class_colors[data[0]], CV_FILLED, 8, 0); 65 cvPoint(point.x + transformed->width/2, 66 transformed->height - point.y), 67 2, class_colors[point.value], CV_FILLED, 8, 0); 66 68 } 67 69 cvReleaseMemStorage(&seq->storage); -
trunk/software/rb/vision/makefile
r488 r490 1 1 CFLAGS = -p -pg -g -Wall -Werror -Wno-unused -pedantic -fPIC --std=gnu99 -fgnu89-inline -O3 $(shell pkg-config --cflags opencv gsl) 2 2 #CFLAGS = -p -pg -g -Wall -Werror -pedantic -fPIC -O3 $(shell pkg-config --cflags opencv gsl) 3 CPPFLAGS = -p -pg -g -Wall -Werror - -Wno-unusedpedantic -fPIC -O3 $(shell pkg-config --cflags opencv gsl)3 CPPFLAGS = -p -pg -g -Wall -Werror -Wno-unused -pedantic -fPIC -O3 $(shell pkg-config --cflags opencv gsl) 4 4 LDFLAGS = $(shell pkg-config --libs opencv gsl) 5 5 DLFLAGS = -shared -nostartfiles -
trunk/software/rb/vision/vision.c
r489 r490 50 50 /* transform image */ 51 51 IplImage *transformed = cvCreateImage(size, img->depth, img->nChannels); 52 CvMat * mat= cvCreateMat( 3, 3, CV_32F);52 CvMat *mat = cvCreateMat(3, 3, CV_32F); 53 53 CvPoint2D32f from[] = { 54 cvPoint2D32f( 1208, 1172),55 cvPoint2D32f( 1196, 1224),56 cvPoint2D32f( 1295, 1178),57 cvPoint2D32f( 1294, 1231)54 cvPoint2D32f(0 , size.height*0.12), 55 cvPoint2D32f(0 , size.height-1), 56 cvPoint2D32f(size.width-1, size.height-1), 57 cvPoint2D32f(size.width-1, size.height*0.12) 58 58 }; 59 59 CvPoint2D32f to[] = { 60 cvPoint2D32f( 1210, 1170),61 cvPoint2D32f( 1210, 1250),62 cvPoint2D32f( 1290, 1170),63 cvPoint2D32f( 1290, 1250)60 cvPoint2D32f(0 , -62 ), 61 cvPoint2D32f(size.width*0.33, size.height-1), 62 cvPoint2D32f(size.width*0.67, size.height-1), 63 cvPoint2D32f(size.width-1 , -62 ) 64 64 }; 65 65 cvGetPerspectiveTransform(from, to, mat); … … 69 69 } 70 70 71 //int [3]point_to_ 71 /* convert an x,y,value to a point_t 72 * (changes x,y vlaues to be centered on the robot) 73 * TODO: Do this using the microstrain */ 74 point_t xyv_to_point(CvMat *im, int x, int y, CvScalar value) 75 { 76 point_t point; 77 point.x = x - (im->cols/2); 78 point.y = im->rows - y; 79 point.value = value.val[0]; 80 return point; 81 } 72 82 73 83 CvSeq *get_points(CvMat *classes) 74 84 { 75 85 CvMemStorage *storage = cvCreateMemStorage(0); 76 CvSeq *seq = cvCreateSeq(CV_32SC3, // sequence of 3 tuples? (scalars?)77 sizeof(CvSeq), // header size - no extra fields78 sizeof( int)*3, // element size79 storage); // the container storage86 CvSeq *seq = cvCreateSeq(CV_32SC3, // sequence of 3 tuples? (scalars?) 87 sizeof(CvSeq), // header size - no extra fields 88 sizeof(point_t), // element size 89 storage); // the container storage 80 90 81 91 CvSeqWriter writer; … … 84 94 for(int y = 0; y < classes->rows; y+=10) { 85 95 CvScalar klass = cvGet2D(classes, y, x); 86 int data[3] = {klass.val[0], x, y};87 CV_WRITE_SEQ_ELEM( data, writer);96 point_t point = xyv_to_point(classes, x, y, klass); 97 CV_WRITE_SEQ_ELEM(point, writer); 88 98 } 89 99 }

