Showing posts with label raspberrypi. Show all posts
Showing posts with label raspberrypi. Show all posts

February 19, 2017

Install Python 3 and OpenCV on Windows

In this post I will describe the easiest way of installing Python 3 and OpenCV library on Windows for face detection and recognition.

Install Python 3

Go to the Python download page and download the latest version of Python 3 for Windows. This example has been tested with Python 3.6.

Launch the installer and ensure the 'Add Python to PATH' checkbox is selected.
Wait for the installation to complete, open a Command Prompt and type 'python'. You should see something like this.

C:\Python>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Type 'quit()' to exit the python shell.


Install OpenCV library

If you just need the face detection (no recognition) all you need is the basic OpenCV library. It can be easily installed with no hassle with pip command.

pip install opencv-python


Install OpenCV library with contrib module

To be able to use the face recognition algorithms, you will need the 'contrib' module that is not included in the standard OpenCV build. Many people suggest to build the library manually to solve the problem but there is a simpler solution documented in this post.

Download the appropriate opencv+contrib .whl file from this page. The file to download should be: opencv_python-3.2.0+contrib-cpXX-cp36m-winYYY.whl. Where XX is your Python version and YYY is your Windows 32/64 bit flavor.
For example, assuming you have Python 3.6 running on Windows 64 bit you must download: opencv_python-3.2.0+contrib-cp36-cp36m-win_amd64.whl

Now open a Command Prompt with admin rights and type the following command to install:

pip install opencv_python-3.2.0+contrib-cp36-cp36m-win_amd64.whl


Test face detection

You can now move on to the face detection example.

August 17, 2016

Automatic orientation of Sense HAT display

In this post I will show you how to automatically detect the position of the Raspberry PI using the Sense HAT accelerometer sensor and rotate the display accordingly.

The auto_rotate_display function in the script below is doing all the logic. It's reading sensors data to detect the orientation and rotating the Sense HAT display by calling the set_rotation function.

import time
from sense_hat import SenseHat

MSG_COLOR = [200,0,160]
BKG_COLOR = [0,0,0]
SCROLL_SPEED = 0.06
MESSAGE = "Can't loose my head"


def auto_rotate_display():
  # read sensors data to detect orientation
  x = round(sense.get_accelerometer_raw()['x'], 0)
  y = round(sense.get_accelerometer_raw()['y'], 0)

  rot = 0
  if x == -1:
    rot=90
  elif y == -1:
    rot=180
  elif x == 1:
    rot=270

  # rotate the display according to the orientation
  print ("Current orientation x=%s y=%s  rotating display by %s degrees" % (x, y, rot))
  sense.set_rotation(rot)


sense = SenseHat()

while True:
  auto_rotate_display()
  sense.show_message(MESSAGE, scroll_speed=SCROLL_SPEED, text_colour=MSG_COLOR, back_colour=BKG_COLOR)
  time.sleep(1)


Accurate temperature reading from Raspberry PI Sense HAT

When I first received the Sense HAT for my Raspberry PI 3 I decided to start building a nice weather station using the embedded environmental sensors and RGB LED matrix.
From a quick experiment it turns out that the temperature readings are not accurate. The problem is caused by thermal conduction from the Pi CPU to the humidity and pressure sensors on the Sense HAT.
I have experimented few algorithms and I have found the following formula to be the most reliable. It basically compensate the temperature reading (t) with the CPU temperature (tCpu).
tCorr = t - ((tCpu-t)/1.5)

Here is the full script to print each 5 seconds the environmental data including the corrected temperature.

import os
import time
from sense_hat import SenseHat

def get_cpu_temp():
  res = os.popen("vcgencmd measure_temp").readline()
  t = float(res.replace("temp=","").replace("'C\n",""))
  return(t)


sense = SenseHat()

while True:
  t = sense.get_temperature_from_humidity()
  t_cpu = get_cpu_temp()
  h = sense.get_humidity()
  p = sense.get_pressure()

  # calculates the real temperature compesating CPU heating
  t_corr = t - ((t_cpu-t)/1.5)
  
  print("t=%.1f  t_cpu=%.1f  t_corr=%.1f  h=%d  p=%d" % (t, t_cpu, t_corr, round(h), round(p)))
  
  time.sleep(5)

Running this script I have noticed that the CPU temperature reading is not very stable making the corrected temperature a little bit unstable. To fix this issue I have decided to apply a moving average to the temperature reading.
As a further improvement I'm also the temperature both from the humidity and pressure sensors and calculating the average.

import os
import time
from sense_hat import SenseHat

# get CPU temperature
def get_cpu_temp():
  res = os.popen("vcgencmd measure_temp").readline()
  t = float(res.replace("temp=","").replace("'C\n",""))
  return(t)

# use moving average to smooth readings
def get_smooth(x):
  if not hasattr(get_smooth, "t"):
    get_smooth.t = [x,x,x]
  get_smooth.t[2] = get_smooth.t[1]
  get_smooth.t[1] = get_smooth.t[0]
  get_smooth.t[0] = x
  xs = (get_smooth.t[0]+get_smooth.t[1]+get_smooth.t[2])/3
  return(xs)


sense = SenseHat()

while True:
  t1 = sense.get_temperature_from_humidity()
  t2 = sense.get_temperature_from_pressure()
  t_cpu = get_cpu_temp()
  h = sense.get_humidity()
  p = sense.get_pressure()

  # calculates the real temperature compesating CPU heating
  t = (t1+t2)/2
  t_corr = t - ((t_cpu-t)/1.5)
  t_corr = get_smooth(t_corr)
  
  print("t1=%.1f  t2=%.1f  t_cpu=%.1f  t_corr=%.1f  h=%d  p=%d" % (t1, t2, t_cpu, t_corr, round(h), round(p)))
  
  time.sleep(5)

Display two digits numbers on the Raspberry PI Sense HAT

Here is a small Python script to display two digits numbers on the Raspberry PI Sense HAT led matrix.

The script is very useful if you want to display humidity and temperature without scrolling.


The show_number function accepts the number to be displayed and RGB values of the color to be used.
Here is the full script.

from sense_hat import SenseHat
import time

OFFSET_LEFT = 1
OFFSET_TOP = 2

NUMS =[1,1,1,1,0,1,1,0,1,1,0,1,1,1,1,  # 0
       0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,  # 1
       1,1,1,0,0,1,0,1,0,1,0,0,1,1,1,  # 2
       1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,  # 3
       1,0,0,1,0,1,1,1,1,0,0,1,0,0,1,  # 4
       1,1,1,1,0,0,1,1,1,0,0,1,1,1,1,  # 5
       1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,  # 6
       1,1,1,0,0,1,0,1,0,1,0,0,1,0,0,  # 7
       1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,  # 8
       1,1,1,1,0,1,1,1,1,0,0,1,0,0,1]  # 9

# Displays a single digit (0-9)
def show_digit(val, xd, yd, r, g, b):
  offset = val * 15
  for p in range(offset, offset + 15):
    xt = p % 3
    yt = (p-offset) // 3
    sense.set_pixel(xt+xd, yt+yd, r*NUMS[p], g*NUMS[p], b*NUMS[p])

# Displays a two-digits positive number (0-99)
def show_number(val, r, g, b):
  abs_val = abs(val)
  tens = abs_val // 10
  units = abs_val % 10
  if (abs_val > 9): show_digit(tens, OFFSET_LEFT, OFFSET_TOP, r, g, b)
  show_digit(units, OFFSET_LEFT+4, OFFSET_TOP, r, g, b)


################################################################################
# MAIN

sense = SenseHat()
sense.clear()

for i in range(0, 100):
  show_number(i, 200, 0, 60)
  time.sleep(0.2)

sense.clear()

June 3, 2016

Raspberry PI - Arduino communication over USB serial

The Raspberry Pi is a fine little computer board with a lot of features and very good connectivity especially on version 3 with the integrated WiFi.
However, when dealing with I/O and sensors has a lot of limitations and is lacking the reliability of Arduino board and availability of libraries to interface it with any kind of device or sensor.

That's why I have decided to develop my own approach to integrate the two famous boards to fulfill my own set of requirements:
  • Use Raspberry Pi as a master device to develop the main application and connect to the Internet.
  • Use Java on the Raspberry Pi. This may sound a strange choice but I'm planning to use it to build an OSGi application for IoT.
  • Use Arduino board as a slave device to interact with several sensors.
  • Use a simple USB cable to power the Arduino board and exchange data.


This tutorial explains how to connect Arduino board to a Raspberry PI using a simple USB cable. This will power the Arduino and provide a serial communication between the two little boards.


Arduino


First step is to load and test the Arduino sketch to allow to retrieve the current milliseconds and toggle the on-board LED as described in this article.



Raspberry PI 

First step is to free the serial interface on the RasPI disabling shell and kernel messages via UART.
Run raspi-conf utility from the command prompt:

$ sudo raspi-config

Identify the Serial option and disable it. If is in the advanced menu in recent versions of Raspbian (9, A8).



Enter the following command to reboot your Raspberry Pi board:

$ sudo reboot


Install RxTx library

On the Raspberry PI side we will use the RxTx library. Install it with the following command:

sudo apt-get install librxtx-java

You can see that the native libraries are installed into the /usr/lib/jni directory and the java client is installed in /usr/share/java.

ls -la /usr/lib/jni/librxtx*
ls -la /usr/share/java/RXTX*


Java app

Import this Java project into Eclipse and build it. It is a slightly improved version of the official RxTx sample.
Now copy the three generated class files to your Raspberry Pi device into the /home/pi directory and run the following command:

java -Djava.library.path=/usr/lib/jni -cp /usr/share/java/RXTXcomm.jar:. ArduRasPi /dev/ttyUSB0 9600

This will run the ArduRasPi program connecting to Arduino on the /dev/ttyUSB0 port and 9600 baud.

You should now be able to issue commands to Arduino and receive the correct answer.





References

Arduino Vs. Raspberry Pi: Which Is The Right DIY Platform For You?
Control an Arduino from Java
Raspberry PI reads and writes data from Arduino, in Java
Arduino and Java