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