# ESP32 CAM using Arduino
# Go to preferences and ADD link!
- Link 1: https://dl.espressif.com/dl/package_esp32_index.json
- Link 2: http://arduino.esp8266.com/stable/package_esp8266com_index.json
# Put link-1 and link-2 at one place!
# ESP32 INSTALL
# CODE: Arduino!
upload the code
- #include <WebServer.h>
- #include <WiFi.h>
- #include <esp32cam.h>
- const char* WIFI_SSID = "WIFI_NAME";
- const char* WIFI_PASS = "WIFI_PASS";
- WebServer server(80);
- static auto loRes = esp32cam::Resolution::find(320, 240);
- static auto hiRes = esp32cam::Resolution::find(800, 600);
- void
- handleBmp()
- {
- if (!esp32cam::Camera.changeResolution(loRes)) {
- Serial.println("SET-LO-RES FAIL");
- }
- auto frame = esp32cam::capture();
- if (frame == nullptr) {
- Serial.println("CAPTURE FAIL");
- server.send(503, "", "");
- return;
- }
- Serial.printf("CAPTURE OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
- static_cast<int>(frame->size()));
- if (!frame->toBmp()) {
- Serial.println("CONVERT FAIL");
- server.send(503, "", "");
- return;
- }
- Serial.printf("CONVERT OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
- static_cast<int>(frame->size()));
- server.setContentLength(frame->size());
- server.send(200, "image/bmp");
- WiFiClient client = server.client();
- frame->writeTo(client);
- }
- void
- serveJpg()
- {
- auto frame = esp32cam::capture();
- if (frame == nullptr) {
- Serial.println("CAPTURE FAIL");
- server.send(503, "", "");
- return;
- }
- Serial.printf("CAPTURE OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
- static_cast<int>(frame->size()));
- server.setContentLength(frame->size());
- server.send(200, "image/jpeg");
- WiFiClient client = server.client();
- frame->writeTo(client);
- }
- void
- handleJpgLo()
- {
- if (!esp32cam::Camera.changeResolution(loRes)) {
- Serial.println("SET-LO-RES FAIL");
- }
- serveJpg();
- }
- void
- handleJpgHi()
- {
- if (!esp32cam::Camera.changeResolution(hiRes)) {
- Serial.println("SET-HI-RES FAIL");
- }
- serveJpg();
- }
- void
- handleJpg()
- {
- server.sendHeader("Location", "/cam-hi.jpg");
- server.send(302, "", "");
- }
- void
- handleMjpeg()
- {
- if (!esp32cam::Camera.changeResolution(hiRes)) {
- Serial.println("SET-HI-RES FAIL");
- }
- Serial.println("STREAM BEGIN");
- WiFiClient client = server.client();
- auto startTime = millis();
- int res = esp32cam::Camera.streamMjpeg(client);
- if (res <= 0) {
- Serial.printf("STREAM ERROR %d\n", res);
- return;
- }
- auto duration = millis() - startTime;
- Serial.printf("STREAM END %dfrm %0.2ffps\n", res, 1000.0 * res / duration);
- }
- void
- setup()
- {
- Serial.begin(115200);
- Serial.println();
- {
- using namespace esp32cam;
- Config cfg;
- cfg.setPins(pins::AiThinker);
- cfg.setResolution(hiRes);
- cfg.setBufferCount(2);
- cfg.setJpeg(80);
- bool ok = Camera.begin(cfg);
- Serial.println(ok ? "CAMERA OK" : "CAMERA FAIL");
- }
- WiFi.persistent(false);
- WiFi.mode(WIFI_STA);
- WiFi.begin(WIFI_SSID, WIFI_PASS);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- }
- Serial.print("http://");
- Serial.println(WiFi.localIP());
- Serial.println(" /cam.bmp");
- Serial.println(" /cam-lo.jpg");
- Serial.println(" /cam-hi.jpg");
- Serial.println(" /cam.mjpeg");
- server.on("/cam.bmp", handleBmp);
- server.on("/cam-lo.jpg", handleJpgLo);
- server.on("/cam-hi.jpg", handleJpgHi);
- server.on("/cam.jpg", handleJpg);
- server.on("/cam.mjpeg", handleMjpeg);
- server.begin();
- }
- void
- loop()
- {
- server.handleClient();
- }
# CODE: Python!
- import cv2
- import urllib.request
- import numpy as np
- face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
- url = 'http://192.168.1.34/cam-lo.jpg'
- cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)
- while True:
- imgResponse = urllib.request.urlopen(url)
- imgnp = np.array(bytearray(imgResponse.read()), dtype=np.uint8)
- img = cv2.imdecode(imgnp, -1)
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- face = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
- for x, y, w, h in face:
- img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)
- cv2.imshow("milgaya", img)
- key = cv2.waitKey(5)
- if key == ord('q'):
- break
- cv2.destroyAllWindows()