Skip to content

Commit 21522cb

Browse files
committed
Initial commit
1 parent 4d5566e commit 21522cb

File tree

7 files changed

+590
-0
lines changed

7 files changed

+590
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Secrets.h

esp32-ai-server/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM python:3.7.3-stretch
2+
3+
# Make working directories
4+
RUN mkdir -p /esp32-ai-server
5+
WORKDIR /esp32-ai-server
6+
7+
# Upgrade pip with no cache
8+
RUN pip install --no-cache-dir -U pip
9+
10+
# Copy application requirements file to the created working directory
11+
COPY requirements.txt .
12+
13+
# Install application dependencies from the requirements file
14+
RUN pip install -r requirements.txt
15+
16+
# Copy every file in the source folder to the created working directory
17+
COPY . .
18+
19+
# Run the python application
20+
CMD ["python", "main.py"]

esp32-ai-server/main.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import tensorflow as tf
2+
import numpy as np
3+
import base64
4+
import io
5+
import os
6+
from tensorflow.keras.preprocessing import image
7+
from tensorflow.keras.applications import imagenet_utils
8+
from PIL import Image
9+
from fastapi import Request, FastAPI
10+
from fastapi.middleware.cors import CORSMiddleware
11+
from uvicorn import run
12+
13+
filename = 'tempimage.jpg'
14+
model = tf.keras.applications.mobilenet_v2.MobileNetV2()
15+
16+
app = FastAPI()
17+
18+
origins = ["*"]
19+
methods = ["*"]
20+
headers = ["*"]
21+
22+
app.add_middleware(
23+
CORSMiddleware,
24+
allow_origins = origins,
25+
allow_credentials = True,
26+
allow_methods = methods,
27+
allow_headers = headers
28+
)
29+
30+
def b64toimg(s):
31+
try:
32+
img = Image.open(io.BytesIO(base64.b64decode(s)))
33+
img.save(filename, 'jpeg')
34+
return True
35+
except:
36+
return False
37+
38+
def processImage(imagestr):
39+
if(b64toimg(imagestr) == False):
40+
return {"error": True}
41+
img = image.load_img(filename,target_size=(224,224))
42+
resizedimg = image.img_to_array(img)
43+
finalimg = np.expand_dims(resizedimg,axis=0)
44+
finalimg = tf.keras.applications.mobilenet_v2.preprocess_input(finalimg)
45+
finalimg.shape
46+
predictions = model.predict(finalimg)
47+
return {"predictions": list(map(lambda x: {"label":x[1], "prediction": float(x[2])}, imagenet_utils.decode_predictions(predictions)[0])) }
48+
49+
@app.post("/")
50+
async def get_net_image_prediction(request: Request):
51+
return processImage((await request.json())["image"])
52+
53+
if __name__ == "__main__":
54+
port = int(os.environ.get('PORT', 2137))
55+
run(app, host="0.0.0.0", port=port)

esp32-ai-server/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fastapi==0.73.0
2+
numpy==1.19.5
3+
uvicorn==0.15.0
4+
image==1.5.33
5+
tensorflow

object_detector/Base64.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright (c) 2013 Adam Rudd.
3+
* See LICENSE for more information
4+
* https://github.com/adamvr/arduino-base64
5+
*/
6+
#if (defined(__AVR__))
7+
#include <avr\pgmspace.h>
8+
#else
9+
#include <pgmspace.h>
10+
#endif
11+
12+
const char PROGMEM b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13+
"abcdefghijklmnopqrstuvwxyz"
14+
"0123456789+/";
15+
16+
/* 'Private' declarations */
17+
inline void a3_to_a4(unsigned char * a4, unsigned char * a3);
18+
inline void a4_to_a3(unsigned char * a3, unsigned char * a4);
19+
inline unsigned char b64_lookup(char c);
20+
21+
int base64_encode(char *output, char *input, int inputLen) {
22+
int i = 0, j = 0;
23+
int encLen = 0;
24+
unsigned char a3[3];
25+
unsigned char a4[4];
26+
27+
while(inputLen--) {
28+
a3[i++] = *(input++);
29+
if(i == 3) {
30+
a3_to_a4(a4, a3);
31+
32+
for(i = 0; i < 4; i++) {
33+
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[i]]);
34+
}
35+
36+
i = 0;
37+
}
38+
}
39+
40+
if(i) {
41+
for(j = i; j < 3; j++) {
42+
a3[j] = '\0';
43+
}
44+
45+
a3_to_a4(a4, a3);
46+
47+
for(j = 0; j < i + 1; j++) {
48+
output[encLen++] = pgm_read_byte(&b64_alphabet[a4[j]]);
49+
}
50+
51+
while((i++ < 3)) {
52+
output[encLen++] = '=';
53+
}
54+
}
55+
output[encLen] = '\0';
56+
return encLen;
57+
}
58+
59+
int base64_decode(char * output, char * input, int inputLen) {
60+
int i = 0, j = 0;
61+
int decLen = 0;
62+
unsigned char a3[3];
63+
unsigned char a4[4];
64+
65+
66+
while (inputLen--) {
67+
if(*input == '=') {
68+
break;
69+
}
70+
71+
a4[i++] = *(input++);
72+
if (i == 4) {
73+
for (i = 0; i <4; i++) {
74+
a4[i] = b64_lookup(a4[i]);
75+
}
76+
77+
a4_to_a3(a3,a4);
78+
79+
for (i = 0; i < 3; i++) {
80+
output[decLen++] = a3[i];
81+
}
82+
i = 0;
83+
}
84+
}
85+
86+
if (i) {
87+
for (j = i; j < 4; j++) {
88+
a4[j] = '\0';
89+
}
90+
91+
for (j = 0; j <4; j++) {
92+
a4[j] = b64_lookup(a4[j]);
93+
}
94+
95+
a4_to_a3(a3,a4);
96+
97+
for (j = 0; j < i - 1; j++) {
98+
output[decLen++] = a3[j];
99+
}
100+
}
101+
output[decLen] = '\0';
102+
return decLen;
103+
}
104+
105+
int base64_enc_len(int plainLen) {
106+
int n = plainLen;
107+
return (n + 2 - ((n + 2) % 3)) / 3 * 4;
108+
}
109+
110+
int base64_dec_len(char * input, int inputLen) {
111+
int i = 0;
112+
int numEq = 0;
113+
for(i = inputLen - 1; input[i] == '='; i--) {
114+
numEq++;
115+
}
116+
117+
return ((6 * inputLen) / 8) - numEq;
118+
}
119+
120+
inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
121+
a4[0] = (a3[0] & 0xfc) >> 2;
122+
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
123+
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
124+
a4[3] = (a3[2] & 0x3f);
125+
}
126+
127+
inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
128+
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
129+
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
130+
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
131+
}
132+
133+
inline unsigned char b64_lookup(char c) {
134+
if(c >='A' && c <='Z') return c - 'A';
135+
if(c >='a' && c <='z') return c - 71;
136+
if(c >='0' && c <='9') return c + 4;
137+
if(c == '+') return 62;
138+
if(c == '/') return 63;
139+
return -1;
140+
}

object_detector/Base64.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2013 Adam Rudd.
3+
* See LICENSE for more information
4+
* https://github.com/adamvr/arduino-base64
5+
*/
6+
#ifndef _BASE64_H
7+
#define _BASE64_H
8+
9+
/* b64_alphabet:
10+
* Description: Base64 alphabet table, a mapping between integers
11+
* and base64 digits
12+
* Notes: This is an extern here but is defined in Base64.c
13+
*/
14+
extern const char b64_alphabet[];
15+
16+
/* base64_encode:
17+
* Description:
18+
* Encode a string of characters as base64
19+
* Parameters:
20+
* output: the output buffer for the encoding, stores the encoded string
21+
* input: the input buffer for the encoding, stores the binary to be encoded
22+
* inputLen: the length of the input buffer, in bytes
23+
* Return value:
24+
* Returns the length of the encoded string
25+
* Requirements:
26+
* 1. output must not be null or empty
27+
* 2. input must not be null
28+
* 3. inputLen must be greater than or equal to 0
29+
*/
30+
int base64_encode(char *output, char *input, int inputLen);
31+
32+
/* base64_decode:
33+
* Description:
34+
* Decode a base64 encoded string into bytes
35+
* Parameters:
36+
* output: the output buffer for the decoding,
37+
* stores the decoded binary
38+
* input: the input buffer for the decoding,
39+
* stores the base64 string to be decoded
40+
* inputLen: the length of the input buffer, in bytes
41+
* Return value:
42+
* Returns the length of the decoded string
43+
* Requirements:
44+
* 1. output must not be null or empty
45+
* 2. input must not be null
46+
* 3. inputLen must be greater than or equal to 0
47+
*/
48+
int base64_decode(char *output, char *input, int inputLen);
49+
50+
/* base64_enc_len:
51+
* Description:
52+
* Returns the length of a base64 encoded string whose decoded
53+
* form is inputLen bytes long
54+
* Parameters:
55+
* inputLen: the length of the decoded string
56+
* Return value:
57+
* The length of a base64 encoded string whose decoded form
58+
* is inputLen bytes long
59+
* Requirements:
60+
* None
61+
*/
62+
int base64_enc_len(int inputLen);
63+
64+
/* base64_dec_len:
65+
* Description:
66+
* Returns the length of the decoded form of a
67+
* base64 encoded string
68+
* Parameters:
69+
* input: the base64 encoded string to be measured
70+
* inputLen: the length of the base64 encoded string
71+
* Return value:
72+
* Returns the length of the decoded form of a
73+
* base64 encoded string
74+
* Requirements:
75+
* 1. input must not be null
76+
* 2. input must be greater than or equal to zero
77+
*/
78+
int base64_dec_len(char *input, int inputLen);
79+
80+
#endif // _BASE64_H

0 commit comments

Comments
 (0)