Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 38b951c

Browse files
migrated Vision::Decoders + Vision::MotionDetection + rgb motion detection example
1 parent b5e8c9d commit 38b951c

24 files changed

+1133
-47
lines changed

‎.DS_Store

6 KB
Binary file not shown.
6 KB
Binary file not shown.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#define CAMERA_MODEL_M5STACK_WIDE
2+
3+
#include <FS.h>
4+
#include <SPIFFS.h>
5+
#include <eloquentarduino.h>
6+
#include <eloquentarduino/io/serial_print.h>
7+
#include <eloquentarduino/vision/camera/ESP32Camera.h>
8+
#include <eloquentarduino/vision/io/writers/JpegWriter.h>
9+
10+
#define FRAME_SIZE FRAMESIZE_QVGA
11+
#define PIXFORMAT PIXFORMAT_RGB565
12+
#define W 320
13+
#define H 240
14+
#define w 32
15+
#define h 24
16+
#define DIFF_THRESHOLD 15
17+
#define MOTION_THRESHOLD 0.15
18+
19+
// delete the second definition if you want to turn on code benchmarking
20+
#define timeit(label, code) { uint32_t start = millis(); code; uint32_t duration = millis() - start; eloquent::io::print_all("It took ", duration, " millis for ", label); }
21+
#define timeit(label, code) code;
22+
23+
using namespace Eloquent::Vision;
24+
25+
camera_fb_t *frame;
26+
Camera::ESP32Camera camera(PIXFORMAT);
27+
//IO::Decoders::GrayscaleRandomAccessDecoder decoder;
28+
IO::Decoders::Red565RandomAccessDecoder decoder;
29+
Processing::Downscaling::Center<W / w, H / h> strategy;
30+
Processing::Downscaling::Downscaler<W, H, w, h> downscaler(&decoder, &strategy);
31+
Processing::MotionDetector<w, h> motion;
32+
IO::Writers::JpegWriter<W, H> jpegWriter;
33+
34+
35+
void capture();
36+
void save();
37+
void stream();
38+
39+
40+
void setup() {
41+
Serial.begin(115200);
42+
SPIFFS.begin(true);
43+
delay(1000);
44+
Serial.println("Begin");
45+
46+
camera.begin(FRAME_SIZE);
47+
// set how much a pixel value should differ to be considered as a change
48+
motion.setDiffThreshold(DIFF_THRESHOLD);
49+
// set how many pixels (in percent) should change to be considered as motion
50+
motion.setMotionThreshold(MOTION_THRESHOLD);
51+
// prevent consecutive triggers
52+
motion.setDebounceFrames(5);
53+
}
54+
55+
56+
void loop() {
57+
capture();
58+
eloquent::io::print_all(motion.changes(), " pixels changed");
59+
60+
if (motion.triggered()) {
61+
Serial.println("Motion detected");
62+
63+
// uncomment to save to disk
64+
// save();
65+
66+
// uncomment to stream to the Python script for visualization
67+
//stream();
68+
69+
delay(1000);
70+
}
71+
72+
delay(30);
73+
}
74+
75+
76+
void capture() {
77+
uint32_t start = millis();
78+
uint8_t downscaled[w * h];
79+
80+
timeit("capture frame", frame = camera.capture());
81+
82+
// scale image from size H * W to size h * w
83+
timeit("downscale", downscaler.downscale(frame->buf, downscaled));
84+
85+
// detect motion on the downscaled image
86+
timeit("motion detection", motion.detect(downscaled));
87+
88+
//eloquent::io::print_all(1000 / (millis() - start), " FPS");
89+
}
90+
91+
92+
void save() {
93+
File imageFile = SPIFFS.open("/capture.jpg", "wb");
94+
uint8_t quality = 30;
95+
96+
eloquent::io::print_all("The image will be saved as /capture.jpg");
97+
jpegWriter.write(imageFile, frame->buf, PIXFORMAT, quality);
98+
imageFile.close();
99+
eloquent::io::print_all("Saved");
100+
}
101+
102+
103+
void stream() {
104+
eloquent::io::print_all("START OF FRAME");
105+
106+
jpegWriter.write(Serial, frame->buf, PIXFORMAT, 30);
107+
108+
eloquent::io::print_all("END OF FRAME");
109+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from serial import Serial
2+
3+
4+
if __name__ == '__main__':
5+
with Serial('/dev/cu.usbserial-01BB622A', baudrate=115200) as camera:
6+
idx = 0
7+
buffer = ''
8+
jpeg = None
9+
10+
while True:
11+
try:
12+
line = camera.readline().decode('utf-8').strip()
13+
if line == 'START OF FRAME':
14+
print('new frame')
15+
jpeg = []
16+
text = ''
17+
while True:
18+
byte = camera.read(1)
19+
jpeg.append(byte)
20+
try:
21+
text += byte.decode('utf-8')
22+
if text.endswith('END OF FRAME'):
23+
with open('frame%d.jpg' % idx, 'wb') as file:
24+
for byte in jpeg[:-len('END OF FRAME')]:
25+
file.write(byte)
26+
print('done', idx)
27+
idx += 1
28+
break
29+
except UnicodeDecodeError:
30+
pass
31+
except UnicodeDecodeError:
32+
pass

‎library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "git",
77
"url": "https://github.com/eloquentarduino/EloquentArduino"
88
},
9-
"version": "1.0.0",
9+
"version": "1.1.0",
1010
"authors": {
1111
"name": "Simone Salerno",
1212
"url": "https://github.com/eloquentarduino"

‎library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=EloquentArduino
2-
version=1.0.0
2+
version=1.1.0
33
author=Simone Salerno <github.com/eloquentarduino>
44
maintainer=Simone Salerno <github.com/eloquentarduino>
55
sentence=An eloquent interface to common Arduino patterns

‎src/EloquentArduino.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
4+
#define _ELOQUENTARDUINO_H_ 1
5+
6+
#include "eloquentarduino/io/print.h"
7+
#include "eloquentarduino/vision/io/decoders/GrayscaleRandomAccessDecoder.h"
8+
#include "eloquentarduino/vision/io/decoders/Red565RandomAccessDecoder.h"
9+
#include "eloquentarduino/vision/processing/downscaling/Downscaler.h"
10+
#include "eloquentarduino/vision/processing/MotionDetector.h"

‎src/eloquentarduino/io/print.h

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,16 @@
55
namespace eloquent {
66
namespace io {
77

8-
9-
//#include "black_magic.h"
10-
11-
//#define serialprint(...) EVAL(MAP(Serial.print, __VA_ARGS__))
12-
13-
//#define _FCSV(f, x) f.print(x); f.print('\t');
14-
//#define fcsv(f, ...) EVAL(MAP(Serial.print, __VA_ARGS__)) f.print('\n');
15-
//#define fcsv(f, ...) EVAL(MAP_WITH_ARG(_FCSV, f, __VA_ARGS__)) f.print('\n');
16-
//#define csv(...) fcsv(Serial, __VA_ARGS__);
17-
18-
198
/**
20-
* Stop condition
9+
* fprint_all stop condition
2110
* @param stream
2211
*/
23-
void fprint_all(Stream* stream) {}
12+
void fprint_all(Stream* stream) {
13+
stream->print('\n');
14+
}
2415

2516
/**
17+
* Print all arguments
2618
* @tparam T
2719
* @tparam Args
2820
* @param stream
@@ -35,50 +27,33 @@ namespace eloquent {
3527
fprint_all(stream, args...);
3628
}
3729

38-
/**
39-
* @tparam Args
40-
* @param args
41-
*/
42-
template<typename... Args>
43-
void print_all(Args... args) {
44-
fprint_all(&Serial, args...);
45-
}
46-
4730

4831
/**
49-
* Stop condition
32+
* print_csv stop condition
5033
* @param stream
5134
*/
52-
void printf_csv(Stream* stream) {
35+
void fprint_csv(Stream* stream) {
5336
stream->print('\n');
5437
}
5538

5639
/**
57-
*
40+
* Print all arguments comma-separated
5841
* @tparam T
5942
* @tparam Args
6043
* @param stream
6144
* @param first
6245
* @param args
6346
*/
6447
template<typename T, typename... Args>
65-
void printf_csv(Stream* stream, T first, Args... args) {
48+
void fprint_csv(Stream* stream, T first, Args... args) {
6649
stream->print(first);
6750
stream->print('\t');
68-
printf_csv(stream, args...);
51+
fprint_csv(stream, args...);
6952
}
7053

71-
/**
72-
*
73-
* @tparam Args
74-
* @param args
75-
*/
76-
template<typename... Args>
77-
void print_csv(Args... args) { printf_csv(&Serial, args...); }
78-
7954

8055
/**
81-
*
56+
* Print array
8257
*/
8358
template<typename T>
8459
void fprint_array(Stream *stream, T *array, uint16_t length, char separator=',') {
@@ -88,15 +63,6 @@ namespace eloquent {
8863
}
8964
}
9065

91-
92-
/**
93-
*
94-
*/
95-
template<typename T>
96-
void print_array(T *array, uint16_t length, char separator=',') {
97-
fprint_array(&Serial, array, length, separator);
98-
}
99-
10066
}
10167
}
10268

‎src/eloquentarduino/io/serial_print.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include "print.h"
4+
5+
#if defined(Stream_h)
6+
7+
namespace eloquent {
8+
namespace io {
9+
10+
/**
11+
* Print all arguments to Serial
12+
* @tparam Args
13+
* @param args
14+
*/
15+
template<typename... Args>
16+
void print_all(Args... args) {
17+
fprint_all(&Serial, args...);
18+
}
19+
20+
21+
/**
22+
* Print CSV-style line to serial
23+
* @tparam Args
24+
* @param args
25+
*/
26+
template<typename... Args>
27+
void print_csv(Args... args) {
28+
fprint_csv(&Serial, args...);
29+
}
30+
31+
32+
/**
33+
* Print array to Serial
34+
*/
35+
template<typename T>
36+
void print_array(T *array, uint16_t length, char separator=',') {
37+
fprint_array(&Serial, array, length, separator);
38+
}
39+
40+
}
41+
}
42+
43+
#endif

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /