SSRN Id4152378
SSRN Id4152378
SSRN Id4152378
Ninad Mehendale
Abstract:
This paper covers ESP32 CAM Based Object Detection & Identification with OpenCV. OpenCV is an
open-sourced image processing library that is very widely used not just in industry but also in the field
of research and development. Here for object detection cvlib Library [1] is used. The library uses a pre-
trained AI model on the COCO dataset to detect objects. The name of the pre-trained model is YOLOv3.
For hardware, the ESP32 Camera Module was used which can be programmed through FTDI Module.
It is required to set up the Arduino IDE for the ESP32 Camera Module. It is important to upload the
firmware and then work on the object detection & identification part. The script for object detection is
written in the python programming language, thus people who want to use this code will also have to
install Python and its required Libraries. This project makes use of OpenCV for Object Detection &
Identification.
Figure 3: connection between FTDI Module and the ESP32 CAM module.
As shown in figure 3 Connect the 5V & GND Pin of ESP32 to the 5V & GND of the FTDI Module.
Similarly, connect the Rx to UOT and Tx to UOR Pin. And the most important thing, you need to short
the IO0 and GND Pin together. This is to put the device in programming mode. Once programming is
done you can remove it.
Installing ESP32CAM Library: Here we will not use the general ESP webserver example but rather
another streaming process. Therefore, we need to add another ESPCAM library. The esp32cam library
provides an object-oriented API to use the OV2640 camera on the ESP32 microcontroller. It is a
wrapper of the esp32-camera library.
Figure 4: Screen shot of Github Link [2] where user can download the zip library as in the image
Once downloaded add zip library as shown in figure 4 to Arduino Library Folder.
To do so follow the following steps:
Open Arduino -> Sketch -> Include Library -> Add .ZIP Library -> Navigate to downloaded zip file -
> add
Source Code/Program for ESP32 CAM Module:
#include <WebServer.h>
#include <WiFi.h>
#include <esp32cam.h>
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()
void handleJpgMid()
{
if (!esp32cam::Camera.changeResolution(midRes)) {
Serial.println("SET-MID-RES FAIL");
}
serveJpg();
}
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);
server.on("/cam-lo.jpg", handleJpgLo);
server.on("/cam-hi.jpg", handleJpgHi);
server.on("/cam-mid.jpg", handleJpgMid);
server.begin();
}
void loop()
{
server.handleClient();
}
Here is a source code for Object Detection & Identification with ESP32 Camera & OpenCV. Copy the
code and paste it in the Arduino IDE.
Before Uploading the code, you have to make a small change to the code. Change the SSID and
password variable and in accordance with your Wi-Fi network. Now compile and upload it to the ESP32
CAM Board. But during uploading, you have to follow few steps every time.
Make sure the IO0 pin is shorted with the ground when you have pressed the upload button.
If you see the dots and dashes while uploading press the reset button immediately
Once the code is uploaded, remove the I01 pin shorting with Ground and press the reset button
once again.
If the output is the Serial monitor is still not there then press the reset button again.
Now you can see a similar output as in figure 5.
url='http://192.168.10.162/cam-hi.jpg'
im=None
def run1():
cv2.namedWindow("live transmission", cv2.WINDOW_AUTOSIZE)
while True:
img_resp=urllib.request.urlopen(url)
imgnp=np.array(bytearray(img_resp.read()),dtype=np.uint8)
im = cv2.imdecode(imgnp,-1)
cv2.destroyAllWindows()
def run2():
cv2.namedWindow("detection", cv2.WINDOW_AUTOSIZE)
while True:
img_resp=urllib.request.urlopen(url)
imgnp=np.array(bytearray(img_resp.read()),dtype=np.uint8)
im = cv2.imdecode(imgnp,-1)
cv2.imshow('detection',im)
key=cv2.waitKey(5)
if key==ord('q'):
break
cv2.destroyAllWindows()
if __name__ == '__main__':
print("started")
with concurrent.futures.ProcessPoolExecutor() as executer:
f1= executer.submit(run1)
f2= executer.submit(run2)
Here we have to replace the IP address with the IP on Arduino Serial Monitor. For the first time, it will
install a few files if they are not existing. Once we have done that, we can see two windows named live
transmission and detected is visible. Now in the detected window, one can view different detected
objects as around them different coloured boxes are visible. The video for this application is also
available on https://youtu.be/A1SPJSVra9I [3]
References
[1] https://www.cvlib.net/
[2] https://github.com/yoursunny/esp32cam
[3] https://how2electronics.com/esp32-cam-based-object-detection-identification-with-opencv/