Laporan Praktikum 1
Laporan Praktikum 1
Laporan Praktikum 1
Code :
/*
Rui Santos
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include "esp_camera.h"
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
String Feedback="";
String Command="",cmd="",P1="",P2="",P3="",P4="",P5="",P6="",P7="",P8="",P9="";
byte ReceiveState=0,cmdState=1,strState=1,questionstate=0,equalstate=0,semicolonstate=0;
// CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
WiFiServer server(80);
void ExecuteCommand() {
if (cmd!="getstill") {
Serial.println("cmd= "+cmd+" ,P1= "+P1+" ,P2= "+P2+" ,P3= "+P3+" ,P4= "+P4+" ,P5= "+P5+" ,P6=
"+P6+" ,P7= "+P7+" ,P8= "+P8+" ,P9= "+P9);
Serial.println("");
if (cmd=="resetwifi") {
WiFi.begin(P1.c_str(), P2.c_str());
Serial.print("Connecting to ");
Serial.println(P1);
long int StartTime=millis();
delay(500);
Serial.println("");
Serial.println("STAIP: "+WiFi.localIP().toString());
Feedback="STAIP: "+WiFi.localIP().toString();
else if (cmd=="restart") {
ESP.restart();
else if (cmd=="quality") {
sensor_t * s = esp_camera_sensor_get();
s->set_quality(s, val);
else if (cmd=="contrast") {
sensor_t * s = esp_camera_sensor_get();
s->set_contrast(s, val);
else if (cmd=="brightness") {
sensor_t * s = esp_camera_sensor_get();
s->set_brightness(s, val);
else {
}
if (Feedback=="") {
Feedback=Command;
void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
if(psramFound()){
config.frame_size = FRAMESIZE_UXGA;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.fb_count = 1;
// camera init
if (err != ESP_OK) {
delay(1000);
ESP.restart();
sensor_t * s = esp_camera_sensor_get();
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
delay(1000);
long int StartTime=millis();
delay(500);
break;
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
server.begin();
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https:\/\/code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https:\/\/cdn.jsdelivr.net/gh/justadudewhohacks/face-api.js@0.22.1/dist/face-
api.min.js"></script>
</head><body>
<div id="container"></div>
<table>
<tr>
<tr>
<td>MirrorImage</td>
<td colspan="2">
<select id="mirrorimage">
<option value="1">yes</option>
<option value="0">no</option>
</select>
</td>
</tr>
<tr>
<td>Quality</td>
</tr>
<tr>
<td>Brightness</td>
</tr>
<tr>
<td>Contrast</td>
</tr>
</table>
</body>
</html>
<script>
var myTimer;
var restartCount=0;
let currentStream;
let faceDetection;
Promise.all([
faceapi.nets.tinyFaceDetector.load(modelPath),
faceapi.nets.faceLandmark68TinyNet.load(modelPath),
faceapi.nets.faceRecognitionNet.load(modelPath),
faceapi.nets.faceExpressionNet.load(modelPath),
faceapi.nets.ageGenderNet.load(modelPath)
])
clearInterval(myTimer);
myTimer = setInterval(function(){error_handle();},5000);
ShowImage.src=location.origin+'/?getstill='+Math.random();
function error_handle() {
restartCount++;
clearInterval(myTimer);
if (restartCount<=2) {
myTimer = setInterval(function(){getStill.click();},10000);
ifr.src = document.location.origin+'?restart';
else
message.innerHTML = "Get still error. <br>Please close the page and check ESP32-CAM.";
getStill.style.display = "block";
clearInterval(myTimer);
restartCount=0;
canvas.setAttribute("width", ShowImage.width);
canvas.setAttribute("height", ShowImage.height);
canvas.style.display = "block";
if (mirrorimage.value==1) {
context.scale(-1, 1);
context.setTransform(1, 0, 0, 1, 0, 0);
else {
context.drawImage(ShowImage,0,0,ShowImage.width,ShowImage.height);
DetectImage();
fetch(location.origin+'/?restart=stop');
fetch(document.location.origin+'/?quality='+this.value+';stop');
fetch(document.location.origin+'/?brightness='+this.value+';stop');
faceapi.draw.drawDetections(canvas, resizedDetections)
faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
resizedDetections.forEach(result => {
message.innerHTML ="";
var maxEmotion="neutral";
var maxProbability=expressions.neutral;
if (expressions.happy>maxProbability) {
maxProbability=expressions.happy;
maxEmotion="happy";
if (expressions.sad>maxProbability) {
maxProbability=expressions.sad;
maxEmotion="sad";
if (expressions.angry>maxProbability) {
maxProbability=expressions.angry;
maxEmotion="angry";
if (expressions.fearful>maxProbability) {
maxProbability=expressions.fearful;
maxEmotion="fearful";
if (expressions.disgusted>maxProbability) {
maxProbability=expressions.disgusted;
maxEmotion="disgusted";
if (expressions.surprised>maxProbability) {
maxProbability=expressions.surprised;
maxEmotion="surprised";
new faceapi.draw.DrawTextField(
`${ageF} years`,
`${gender} (${genderProbabilityF})`
],
result.detection.box.bottomRight
).draw(canvas)
})
try {
document.createEvent("TouchEvent");
setTimeout(function(){getStill.click();},500);
catch(e) {
setTimeout(function(){getStill.click();},500);
}
}
</script>
)rawliteral";
void loop() {
Feedback="";Command="";cmd="";P1="";P2="";P3="";P4="";P5="";P6="";P7="";P8="";P9="";
ReceiveState=0,cmdState=1,strState=1,questionstate=0,equalstate=0,semicolonstate=0;
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
getCommand(c);
if (c == '\n') {
if (currentLine.length() == 0) {
if (cmd=="getstill") {
camera_fb_t * fb = NULL;
fb = esp_camera_fb_get();
if(!fb) {
delay(1000);
ESP.restart();
}
client.println("HTTP/1.1 200 OK");
client.println("Access-Control-Allow-Origin: *");
client.println("Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS");
client.println("Content-Type: image/jpeg");
client.println("Connection: close");
client.println();
if (n+1024<fbLen) {
client.write(fbBuf, 1024);
fbBuf += 1024;
else if (fbLen%1024>0) {
client.write(fbBuf, remainder);
esp_camera_fb_return(fb);
else {
client.println("Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS");
client.println("Connection: close");
client.println();
String Data="";
if (cmd!="")
Data = Feedback;
else {
int Index;
client.print(Data.substring(Index, Index+1000));
client.println();
Feedback="";
break;
} else {
currentLine = "";
else if (c != '\r') {
currentLine += c;
if ((currentLine.indexOf("/?")!=-1)&&(currentLine.indexOf(" HTTP")!=-1)) {
if (Command.indexOf("stop")!=-1) {
client.println();
client.println();
client.stop();
}
currentLine="";
Feedback="";
ExecuteCommand();
delay(1);
client.stop();
if (c=='?') ReceiveState=1;
if (ReceiveState==1) {
Command=Command+String(c);
if (c=='=') cmdState=0;
if (c==';') strState++;
if ((cmdState==1)&&((c!='?')||(questionstate==1))) cmd=cmd+String(c);
if ((cmdState==0)&&(strState==1)&&((c!='=')||(equalstate==1))) P1=P1+String(c);
if ((cmdState==0)&&(strState==2)&&(c!=';')) P2=P2+String(c);
if ((cmdState==0)&&(strState==3)&&(c!=';')) P3=P3+String(c);
if ((cmdState==0)&&(strState==4)&&(c!=';')) P4=P4+String(c);
if ((cmdState==0)&&(strState==5)&&(c!=';')) P5=P5+String(c);
if ((cmdState==0)&&(strState==6)&&(c!=';')) P6=P6+String(c);
if ((cmdState==0)&&(strState==7)&&(c!=';')) P7=P7+String(c);
if ((cmdState==0)&&(strState==8)&&(c!=';')) P8=P8+String(c);
if ((cmdState==0)&&(strState>=9)&&((c!=';')||(semicolonstate==1))) P9=P9+String(c);
if (c=='?') questionstate=1;
if (c=='=') equalstate=1;
if ((strState>=9)&&(c==';')) semicolonstate=1;
Hasil :
/*********
Rui Santos
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
*********/
#include "WiFi.h"
#include "esp_camera.h"
#include "esp_timer.h"
#include "img_converters.h"
#include "Arduino.h"
#include "driver/rtc_io.h"
#include <ESPAsyncWebServer.h>
#include <StringArray.h>
#include <SPIFFS.h>
#include <FS.h>
AsyncWebServer server(80);
// CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
<!DOCTYPE html>
<html>
<head>
<script src="https:\/\/code.jquery.com/jquery-3.3.1.min.js"></script>
<script src='https:\/\/cdn.jsdelivr.net/gh/justadudewhohacks/face-api.js@0.22.1/dist/face-
api.min.js'></script>
<style>
#container { padding: 0; }
canvas {
position: absolute;
top: 0;
left: 0;
z-index:999;
img {
width: auto ;
max-width: 100% ;
height: auto ;
.flex-container {
flex-direction: column;
button {
padding: 10px;
margin-bottom: 30px;
</style>
</head>
<body>
<div class="flex-container">
<div>
<div id="container"></div>
</div>
<div>
<img src="test-photo.jpg"></img>
</div>
</div>
<div>
<ol>
<li>Press the "ENROLL NEW FACE - CAPTURE TEST PHOTO" button and refresh the page</li>
<li>Refresh the page. The face recognition code runs automatically, so you have to wait 5 to
40 seconds for an alert with the face recognition result.</li>
</ol>
<p><b>IMPORTANT:</b> if you don't see a popup with your Face Recognition result, refresh
the web page and wait a few seconds. I only recommend testing this web server on a
laptop/desktop computer with Google Chrome web browser.</p>
<p>You can only do face recognition for one subject - your TEST PHOTO should only have one
face.</p>
</div>
</body>
<script>
function capturePhoto() {
var xhr = new XMLHttpRequest();
xhr.send();
function captureTestPhoto(){
xhr.send();
let currentStream;
let convas;
let faceDetection;
Promise.all([
faceapi.nets.faceRecognitionNet.loadFromUri(modelPath),
faceapi.nets.faceLandmark68Net.loadFromUri(modelPath),
faceapi.nets.ssdMobilenetv1.loadFromUri(modelPath)
]).then(start());
container.style.position = 'relative'
document.body.append(container)
if( document.getElementsByTagName("canvas").length == 0 ) {
canvas = faceapi.createCanvasFromMedia(ShowImage)
document.getElementById('container').append(canvas)
results.forEach((result, i) => {
drawBox.draw(canvas)
xhr.send();
})
alert("DONE!");
function loadLabeledImages() {
return Promise.all(
const descriptions = []
descriptions.push(detections.descriptor)
})
</script>
</html>)rawliteral";
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
delay(1000);
Serial.println("Connecting to WiFi...");
if (!SPIFFS.begin(true)) {
ESP.restart();
else {
delay(500);
Serial.println(WiFi.localIP());
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
if (psramFound()) {
config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
// Camera init
ESP.restart();
});
takeNewPhoto = true;
});
Serial.println("Trigger action");
});
takeNewPhoto2 = true;
});
});
// Start server
server.begin();
void loop() {
if (takeNewPhoto) {
takeNewPhoto = capturePhotoSaveSpiffs(FILE_PHOTO.c_str());
if (takeNewPhoto2) {
takeNewPhoto2 = capturePhotoSaveSpiffs(FILE_PHOTO_2.c_str());
delay(1);
do {
Serial.println("Taking a photo...");
fb = esp_camera_fb_get();
if (!fb) {
return true;
if (!file) {
else {
Serial.print(photoName);
Serial.print(file.size());
Serial.println(" bytes");
file.close();
esp_camera_fb_return(fb);
ok = checkPhoto(SPIFFS, photoName);
} while ( !ok );
return false;
}
Hasil :