forked from asmaklad/TeleView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElequentVision.h
More file actions
173 lines (135 loc) · 3.96 KB
/
ElequentVision.h
File metadata and controls
173 lines (135 loc) · 3.96 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef ELEQUENTVISION_H
#define ELEQUENTVISION_H
// suorces
// https://github.com/eloquentarduino/EloquentArduino/blob/1.1.8/examples/ESP32CameraNaiveMotionDetection/ESP32CameraNaiveMotionDetection.ino
// https://eloquentarduino.github.io/2020/05/easier-faster-pure-video-esp32-cam-motion-detection/?utm_source=old_version
#include "esp_camera.h"
#include "camera_pins.h"
#define FRAME_SIZE FRAMESIZE_QVGA
#define EWIDTH 320
#define EHEIGHT 240
#define BLOCK_SIZE 20
#define W (EWIDTH / BLOCK_SIZE)
#define H (EHEIGHT / BLOCK_SIZE)
#define BLOCK_DIFF_THRESHOLD 0.30
#define IMAGE_DIFF_THRESHOLD 0.10
#define EV_DEBUG 1
#define EV_DEBUG_EXT 0
uint16_t prev_frame[H][W] = { 0 };
uint16_t current_frame[H][W] = { 0 };
bool setup_camera(framesize_t);
bool capture_still();
bool motion_detect();
void update_frame();
void print_frame(uint16_t frame[H][W]);
/**
void setup() {
Serial.begin(115200);
Serial.println(setup_camera(FRAME_SIZE) ? "OK" : "ERR INIT");
}
void loop() {
if (!capture_still()) {
Serial.println("Failed capture");
delay(3000);
return;
}
if (motion_detect()) {
Serial.println("Motion detected");
}
update_frame();
Serial.println("=================");
}
*/
bool setup_camera(framesize_t frameSize) {
sensor_t *sensor = esp_camera_sensor_get();
sensor->set_pixformat(sensor, PIXFORMAT_GRAYSCALE );
sensor->set_framesize(sensor, frameSize);
return true;
}
/**
* Capture image and do down-sampling
*/
bool capture_still() {
camera_fb_t *frame_buffer = esp_camera_fb_get();
if (!frame_buffer)
return false;
// set all 0s in current frame
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++)
current_frame[y][x] = 0;
// down-sample image in blocks
for (uint32_t i = 0; i < EWIDTH * EHEIGHT; i++) {
const uint16_t x = i % EWIDTH;
const uint16_t y = floor(i / EWIDTH);
const uint8_t block_x = floor(x / BLOCK_SIZE);
const uint8_t block_y = floor(y / BLOCK_SIZE);
const uint8_t pixel = frame_buffer->buf[i];
const uint16_t current = current_frame[block_y][block_x];
// average pixels in block (accumulate)
current_frame[block_y][block_x] += pixel;
}
// average pixels in block (rescale)
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++)
current_frame[y][x] /= BLOCK_SIZE * BLOCK_SIZE;
#if EV_DEBUG
Serial.println("Current frame:");
print_frame(current_frame);
Serial.println("---------------");
#endif
esp_camera_fb_return(frame_buffer);
return true;
}
/**
* Compute the number of different blocks
* If there are enough, then motion happened
*/
bool motion_detect() {
uint16_t changes = 0;
const uint16_t blocks = (EWIDTH * EHEIGHT) / (BLOCK_SIZE * BLOCK_SIZE);
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
float current = current_frame[y][x];
float prev = prev_frame[y][x];
float delta = abs(current - prev) / prev;
if (delta >= BLOCK_DIFF_THRESHOLD) {
#if EV_DEBUG_EXT
Serial.print("diff\t");
Serial.print(y);
Serial.print('\t');
Serial.println(x);
#endif
changes += 1;
}
}
}
Serial.print("Changed ");
Serial.print(changes);
Serial.print(" out of ");
Serial.println(blocks);
return (1.0 * changes / blocks) > IMAGE_DIFF_THRESHOLD;
}
/**
* Copy current frame to previous
*/
void update_frame() {
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
prev_frame[y][x] = current_frame[y][x];
}
}
}
/**
* For serial debugging
* @param frame
*/
void print_frame(uint16_t frame[H][W]) {
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
Serial.print(frame[y][x]);
Serial.print('\t');
}
Serial.println();
}
}
#endif //ELEQUENTVISION_H