-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
150 lines (139 loc) · 5.26 KB
/
Copy pathmain.cpp
File metadata and controls
150 lines (139 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <filesystem>
#include "chrono"
#include "opencv2/opencv.hpp"
#include "yolov8.hpp"
#include <networktables/NetworkTableInstance.h>
#include <networktables/NetworkTable.h>
#include "cameraserver/CameraServer.h"
const std::vector<std::string> CLASS_NAMES = {
"note", "robot"};
const std::vector<std::vector<unsigned int>> COLORS = {
{255, 0, 0}, {0, 255, 0}};
std::string getNewFileName() {
std::string path = "./videos";
int num = 0;
for (const auto & entry : std::filesystem::directory_iterator(path)) {
auto name = entry.path().generic_string();
std::cout << name << std::endl;
auto aviPos = name.find(".avi");
if(aviPos != std::string::npos) {
auto startPos = name.find("_") + 1;
auto numStr = name.substr(startPos, aviPos - startPos);
std::cout << "numstr: " << numStr << '\n';
int currentNum = std::stoi(numStr);
if(currentNum >= num) num = currentNum + 1;
}
}
return "./videos/output_" + std::to_string(num) + ".avi";
}
int main(int argc, char** argv)
{
int frameTime = 0;
int shooterFrameTime = 0;
double trackedSize = 0.0;
int trackedIndex = 0;
double tx = 0.0;
double ty = 0.0;
bool targetFound = false;
int width, height;
width = 640;
height = 640;
cs::VideoMode startingMode{cs::VideoMode::PixelFormat::kMJPEG, width, height, 30};
CS_Status status = 0;
std::string intakePath = "";
std::string shooterPath = "";
for (const auto& caminfo : cs::EnumerateUsbCameras(&status)) {
// fmt::print("Dev {}: Path {} (Name {})\n", caminfo.dev, caminfo.path, caminfo.name);
// fmt::print("vid {}: pid {}\n", caminfo.vendorId, caminfo.productId);
if(caminfo.vendorId == 1118) intakePath = caminfo.path;
else if(caminfo.vendorId == 3141) shooterPath = caminfo.path;
}
// const std::string engine_file_path{argv[1]};
// const std::string path{argv[2]};
cs::UsbCamera intakeCam{"intakeCam", intakePath};
cs::UsbCamera shooterCam{"shooterCam", shooterPath};
intakeCam.SetVideoMode(startingMode);
shooterCam.SetVideoMode(startingMode);
auto inst = nt::NetworkTableInstance::GetDefault();
inst.SetServerTeam(6722);
inst.StartClient4("jetson-client");
auto table = inst.GetTable("/jetson");
cs::CvSink intakeSink{frc::CameraServer::GetVideo(intakeCam)};
cs::CvSink shooterSink{frc::CameraServer::GetVideo(shooterCam)};
assert(argc == 3);
auto yolov8 = new YOLOv8("best.engine");
yolov8->make_pipe(true);
cv::Mat res, image, shooterImage;
cv::Size size = cv::Size{width, height};
std::vector<Object> objs;
cs::CvSource intakeSource{"intakeRes", startingMode};
frc::CameraServer::StartAutomaticCapture(intakeSource);
int time = shooterSink.GrabFrameNoTimeout(image);
cv::VideoWriter outputVideo;
outputVideo.release();
table->PutBoolean("recordState", false);
bool recordState = false;
bool recording = true;
while(true) {
recordState = table->GetBoolean("recordState", false);
if(!recording && recordState) {
outputVideo.open(getNewFileName(), cv::VideoWriter::fourcc('M','J','P','G'), 30, {shooterImage.size().width, shooterImage.size().height});
if(!outputVideo.isOpened()) {
std::cout << "Video not opened!\n";
} else {
std::cout << "Video opened!\n";
recording = true;
}
}
if(!recordState && recording) {
outputVideo.release();
recording = false;
}
frameTime = intakeSink.GrabFrameNoTimeout(image);
shooterFrameTime = shooterSink.GrabFrameNoTimeout(shooterImage);
if(recording) outputVideo << shooterImage;
objs.clear();
yolov8->copy_from_Mat(image, size);
yolov8->infer();
yolov8->postprocess(objs);
frameTime = 0;
trackedSize = 0.0;
trackedIndex = 0;
tx = 0.0;
ty = 0.0;
targetFound = false;
// target processing
for(int i = 0; i < objs.size(); i++) {
bool targetViable = true;
if(objs[i].label != 0) continue;
double adjX = objs[i].rect.x - width / 2 + objs[i].rect.width / 2;
double adjY = height / 2 - objs[i].rect.y - objs[i].rect.height;
//targetViable &= adjY < 50.0;
double size = objs[i].rect.width * objs[i].rect.height;
size = size / (width * height);
if(!targetViable) continue;
targetFound = true;
if(size > trackedSize) {
trackedSize = size;
trackedIndex = i;
tx = adjX;
ty = adjY;
}
}
// send data to network table
table->PutNumber("tx", tx);
table->PutNumber("ty", ty);
table->PutNumber("ts", trackedSize);
table->PutBoolean("tv", targetFound);
if(targetFound) {
printf("(%f, %f) Size: %f\n", tx, ty, trackedSize);
} else {
printf("No viable target!\n");
}
yolov8->draw_objects(image, res, objs, CLASS_NAMES, COLORS);
intakeSource.PutFrame(res);
}
delete yolov8;
return 0;
}