ESP32 CAM using Arduino | Python webcam

 # ESP32 CAM using Arduino


 # Go to preferences and ADD link!

# Copy-paste the following URLs at Aditional boards Manager URLs : 
  • 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
 
Go To Boards Manager


Install this

# CODE: Arduino!
 upload the code

  1. #include <WebServer.h>
  2. #include <WiFi.h>
  3. #include <esp32cam.h>

  4. const char* WIFI_SSID = "WIFI_NAME";
  5. const char* WIFI_PASS = "WIFI_PASS";

  6. WebServer server(80);

  7. static auto loRes = esp32cam::Resolution::find(320, 240);
  8. static auto hiRes = esp32cam::Resolution::find(800, 600);

  9. void
  10. handleBmp()
  11. {
  12. if (!esp32cam::Camera.changeResolution(loRes)) {
  13. Serial.println("SET-LO-RES FAIL");
  14. }

  15. auto frame = esp32cam::capture();
  16. if (frame == nullptr) {
  17. Serial.println("CAPTURE FAIL");
  18. server.send(503, "", "");
  19. return;
  20. }
  21. Serial.printf("CAPTURE OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
  22. static_cast<int>(frame->size()));

  23. if (!frame->toBmp()) {
  24. Serial.println("CONVERT FAIL");
  25. server.send(503, "", "");
  26. return;
  27. }
  28. Serial.printf("CONVERT OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
  29. static_cast<int>(frame->size()));

  30. server.setContentLength(frame->size());
  31. server.send(200, "image/bmp");
  32. WiFiClient client = server.client();
  33. frame->writeTo(client);
  34. }

  35. void
  36. serveJpg()
  37. {
  38. auto frame = esp32cam::capture();
  39. if (frame == nullptr) {
  40. Serial.println("CAPTURE FAIL");
  41. server.send(503, "", "");
  42. return;
  43. }
  44. Serial.printf("CAPTURE OK %dx%d %db\n", frame->getWidth(), frame->getHeight(),
  45. static_cast<int>(frame->size()));

  46. server.setContentLength(frame->size());
  47. server.send(200, "image/jpeg");
  48. WiFiClient client = server.client();
  49. frame->writeTo(client);
  50. }

  51. void
  52. handleJpgLo()
  53. {
  54. if (!esp32cam::Camera.changeResolution(loRes)) {
  55. Serial.println("SET-LO-RES FAIL");
  56. }
  57. serveJpg();
  58. }

  59. void
  60. handleJpgHi()
  61. {
  62. if (!esp32cam::Camera.changeResolution(hiRes)) {
  63. Serial.println("SET-HI-RES FAIL");
  64. }
  65. serveJpg();
  66. }

  67. void
  68. handleJpg()
  69. {
  70. server.sendHeader("Location", "/cam-hi.jpg");
  71. server.send(302, "", "");
  72. }

  73. void
  74. handleMjpeg()
  75. {
  76. if (!esp32cam::Camera.changeResolution(hiRes)) {
  77. Serial.println("SET-HI-RES FAIL");
  78. }

  79. Serial.println("STREAM BEGIN");
  80. WiFiClient client = server.client();
  81. auto startTime = millis();
  82. int res = esp32cam::Camera.streamMjpeg(client);
  83. if (res <= 0) {
  84. Serial.printf("STREAM ERROR %d\n", res);
  85. return;
  86. }
  87. auto duration = millis() - startTime;
  88. Serial.printf("STREAM END %dfrm %0.2ffps\n", res, 1000.0 * res / duration);
  89. }

  90. void
  91. setup()
  92. {
  93. Serial.begin(115200);
  94. Serial.println();

  95. {
  96. using namespace esp32cam;
  97. Config cfg;
  98. cfg.setPins(pins::AiThinker);
  99. cfg.setResolution(hiRes);
  100. cfg.setBufferCount(2);
  101. cfg.setJpeg(80);

  102. bool ok = Camera.begin(cfg);
  103. Serial.println(ok ? "CAMERA OK" : "CAMERA FAIL");
  104. }

  105. WiFi.persistent(false);
  106. WiFi.mode(WIFI_STA);
  107. WiFi.begin(WIFI_SSID, WIFI_PASS);
  108. while (WiFi.status() != WL_CONNECTED) {
  109. delay(500);
  110. }

  111. Serial.print("http://");
  112. Serial.println(WiFi.localIP());
  113. Serial.println(" /cam.bmp");
  114. Serial.println(" /cam-lo.jpg");
  115. Serial.println(" /cam-hi.jpg");
  116. Serial.println(" /cam.mjpeg");

  117. server.on("/cam.bmp", handleBmp);
  118. server.on("/cam-lo.jpg", handleJpgLo);
  119. server.on("/cam-hi.jpg", handleJpgHi);
  120. server.on("/cam.jpg", handleJpg);
  121. server.on("/cam.mjpeg", handleMjpeg);

  122. server.begin();
  123. }

  124. void
  125. loop()
  126. {
  127. server.handleClient();
  128. }

# CODE: Python!

  1. import cv2
  2. import urllib.request
  3. import numpy as np

  4. face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
  5. url = 'http://192.168.1.34/cam-lo.jpg'

  6. cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE)

  7. while True:
  8. imgResponse = urllib.request.urlopen(url)
  9. imgnp = np.array(bytearray(imgResponse.read()), dtype=np.uint8)
  10. img = cv2.imdecode(imgnp, -1)
  11. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  12. face = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

  13. for x, y, w, h in face:
  14. img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)

  15. cv2.imshow("milgaya", img)
  16. key = cv2.waitKey(5)
  17. if key == ord('q'):
  18. break

  19. cv2.destroyAllWindows()






Post a Comment

If you have any doubts, please let me know

Previous Post Next Post