Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <unistd.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <sys/mman.h> |
| 5 | #include <errno.h> |
| 6 | #include <string.h> |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 7 | #include <netinet/in.h> |
| 8 | #include <netinet/tcp.h> |
| 9 | #include <arpa/inet.h> |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 10 | |
Brian Silverman | 6742a44 | 2013-11-03 12:58:42 -0800 | [diff] [blame^] | 11 | #include <vector> |
| 12 | #include <iostream> |
| 13 | |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 14 | #include "aos/common/time.h" |
| 15 | #include "aos/atom_code/camera/Buffers.h" |
| 16 | #include "aos/externals/libjpeg/include/jpeglib.h" |
Brian Silverman | 5b3e51e | 2013-03-29 22:53:44 -0700 | [diff] [blame] | 17 | #include "aos/atom_code/init.h" |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 18 | #include "aos/common/logging/logging.h" |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 19 | |
| 20 | #include "vision/OpenCVWorkTask.h" |
| 21 | #include "vision/CameraProcessor.h" |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 22 | #include "vision/JPEGRoutines.h" |
| 23 | |
| 24 | |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 25 | namespace frc971 { |
| 26 | namespace vision { |
| 27 | |
| 28 | } // namespace vision |
| 29 | } // namespace frc971 |
| 30 | |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | void SaveImageToFile(IplImage *image, const char *filename) { |
| 33 | FILE *file = fopen(filename, "w"); |
| 34 | |
| 35 | fputs("P3\n320 240\n255\n", file); |
| 36 | ::cv::Mat img(image); |
| 37 | for (int i = 0; i < img.rows; i++) { |
| 38 | for (int j = 0; j < img.cols; j++) { |
| 39 | // You can now access the pixel value with cv::Vec3b |
| 40 | fprintf(file, "%d %d %d ", |
| 41 | img.at<cv::Vec3b>(i,j)[0], |
| 42 | img.at<cv::Vec3b>(i,j)[1], |
| 43 | img.at<cv::Vec3b>(i,j)[2]); |
| 44 | |
| 45 | } |
| 46 | fputs("\n", file); |
| 47 | } |
| 48 | fclose(file); |
| 49 | } |
| 50 | } |
| 51 | |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 52 | #include "frc971/queues/CameraTarget.q.h" |
| 53 | using frc971::vision::targets; |
| 54 | |
Brian Silverman | 6742a44 | 2013-11-03 12:58:42 -0800 | [diff] [blame^] | 55 | void sender_main(){ |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 56 | ::aos::camera::Buffers buffers; |
| 57 | CvSize size; |
| 58 | size.width = ::aos::camera::Buffers::Buffers::kWidth; |
| 59 | size.height = ::aos::camera::Buffers::Buffers::kHeight; |
Brian Silverman | 6742a44 | 2013-11-03 12:58:42 -0800 | [diff] [blame^] | 60 | unsigned char buffer[::aos::camera::Buffers::Buffers::kWidth * |
| 61 | ::aos::camera::Buffers::Buffers::kHeight * 3]; |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 62 | |
| 63 | // create processing object |
| 64 | ProcessorData processor(size.width, size.height, false); |
| 65 | |
| 66 | printf("started sender main\n"); |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 67 | LOG(INFO, "Camera server started\n"); |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 68 | while(true){ |
| 69 | //usleep(7500); |
| 70 | size_t data_size; |
| 71 | timeval timestamp_timeval; |
Brian Silverman | 6742a44 | 2013-11-03 12:58:42 -0800 | [diff] [blame^] | 72 | LOG(DEBUG, "getting new image\n"); |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 73 | const void *image = buffers.GetNext( |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 74 | true, &data_size, ×tamp_timeval, NULL); |
| 75 | ::aos::time::Time timestamp(timestamp_timeval); |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 76 | |
Brian Silverman | 6742a44 | 2013-11-03 12:58:42 -0800 | [diff] [blame^] | 77 | LOG(DEBUG, "Got new image\n"); |
| 78 | frc971::vision::process_jpeg( |
| 79 | buffer, static_cast<unsigned char *>(const_cast<void *>(image)), |
| 80 | data_size); |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 81 | |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 82 | // build the headers on top of the buffer |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 83 | cvSetData(processor.src_header_image, |
| 84 | buffer, |
| 85 | ::aos::camera::Buffers::Buffers::kWidth * 3); |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 86 | |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 87 | // Reset. |
| 88 | processor.clear(); |
| 89 | // transform the buffer into targets |
| 90 | processor.threshold(buffer); |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 91 | |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 92 | processor.getContours(); |
| 93 | processor.filterToTargets(); |
| 94 | |
| 95 | // could be used for debug ie drawContours |
| 96 | //std::vector<std::vector<cv::Point> > target_contours; |
| 97 | //std::vector<std::vector<cv::Point> > best_contours; |
| 98 | std::vector<Target>::iterator target_it; |
| 99 | Target *best_target = NULL; |
| 100 | // run through the targets |
Brian Silverman | 6742a44 | 2013-11-03 12:58:42 -0800 | [diff] [blame^] | 101 | LOG(DEBUG, "Found %u targets\n", processor.target_list.size()); |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 102 | for(target_it = processor.target_list.begin(); |
| 103 | target_it != processor.target_list.end(); target_it++){ |
| 104 | //target_contours.push_back(*(target_it->this_contour)); |
| 105 | // select the highest target |
| 106 | if (best_target == NULL) { |
| 107 | best_target = &*target_it; |
| 108 | } else { |
| 109 | if (target_it->height < best_target->height) { |
| 110 | best_target = &*target_it; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | // if we found one then send it on |
| 115 | if (best_target != NULL) { |
Brian Silverman | 6742a44 | 2013-11-03 12:58:42 -0800 | [diff] [blame^] | 116 | LOG(DEBUG, "Target is %f\n", best_target->rect.centroid.x); |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 117 | targets.MakeWithBuilder() |
| 118 | .percent_azimuth_off_center( |
| 119 | best_target->rect.centroid.y / (double)::aos::camera::Buffers::kHeight - 0.5) |
| 120 | .percent_elevation_off_center( |
| 121 | best_target->rect.centroid.x / (double)::aos::camera::Buffers::kWidth - 0.5) |
| 122 | .timestamp(timestamp.ToNSec()) |
| 123 | .Send(); |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 124 | } |
Austin Schuh | 86bec78 | 2013-04-04 05:50:52 +0000 | [diff] [blame] | 125 | //static int counter = 0; |
| 126 | //if (++counter > 2) { |
| 127 | //break; |
| 128 | //} |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 129 | } |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | int main(int /*argc*/, char** /*argv*/){ |
Brian Silverman | 6742a44 | 2013-11-03 12:58:42 -0800 | [diff] [blame^] | 134 | ::aos::InitNRT(); |
| 135 | sender_main(); |
| 136 | ::aos::Cleanup(); |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 137 | } |
| 138 | |