blob: 982d6709774b0038c7fa5fb0b7f32ce4649eb7ed [file] [log] [blame]
Brian Silverman6ae77dd2013-03-29 22:28:08 -07001#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 Silverman6ae77dd2013-03-29 22:28:08 -07007#include <netinet/in.h>
8#include <netinet/tcp.h>
9#include <arpa/inet.h>
Brian Silverman6ae77dd2013-03-29 22:28:08 -070010
Brian Silverman6742a442013-11-03 12:58:42 -080011#include <vector>
12#include <iostream>
13
Brian Silverman8f1018b2013-11-17 21:35:15 -080014#include "libjpeg/include/jpeglib.h"
15
Brian Silverman6ae77dd2013-03-29 22:28:08 -070016#include "aos/common/time.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -080017#include "aos/linux_code/camera/Buffers.h"
18#include "aos/linux_code/init.h"
Austin Schuh86bec782013-04-04 05:50:52 +000019#include "aos/common/logging/logging.h"
Brian Silverman6ae77dd2013-03-29 22:28:08 -070020
21#include "vision/OpenCVWorkTask.h"
22#include "vision/CameraProcessor.h"
Brian Silverman6ae77dd2013-03-29 22:28:08 -070023#include "vision/JPEGRoutines.h"
24
25
Brian Silverman6ae77dd2013-03-29 22:28:08 -070026namespace frc971 {
27namespace vision {
28
29} // namespace vision
30} // namespace frc971
31
Austin Schuh86bec782013-04-04 05:50:52 +000032namespace {
33void 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 Silverman6ae77dd2013-03-29 22:28:08 -070053#include "frc971/queues/CameraTarget.q.h"
54using frc971::vision::targets;
55
Brian Silverman6742a442013-11-03 12:58:42 -080056void sender_main(){
Brian Silverman6ae77dd2013-03-29 22:28:08 -070057 ::aos::camera::Buffers buffers;
58 CvSize size;
59 size.width = ::aos::camera::Buffers::Buffers::kWidth;
60 size.height = ::aos::camera::Buffers::Buffers::kHeight;
Brian Silverman6742a442013-11-03 12:58:42 -080061 unsigned char buffer[::aos::camera::Buffers::Buffers::kWidth *
62 ::aos::camera::Buffers::Buffers::kHeight * 3];
Brian Silverman6ae77dd2013-03-29 22:28:08 -070063
64 // create processing object
65 ProcessorData processor(size.width, size.height, false);
66
67 printf("started sender main\n");
Austin Schuh86bec782013-04-04 05:50:52 +000068 LOG(INFO, "Camera server started\n");
Brian Silverman6ae77dd2013-03-29 22:28:08 -070069 while(true){
70 //usleep(7500);
71 size_t data_size;
72 timeval timestamp_timeval;
Brian Silverman6742a442013-11-03 12:58:42 -080073 LOG(DEBUG, "getting new image\n");
Brian Silverman6ae77dd2013-03-29 22:28:08 -070074 const void *image = buffers.GetNext(
Austin Schuh86bec782013-04-04 05:50:52 +000075 true, &data_size, &timestamp_timeval, NULL);
76 ::aos::time::Time timestamp(timestamp_timeval);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070077
Brian Silverman6742a442013-11-03 12:58:42 -080078 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 Schuh86bec782013-04-04 05:50:52 +000082
Brian Silverman6ae77dd2013-03-29 22:28:08 -070083 // build the headers on top of the buffer
Austin Schuh86bec782013-04-04 05:50:52 +000084 cvSetData(processor.src_header_image,
85 buffer,
86 ::aos::camera::Buffers::Buffers::kWidth * 3);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070087
Austin Schuh86bec782013-04-04 05:50:52 +000088 // Reset.
89 processor.clear();
90 // transform the buffer into targets
91 processor.threshold(buffer);
Brian Silverman6ae77dd2013-03-29 22:28:08 -070092
Austin Schuh86bec782013-04-04 05:50:52 +000093 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 Silverman6742a442013-11-03 12:58:42 -0800102 LOG(DEBUG, "Found %u targets\n", processor.target_list.size());
Austin Schuh86bec782013-04-04 05:50:52 +0000103 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 Silverman6742a442013-11-03 12:58:42 -0800117 LOG(DEBUG, "Target is %f\n", best_target->rect.centroid.x);
Austin Schuh86bec782013-04-04 05:50:52 +0000118 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 Silverman6ae77dd2013-03-29 22:28:08 -0700125 }
Austin Schuh86bec782013-04-04 05:50:52 +0000126 //static int counter = 0;
127 //if (++counter > 2) {
128 //break;
129 //}
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700130 }
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700131}
132
133
134int main(int /*argc*/, char** /*argv*/){
Brian Silverman6742a442013-11-03 12:58:42 -0800135 ::aos::InitNRT();
136 sender_main();
137 ::aos::Cleanup();
Brian Silverman6ae77dd2013-03-29 22:28:08 -0700138}
139