Raspberry Pi Security Camera Gmail alert with captured .JPEG image.

 

Before completing the project I wanted to give you a taste of what your building. Here is a quick You-tube video that will show you the final project in action.

To fabricate my Pi Security Camera  project, I  used these two resources. The first resource I used was from the Smart-Security-Camera website. The second resource was from web-author Adrian Rosebrock at PyImageSearch with an excellent guide for help installing OpenCV on the Raspberry Pi.

Install OpenCV first.  ( I highly recommend the PyImageSearch link)

Install the Smart-Security-Camera source code second.
You’ll need to access the github repository from within your ‘Smart-Security-Camera’ project folder. (NOTE: I placed my project folder as a sub-directory under the ‘opencv’ directory ) For instance, if I named my project folder, ‘myPiCam’ , then I would run the command $ cd myPiCam.
To access github, you need to have  git‘ installed.

Here is a link to the actual Git website. ( This is a great resource. )

To install ‘git’ on the Raspberry Pi, write the following:
1. ~$ sudo apt-get install git -y
Then use ‘git’ to  install the Smart-Security-Camera source code from github:

I placed my ‘Smart-Security-Project’ project folder in the opencv directory(folder). It is here that I performed the below ‘git clone’ of the Smart-Security-Camera source code. There are other ways to build out the filesystem structure for this project. I decided this path. It worked first time out of the box for me.

2. ‘cd‘ over to the ‘opencv‘ directory and enter:
(remove the square brackets if using the ‘super- user-do command, ‘sudo‘.)
~$ [sudo] git clone https://github.com/HackerHouseYT/Smart-Security-Camera
By placing the SSC (Smart-Security-Camera) in the opencv directory it will have access to all opencv code resources.
( The [sudo] in brackets means: only if you need to use super-user permissions – use it. I would suggest that if you get Error messages when trying to access the git respository, use the ‘sudo’  in front.  Also, check for spelling errors. Make sure you typed: ‘https’ and not ‘http’.)

You’ll need to set permissions using ‘chmod‘ on the files  you download to have CRUD (create/read/update/delete) access capabilities:
‘cd’ into the specific file below and then enter: ~$ chmod  u+x  <filename-here>.py for each file listed below.

1. camera.py
2. main.py
3. email.py

Be sure to run this command line statement:  ~$ sudo install -r requirements.txt

The requirements.txt file houses the names of project dependencies in the project:
1. imutils
2. flask
3. picamera[array]
4. Flask-BasicAuth==0.2.0

My working environment: The Pi Hardware and a ‘Bit’ more. (no fun intended!)

The Pi : I used a RPi/ B+ with a micro-fan and sinks to keep the core temp at 78.4(Celsius) or 173.12 (Fahrenheit) while running the pi’s camera, program, terminals and the browser. (I have backups!)  While the Pi is not running programs, the pi core temp is 47.8c or 118.03f.

I use a 4 port POWERED USB hub to plug my keyboard, mouse, micro-fan, and the Raspberry Pi into.  It works awesome!

Finding the current CPU frequency along with the minimum and maximum frequencies use:

  1.  Current CPU speed:  ~$ cat    /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
  2.   Current set MIN:      ~$ cat   /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
  3.   Current set MAX:     ~$ cat     /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq

My scaling_cur_freq is 1200000 in kilohertz during operations.  Or, 1200 MHz.
The temp is 67.1(c) with a fan.  (The microfan is powered directly from my power hub)

Find other clock speeds on the Pi:

~$ vcgencmd measure_clock THE-CLOCK-NAME
For the THE-CLOCK-NAME insert any of the following:  h264, isp, v3d, uart, pwm, emmc, pixel, vec, hdmi, or dpi

My RPi/B+ CPU info:  ~$ cat /proc/cpuinfo
ARMv7 Processor rev 4 (v71)                                   //  ARMv7 architecture
BCM2835                                                                    //   Broadcom

CPU 1.4 GHz 64/32-bit quad-core ARM Cortex-A53
Memory 1 GB LPDDR2 RAM at 900 MHz
Storage MicroSDHC slot
Graphics Broadcom VideoCore IV 300 MHz/400 MHz

Other useful commands:
1. ~$ cat /proc/meminfo
2. ~$ cat /proc/partitions
3. ~$ cat /proc/version

The Pi also has a special command available which displays data that is specific to the Raspberry Pi board. The ‘vcgencmd‘ is a tool that can access a lot of the Raspberry Pi’s specific information including clock frequencies, various voltages, the CPU temperature, and which hardware codecs are enabled.

If your pi core temp rises above at 80 degrees celsius you’ll see a little ‘red’ thermometer appear in the top right of the pi display. Get a fan!

What is the core temp? Here’s how to check it.
~$  /opt/vc/bin/vcgencmd measure_temp

Use the 'vcgencmd' system command in other ways, for instance:
'vcgencmd measure_volts' to find the internal voltages for core, sdram_c, sdram_i, and sdram_p.

If you need to obtain information on the hardware codecs you have enabled, use the 'vcgencmd codec_enabled ' where ‘CODEC’ is replaced by one of the following; H264, MPG2, WVC1, MPG4, MJPG, or WMV9

Memory

The memory split between my arm and gpu:  ~$ vcgencmd get_mem arm && vcgencmd get_mem gpu
arm=896M
gpu=128M

Pi Camera:

If the camera frame(image) is flipped then open the camera.py file with your editor and in the function ‘flip_if_needed(self, frame)’ , on the code line statement,  ‘return np.flip(frame, 0)’ ,
change the ‘0’ [that’s a integer zero] to a ‘1’ [that’s a non-zero, integer 1].
( These are Boolean values.  Zero=False.(off)  One=True.(on)  All ‘non-zero’ integers are True. )

Project code needed:

Raspbian Stretch OS Image.   See this blog post.
Opencv.
Smart-Security-Camera at: ~$ sudo git clone https://github.com/HackerShackOfficial/Smart-Security-Camera.git

1. Pip  (Only if needed!  Already built-in on versions of Python >= 2.7.9 and > 3.4) or virtualenv.
a.  Just be sure to upgrade after pip install.
b.  Check to see if pip is installed on your system:
For Linux users: ~$ pip --version

1. For Linux or macOS users:
~$ pip install -U pip
2. For Windows users:
~$ python -m pip install -U pip

You can run ~$ pip list which shows a list of current available pip installations on your Pi.

Running the project in a virtual environment.
(It’s a good idea to do this !)

Python Virtual Environment: creates an ‘isolated’ virtual environment to place your code. It’s like building your application in one room of a house and keeping all your other rooms (primary code) protected.

You may also want to check and be sure that you have up-to-date copies of setuptools and wheel.
~$ python -m pip install --upgrade pip setuptools wheel

There a two common tools which to choose from:

1. The ‘venv‘ module: (I use this on Linux machine. Quick Read here.)
Create the Linux virtual environment(ve):   ~$ python3 -m venv myVirtEnv
Now, activate the ve :   ~$ source  myVirtEnv/bin/activate
You should see something like this: (myVirtEnv) . It will be next to the pi@raspberrypi:~$
* if not, go back and check that the code was entered correctly.
* some installations require – ~$ source   myVirtEnv/Scripts/activate
To deactivate the virtual environment, write:

2. The ‘virtualenv’ module:

Linux users:   $ sudo apt-get install python virtualenv

Mac OS users:  $ sudo easy_install virtualenv

At the command line under in the ~$/home/pi  directory you need to first enter:
1.  ~$ source     ~/.profile
2.  ~$ workon  myVirtEnv
// replace ‘myVirtEnv’ with whatever you named your virtual environment.

Email(Gmail) Settings and Issues:

Google needs this approval from your gmail account

Go here: ~$  https://myaccount.google.com/lesssecureapps

Troubleshooting

1. If you get the error “ImportError: No module named imutils.video.pivideostream”
You need to: ~$ sudo  git clone https://github.com/jrosebr1/imutils.git
This  is from: https://www.pyimagesearch.com/

From Brock:

my guess is that you may have accidentally installed and uninstalled (perhaps multiple times over) the imutils library from your system Python install and your Python virtual environment. Here’s the fool-proof method:

$ workon cv
$ pip install imutils
$ python
>>> import imutils

2. If you get the error: ImportError: No module named flask_basicauth

~$ sudo pip install Flask-BasicAuth
Code resource: https://github.com/jpvanhal/flask-basicauth
What is Basic Auth?  Answer: https://en.wikipedia.org/wiki/Basic_access_authentication

AFTER performing troubleshooting (1) and (2) from above :
1. The Pi Camera light came on indicating camera on!.
2. In Chrome, an alert window came up asking for my username and password.
I wasn’t sure at first what this was related to. I opened the main.py file and there was the answer.

Inside main.py file locate the Flask instance variable (app) of the Python web microframework called Flask.
1.  On the code line:  app.config[‘BASIC_AUTH_USERNAME’]  = ‘ ‘
2. On the code line:  app.config[‘BASIC_AUTH_PASSWORD’]  = ‘ ‘

I entered in a username and password of my own for the above entries and saved them. These values will be needed for the pop-up window that will appear (this window appears right after you run: ~$ python main.py , to start the application) for user authentication to the program.

You write the IP address of your raspberry pi into the URL of your browser; example: 192.168.0.289:5000 . (The Flask default port is 5000.) To find the raspberry pi IP address run[ ~$ ifconfig  ] from the Pi’s terminal. Hit the ‘enter’ key after inserting the IP of the PI in the URL.  A Flask Authentication form will open asking for the username and password you set in the main.py file.  If all goes well, in your browser window you will be viewing a Flask ‘index.html’ view with a streaming pi camera video.

Make sure you have good ambient light for the Opencv models to work. My gmail camera capture did not work at first on the ‘full-body-recognition’ opencv model. I changed it to the ‘facial-recognition’ opencv model and it worked. For the default, I believe the camera is set at 600 for time. This can be changed in the code.

 

Keep learning.

If you want to make suggestions or comments, all are welcome.
Tim