People Detection
Introduction
In this example we use a pre-trained model to find out if a person is in sight of the camera, installed on our esp32-cam board. Before starting the project we must make sure that the camera is working and that we can read images from it.
The pre-trained model uses 96x96 pixel gray scale images as its input (features), which means that we must be able to read this type of image from the ov2640 camera on the esp-cam board. It turns out that this is pretty simple and below you can find a MicroPython script reading such an image and saving it to a file
camImage.raw on the ESP32 filesystem on its 4MByte flash.
# Reads a 96x96 pixel gray scale image from the camera in raw mode
# This image can directly be passed into the input tensor of the
# person detection model
# The program is part of a course on AI and edge computing at the
# University of Cape Coast, Ghana
# Copyright (c) U. Raich [2022]
# The program is released under the MIT License
import sys
import camera
from utime import sleep_ms
try:
camera.init(0,format=camera.GRAYSCALE,framesize=camera.FRAME_96X96)
except:
print("Error when initializing the camera")
sys.exit()
buf=camera.capture()
print("type: ", type(buf), " Length: ",len(buf))
# save the raw image to a file
print("Writing the data to camImage.raw")
if len(buf) == 96*96:
f = open("camImage.raw","w+b")
f.write(buf)
f.close()
camera.deinit()
Once we have a saved image we can transfer it to the PC via the serial connection and display it there. The simple shell script below transfers the raw image file. It requires
rshell to be installed on the PC.
#!/bin/sh
rshell cp /pyboard/camImage.raw .
Finally the image can be displayed on the PC using the script below:
#!/usr/bin/python3
from PIL import Image
import io,sys
if len(sys.argv) != 2:
print("Usage %s filename"%sys.argv[0])
sys.exit()
image_data_file = open (sys.argv[1], 'rb')
image_data = bytearray(9612)
image_data_file.readinto(image_data)
image_data_file.close()
image = Image.frombytes ('L', (96,96), bytes(image_data) ,'raw')
image.show()
--
Uli Raich - 2022-01-31
Comments