-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_image_provider.cpp
56 lines (43 loc) · 1.86 KB
/
arduino_image_provider.cpp
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
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "image_provider.h"
#ifndef ARDUINO_EXCLUDE_CODE
#include "Arduino.h"
#include <TinyMLShield.h>
// Get an image from the camera module
TfLiteStatus GetImage(tflite::ErrorReporter* error_reporter, int image_width,
int image_height, int channels, int8_t* image_data) {
byte data[176 * 144]; // Receiving QCIF grayscale from camera = 176 * 144 * 1
static bool g_is_camera_initialized = false;
static bool serial_is_initialized = false;
// Initialize camera if necessary
if (!g_is_camera_initialized) {
if (!Camera.begin(QCIF, GRAYSCALE, 5, OV7670)) {
TF_LITE_REPORT_ERROR(error_reporter, "Failed to initialize camera!");
return kTfLiteError;
}
g_is_camera_initialized = true;
}
// Read camera data
Camera.readFrame(data);
int min_x = (176 - 96) / 2;
int min_y = (144 - 96) / 2;
int index = 0;
// Crop 96x96 image. This lowers FOV, ideally we should downsample
for (int y = min_y; y < min_y + 96; y++) {
for (int x = min_x; x < min_x + 96; x++) {
image_data[index++] = static_cast<int8_t>(data[(y * 176) + x] - 128); // convert TF input image to signed 8-bit
}
}
return kTfLiteOk;
}
#endif // ARDUINO_EXCLUDE_CODE