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