Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import cv2 |
| 4 | import sys |
| 5 | import flatbuffers |
| 6 | |
| 7 | import frc971.vision.sift.TrainingImage as TrainingImage |
| 8 | import frc971.vision.sift.TrainingData as TrainingData |
| 9 | import frc971.vision.sift.Feature as Feature |
| 10 | |
| 11 | def main(): |
| 12 | image4_cleaned_path = sys.argv[1] |
| 13 | output_path = sys.argv[2] |
| 14 | |
| 15 | image4_cleaned = cv2.imread(image4_cleaned_path) |
| 16 | |
| 17 | image = cv2.cvtColor(image4_cleaned, cv2.COLOR_BGR2GRAY) |
| 18 | image = cv2.resize(image, (640, 480)) |
| 19 | sift = cv2.xfeatures2d.SIFT_create() |
| 20 | keypoints, descriptors = sift.detectAndCompute(image, None) |
| 21 | |
| 22 | fbb = flatbuffers.Builder(0) |
| 23 | |
| 24 | features_vector = [] |
| 25 | |
| 26 | for keypoint, descriptor in zip(keypoints, descriptors): |
| 27 | Feature.FeatureStartDescriptorVector(fbb, len(descriptor)) |
| 28 | for n in reversed(descriptor): |
| 29 | fbb.PrependFloat32(n) |
| 30 | descriptor_vector = fbb.EndVector(len(descriptor)) |
| 31 | |
| 32 | Feature.FeatureStart(fbb) |
| 33 | |
| 34 | Feature.FeatureAddDescriptor(fbb, descriptor_vector) |
| 35 | Feature.FeatureAddX(fbb, keypoint.pt[0]) |
| 36 | Feature.FeatureAddY(fbb, keypoint.pt[1]) |
| 37 | Feature.FeatureAddSize(fbb, keypoint.size) |
| 38 | Feature.FeatureAddAngle(fbb, keypoint.angle) |
| 39 | Feature.FeatureAddResponse(fbb, keypoint.response) |
| 40 | Feature.FeatureAddOctave(fbb, keypoint.octave) |
| 41 | |
| 42 | features_vector.append(Feature.FeatureEnd(fbb)) |
| 43 | |
| 44 | TrainingImage.TrainingImageStartFeaturesVector(fbb, len(features_vector)) |
| 45 | for feature in reversed(features_vector): |
| 46 | fbb.PrependUOffsetTRelative(feature) |
| 47 | features_vector_table = fbb.EndVector(len(features_vector)) |
| 48 | |
| 49 | TrainingImage.TrainingImageStart(fbb) |
| 50 | TrainingImage.TrainingImageAddFeatures(fbb, features_vector_table) |
| 51 | # TODO(Brian): Fill out the transformation matrices. |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame^] | 52 | training_image_offset = TrainingImage.TrainingImageEnd(fbb) |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 53 | |
| 54 | TrainingData.TrainingDataStartImagesVector(fbb, 1) |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame^] | 55 | fbb.PrependUOffsetTRelative(training_image_offset) |
| 56 | images_offset = fbb.EndVector(1) |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 57 | |
| 58 | TrainingData.TrainingDataStart(fbb) |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame^] | 59 | TrainingData.TrainingDataAddImages(fbb, images_offset) |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 60 | fbb.Finish(TrainingData.TrainingDataEnd(fbb)) |
| 61 | |
| 62 | bfbs = fbb.Output() |
| 63 | |
| 64 | output_prefix = [ |
| 65 | b'#ifndef Y2020_VISION_SIFT_DEMO_SIFT_H_', |
| 66 | b'#define Y2020_VISION_SIFT_DEMO_SIFT_H_', |
| 67 | b'#include <string_view>', |
| 68 | b'namespace frc971 {', |
| 69 | b'namespace vision {', |
| 70 | b'inline std::string_view DemoSiftData() {', |
| 71 | ] |
| 72 | output_suffix = [ |
| 73 | b' return std::string_view(kData, sizeof(kData));', |
| 74 | b'}', |
| 75 | b'} // namespace vision', |
| 76 | b'} // namespace frc971', |
| 77 | b'#endif // Y2020_VISION_SIFT_DEMO_SIFT_H_', |
| 78 | ] |
| 79 | |
| 80 | with open(output_path, 'wb') as output: |
| 81 | for line in output_prefix: |
| 82 | output.write(line) |
| 83 | output.write(b'\n') |
| 84 | output.write(b'alignas(64) static constexpr char kData[] = "') |
| 85 | for byte in fbb.Output(): |
| 86 | output.write(b'\\x' + (b'%x' % byte).zfill(2)) |
| 87 | output.write(b'";\n') |
| 88 | for line in output_suffix: |
| 89 | output.write(line) |
| 90 | output.write(b'\n') |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | main() |