From charlesreid1

 
(26 intermediate revisions by the same user not shown)
Line 39: Line 39:
apt-get install -y vim aptitude
apt-get install -y vim aptitude
apt-get install -y python build-essential
apt-get install -y python build-essential
apt-get install -y python-pip  
apt-get install -y python-pip
pip install picamera
</pre>
</pre>


==Testing Picamera==
This doesn't do much - most everything here is already installed.


Open python and try to import the picamera library:
Now install the pi camera library (should also be already installed):


<pre>
<pre>
>>> import picamera
pip install picamera
</pre>
</pre>


=Not Working Outline: Kali ARM Image=
Now one last thing to do before testing the camera is to enable the boot flag that lets you use the camera (disabled by default).


For this project, I was replicating the Raspberry Pi timelapse setup from the [[RaspberryPi/Timelapse]] page.
Run <code>raspi-config</code> and pick the camera menu item (number 6). Choose to enable the camera. Save changes, and you'll be asked to reboot the Pi. Reboot the Pi. Now you can try out the camera.


==Hardware==
==Testing Picamera==


* Raspberry Pi
Open python and try to import the picamera library:
* Pi camera
* Camera case
* Network cable
* Power cable


==Setting up the Pi==
<pre>
import picamera
</pre>


To begin with, I installed a fresh Kali Linux arm image. I wanted to make sure I had installed the operating system correctly and that I could reach the Pi just fine. I connected the Pi directly to my laptop using a crossover cable. After connecting the two, I restarted both machines. They automatically picked link-local addresses at <code>169.254.X.Y</code>, which I was able to use to SSH directly into the Pi.
Now try capturing your first photo:


This confirmed that I had everything working ok on the Pi.
<pre>
import picamera
camera = picamera.PiCamera()
camera.capture('helloworld.jpg')
</pre>


==Connecting to the Pi==
Woo hoo! Works perfect!


The next step was to connect to the Pi over a network, so that the Pi would be able to download and install any necessary libraries. I started by modifying cmdline.txt on the SD card to manually set the Pi's IP address to 192.168.0.111. I then plugged the Pi into the network router, and was able to SSH into the machine at 192.168.0.111.
===Problems out of nowhere===


==Installing Libraries==
Suddenly, out of nowhere, just when I had finished rigging the wireless and the zip ties and the camera box, everything failed. The camera would no longer respond. Hardware connection problem. Tear it all apart.


Once on the Pi, I needed to install some libraries. From my previous adventure at [[RaspberryPi/Timelapse]] I knew I needed a few libraries:
===Resolution of problems out of nowhere===
* python-picamera
* python-picamera-docs


===Can't use aptitude===
The problems that came out of nowhere turned out to be connection problems with the flat yellow plate on the front of the camera face. I had to re-seat that plate snugly. After that, the camera worked fine.


These packages were not in the aptitude repositories for this OS:
===Testing with crossover cable===


<pre>
Connected Pi and laptop with crossover cable, set Pi IP to 169.254.111.111, tested local ssh to Pi - PASSED
# apt-get install -y python-picamera python-picamera-docs
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python-picamera
E: Unable to locate package python-picamera-docs
</pre>


But that's ok, because we can use pip instead!
Connected Pi and laptop with crossover cable, same IP, tested capturing photo from Pi camera - PASSED


If this is a brand new Pi, you'll need to install some tools (not all necessary, but you'll need them sooner or later.) Start with screen, so you can run the installation process in the background:
===Testing with Wireless===


<pre>
Wireless, 192.168.0.X, test whether we can connect to the wireless - PASSED
apt-get install -y screen
</pre>


Then install some utilities onto the Pi:
Wireless, 192.168.0.X, test whether we can auto-MAGIC-ally connect to the wireless on boot - PASSED


<pre>
Wireless, 192.168.0.X, test whether we can plug in on other side of house, SSH in, start/finish photo capture, run HTTP server - PASSED
apt-get install -y vim aptitude
</pre>


Next, install Python and tools for building stuff:
Wireless, 192.168.0.X, test whether we can capture photos, with everything taped into position - PASSED


<pre>
Wireless, 192.168.0.X, test whether we can run a time lapse test - PASSED
apt-get install -y python build-essential
</pre>


Last, install pip:
==Final Arrangement for Timelapse 2==


<pre>
The final arrangement of parts for the first timelapse was hacky and definitely would not have held together more than 24 hours. Here are photos of the Pi on the balcony. I used some packing tape (should have used duct tape, but I didn't have a fresh roll) to create a "sheet". I wrapped that around the Pi, the cabling for the camera, and the balcony rail.
apt-get install -y python-pip
</pre>


===Using pip===
[[Image:PiBalcony3.jpg|500px]]


Now we should have pip, which will enable us install the Pi camera API:
[[Image:PiBalcony2.jpg|500px]]


<pre>
[[Image:PiBalcony1.jpg|500px]]
pip install picamera
</pre>
 
(I found this by googling the term "picamera" - one of the top results was the page for this library in the pip repository.)


<pre>
This whole arrangement was made super awkward by the camera cable. It was wide, inflexible, hard to twist (and had to be twisted at least once given the way it came out of the case), but the real challenge was that if it was tugged too hard, the yellow plate on the front of the camera would pop off, and the entire assembly would have to be un-taped and disassembled to push the plate back onto the board, reassembled, re-taped, etc.
# pip install picamera
Collecting picamera
  Downloading picamera-1.12.tar.gz (109kB)
    100% |████████████████████████████████| 112kB 680kB/s
Building wheels for collected packages: picamera
  Running setup.py bdist_wheel for picamera ... done
  Stored in directory: /root/.cache/pip/wheels/ec/7e/4f/ff55e2d9ee8377f0452e7f03822c3ee914c8c75a9cb9f57642
Successfully built picamera
Installing collected packages: picamera
Successfully installed picamera-1.12
</pre>


===Errors with libmmal===
[[Image:PiDesk1.jpg|500px]]


I saw errors coming from missing libmmal files when I tried to import the picamera library. The following Python code imports the picamera library:
=Scripting=


<pre>
==Taking photos==
import picamera
</pre>


This failed with the following error message:
To script taking a picture, use the following code, which loops forever, taking photos and marking them with timestamps:


<pre>
<pre>
Traceback (most recent call last):
# pic.py
  File "lapse.py", line 3, in <module>
    import picamera
  File "/usr/local/lib/python2.7/dist-packages/picamera/__init__.py", line 72, in <module>
    from picamera.exc import (
  File "/usr/local/lib/python2.7/dist-packages/picamera/exc.py", line 41, in <module>
    import picamera.mmal as mmal
  File "/usr/local/lib/python2.7/dist-packages/picamera/mmal.py", line 47, in <module>
    _lib = ct.CDLL('libmmal.so')
  File "/usr/lib/python2.7/ctypes/__init__.py", line 362, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libmmal.so: cannot open shared object file: No such file or directory
</pre>


After some googling, I discovered that <code>libmmal.so</code> is supposed to reside in <code>/opt/vc</code>, which is where the Raspberry Pi stores its VideoCore API files. More information here: [http://elinux.org/Raspberry_Pi_VideoCore_APIs]
import picamera
from datetime import datetime
import time


The source code for building this library is available on Github here: https://github.com/raspberrypi/firmware/tree/master/opt/vc
camera = picamera.PiCamera()


Precompiled binaries of these libraries are available here: https://github.com/raspberrypi/firmware/tree/master/hardfp/opt/vc
while True:


This repository looked promising, until it ended up being so large it ate up all the available space on the Pi: https://github.com/raspberrypi/firmware.git
    prefix = datetime.strftime(datetime.now(),"photo_%Y%m%d-%H%M%S")
    filename = prefix+".jpg"


It seems to have precompiled versions of libraries and binaries and things, available to port over to the Pi. Dunno.
    camera.capture(filename)
    print "Saving photo to %s"%(filename)


===Alternate route: install raspi-config===
    time.sleep(2)


Even though it is intended for Raspbian, we can try using the raspi-config utility, which is what Raspbian users use to enable the camera module on their Raspberry Pi. It is not available on ours because we installed our own operating system.
</pre>


The .deb file for the raspi-config utility is difficult to find, but the link to all the release versions listed by date going back about 5 years are available online here: http://archive.raspberrypi.org/debian/pool/main/r/raspi-config/
==Processing photos==


From there, you copy the link to the .deb installer file, and download it to the Pi:
While the full details are over at the [[Timelapse/Processing]] page, here's a quick taste of what collating a bunch of images together looks like.  


<pre>
Assuming you have all the images you want in a pile in one directory, we want to rename them in order so that they have the same name as before, but with the suffix _0001,0002,0003, etc.
wget http://archive.raspberrypi.org/debian/pool/main/r/raspi-config/raspi-config_20160527_all.deb
</pre>


To install the contents of a deb file, use the <code>dpkg</code> utility with the <code>-i</code> flag, for install.
=Successful Capture=


These packages have unmet dependencies:
Notes on what the successful capture looks like, and links to pages/notes on processing timelapse photos.


<pre>
=Notes=
# apt-get -f install libasound2 lua5.1 alsa-utils
Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
alsa-utils : Depends: libfftw3-double3 but it is not going to be installed
libasound2 : Depends: libasound2-data (>= 1.1.1-2) but 1.1.0-1 is to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
</pre>
 
This whole deal is worded in a very confusing way. If you try running apt-get -f install (something), your command will fail, with everything depending on everything else and nothing actually installing and nothing working. But this is the wrong way to do it! They want you to run apt-get -f install with NO arguments. This installs all the necessary packages.
 
<pre>
# apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following additional packages will be installed:
  alsa-utils libasound2 libasound2-data libfftw3-double3 lua5.1
Suggested packages:
  libfftw3-bin libfftw3-dev
The following NEW packages will be installed:
  alsa-utils libfftw3-double3 lua5.1
The following packages will be upgraded:
  libasound2 libasound2-data
2 upgraded, 3 newly installed, 0 to remove and 488 not upgraded.
1 not fully installed or removed.
Need to get 2,229 kB of archives.
After this operation, 3,360 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://kali.localmsp.org/kali kali-rolling/main armhf lua5.1 armhf 5.1.5-8 [75.7 kB]
Get:2 http://kali.localmsp.org/kali kali-rolling/main armhf libasound2 armhf 1.1.1-2 [472 kB]
Get:3 http://kali.localmsp.org/kali kali-rolling/main armhf libasound2-data all 1.1.1-2 [198 kB]
Get:4 http://kali.localmsp.org/kali kali-rolling/main armhf libfftw3-double3 armhf 3.3.4-2+b1 [437 kB]
Get:5 http://kali.localmsp.org/kali kali-rolling/main armhf alsa-utils armhf 1.1.1-1 [1,047 kB]
Fetched 2,229 kB in 3s (723 kB/s)
Selecting previously unselected package lua5.1.
(Reading database ... 117736 files and directories currently installed.)
Preparing to unpack .../lua5.1_5.1.5-8_armhf.deb ...
Unpacking lua5.1 (5.1.5-8) ...
Preparing to unpack .../libasound2_1.1.1-2_armhf.deb ...
Unpacking libasound2:armhf (1.1.1-2) over (1.1.0-1) ...
Preparing to unpack .../libasound2-data_1.1.1-2_all.deb ...
Unpacking libasound2-data (1.1.1-2) over (1.1.0-1) ...
Selecting previously unselected package libfftw3-double3:armhf.
Preparing to unpack .../libfftw3-double3_3.3.4-2+b1_armhf.deb ...
Unpacking libfftw3-double3:armhf (3.3.4-2+b1) ...
Selecting previously unselected package alsa-utils.
Preparing to unpack .../alsa-utils_1.1.1-1_armhf.deb ...
Unpacking alsa-utils (1.1.1-1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for libc-bin (2.23-2) ...
Processing triggers for systemd (229-2) ...
Setting up lua5.1 (5.1.5-8) ...
update-alternatives: using /usr/bin/lua5.1 to provide /usr/bin/lua (lua-interpreter) in auto mode
update-alternatives: using /usr/bin/luac5.1 to provide /usr/bin/luac (lua-compiler) in auto mode
Setting up libasound2-data (1.1.1-2) ...
Setting up libasound2:armhf (1.1.1-2) ...
Setting up libfftw3-double3:armhf (3.3.4-2+b1) ...
Setting up alsa-utils (1.1.1-1) ...
Setting up raspi-config (20160527) ...
update-rc.d: We have no instructions for the raspi-config init script.
update-rc.d: It looks like a non-network service, we enable it.
Processing triggers for libc-bin (2.23-2) ...
Processing triggers for systemd (229-2) ...
</pre>


Now we can install the raspi-config utility:
==Timelapse 2 Notes==


<pre>
Running my first timelapse, I didn't bother doing any calculations to see how long I could run based on available disk space - I just ran at a frequency of 1 photo per 2 seconds and let it run all night. It maxed out the SD card after about 6 hours.
# dpkg -i raspi-config_20160527_all.deb
(Reading database ... 117835 files and directories currently installed.)
Preparing to unpack raspi-config_20160527_all.deb ...
Unpacking raspi-config (20160527) over (20160527) ...
Setting up raspi-config (20160527) ...
Processing triggers for systemd (229-2) ...
</pre>


=Scripting=
Results and notes from this script/timelapse:
* Dislike case of Raspberry Pi 3
* Tape job was very perilously perched after a night of sitting - need mechanical security, not just tape and balance (ZIP TIES and/or LASHING)


To script taking a picture, use the following code, which loops forever, taking photos and marking them with timestamps:
What to keep the same:
* Camera worked ok
* Mini SD card worked ok
* Kali operating system worked ok, connected to wireless ok, hosted files ok, transferred data ok
* Power cable situation worked ok


<pre>
What to change for next timelapse:
# pic.py
* More HD space
* No red light
* Case for Pi - loose top makes it awkward to do anything with it, forced camera angle is awkward too
* Need a swiveling plate or platform or boom
* Up/down angle of camera was difficult to control


import picamera
For Next Design:
from datetime import datetime
* Make it reusable across other types of camera and sensor platforms
import time
* What other kinds of cameras might we be using
* What other kinds of control systems
* Generic mounting brackets, mechanical connections, on a platform would be good. Screws/bolts/etc.
* Way of positioning platform using railing.


camera = picamera.PiCamera()
What to change eventually:
* Adjustable frequency and more sophistication in script
* Motor control of plate or device to control angles and motion
* Image processing (vivid, contrast, hue, blur, tilt shift)


while True:
==Processing timelapse photos==


    prefix = datetime.strftime(datetime.now(),"photo_%Y%m%d-%H%M%S")
For notes on processing timelapse photos, see [[Timelapse/Processing]] page.
    filename = prefix+".jpg"


    camera.capture(filename)
Notes on time-lapse photography in general at the [[Timelapse]] page.
    print "Saving photo to %s"%(filename)


    time.sleep(2)
==Next timelapse==


</pre>
For planning notes for Timelapse 3, see: [[RaspberryPi/Timelapse3]]


=Flags=
=Flags=
{{TimelapseFlag}}


{{PiFlag}}
{{PiFlag}}

Latest revision as of 16:55, 14 August 2016

Outline: Raspbian

The issue with using Kali was, the use of the camera required onboard video libraries that did not come bundled with Kali. These were also not in the aptitude repositories, and I had no luck installing them. So for the camera project I decided to go with Raspbian and not sink any more time into getting Kali to work with camera hardware.

pi/raspberry

Hardware

  • Raspberry Pi
  • Pi camera
  • Camera case
  • Network cable
  • Power cable

Setting up the Pi

To begin with, I installed a fresh Kali Linux arm image. I wanted to make sure I had installed the operating system correctly and that I could reach the Pi just fine. I connected the Pi directly to my laptop using a crossover cable. After connecting the two, I restarted both machines. They automatically picked link-local addresses at 169.254.X.Y, which I was able to use to SSH directly into the Pi.

This confirmed that I had everything working ok on the Pi.

Connecting to the Pi

I connected the Pi to a router, set a fixed IP address on the boot line of cmdline.txt on the memory card's boot sector, and plugged it into my home router. I then fired up the Pi.

SSH to 192.168.0.111 confirmed I was able to remotely reach the Pi. The Pi was also able to successfully reach the gateway and the outside internet.

Installing stuff

Get busy installing stuff:

apt-get install -y screen
screen

In the new screen window:

apt-get install -y vim aptitude
apt-get install -y python build-essential
apt-get install -y python-pip

This doesn't do much - most everything here is already installed.

Now install the pi camera library (should also be already installed):

pip install picamera

Now one last thing to do before testing the camera is to enable the boot flag that lets you use the camera (disabled by default).

Run raspi-config and pick the camera menu item (number 6). Choose to enable the camera. Save changes, and you'll be asked to reboot the Pi. Reboot the Pi. Now you can try out the camera.

Testing Picamera

Open python and try to import the picamera library:

import picamera

Now try capturing your first photo:

import picamera
camera = picamera.PiCamera()
camera.capture('helloworld.jpg')

Woo hoo! Works perfect!

Problems out of nowhere

Suddenly, out of nowhere, just when I had finished rigging the wireless and the zip ties and the camera box, everything failed. The camera would no longer respond. Hardware connection problem. Tear it all apart.

Resolution of problems out of nowhere

The problems that came out of nowhere turned out to be connection problems with the flat yellow plate on the front of the camera face. I had to re-seat that plate snugly. After that, the camera worked fine.

Testing with crossover cable

Connected Pi and laptop with crossover cable, set Pi IP to 169.254.111.111, tested local ssh to Pi - PASSED

Connected Pi and laptop with crossover cable, same IP, tested capturing photo from Pi camera - PASSED

Testing with Wireless

Wireless, 192.168.0.X, test whether we can connect to the wireless - PASSED

Wireless, 192.168.0.X, test whether we can auto-MAGIC-ally connect to the wireless on boot - PASSED

Wireless, 192.168.0.X, test whether we can plug in on other side of house, SSH in, start/finish photo capture, run HTTP server - PASSED

Wireless, 192.168.0.X, test whether we can capture photos, with everything taped into position - PASSED

Wireless, 192.168.0.X, test whether we can run a time lapse test - PASSED

Final Arrangement for Timelapse 2

The final arrangement of parts for the first timelapse was hacky and definitely would not have held together more than 24 hours. Here are photos of the Pi on the balcony. I used some packing tape (should have used duct tape, but I didn't have a fresh roll) to create a "sheet". I wrapped that around the Pi, the cabling for the camera, and the balcony rail.

PiBalcony3.jpg

PiBalcony2.jpg

PiBalcony1.jpg

This whole arrangement was made super awkward by the camera cable. It was wide, inflexible, hard to twist (and had to be twisted at least once given the way it came out of the case), but the real challenge was that if it was tugged too hard, the yellow plate on the front of the camera would pop off, and the entire assembly would have to be un-taped and disassembled to push the plate back onto the board, reassembled, re-taped, etc.

PiDesk1.jpg

Scripting

Taking photos

To script taking a picture, use the following code, which loops forever, taking photos and marking them with timestamps:

# pic.py

import picamera
from datetime import datetime
import time

camera = picamera.PiCamera()

while True:

    prefix = datetime.strftime(datetime.now(),"photo_%Y%m%d-%H%M%S")
    filename = prefix+".jpg"

    camera.capture(filename)
    print "Saving photo to %s"%(filename)

    time.sleep(2)

Processing photos

While the full details are over at the Timelapse/Processing page, here's a quick taste of what collating a bunch of images together looks like.

Assuming you have all the images you want in a pile in one directory, we want to rename them in order so that they have the same name as before, but with the suffix _0001,0002,0003, etc.

Successful Capture

Notes on what the successful capture looks like, and links to pages/notes on processing timelapse photos.

Notes

Timelapse 2 Notes

Running my first timelapse, I didn't bother doing any calculations to see how long I could run based on available disk space - I just ran at a frequency of 1 photo per 2 seconds and let it run all night. It maxed out the SD card after about 6 hours.

Results and notes from this script/timelapse:

  • Dislike case of Raspberry Pi 3
  • Tape job was very perilously perched after a night of sitting - need mechanical security, not just tape and balance (ZIP TIES and/or LASHING)

What to keep the same:

  • Camera worked ok
  • Mini SD card worked ok
  • Kali operating system worked ok, connected to wireless ok, hosted files ok, transferred data ok
  • Power cable situation worked ok

What to change for next timelapse:

  • More HD space
  • No red light
  • Case for Pi - loose top makes it awkward to do anything with it, forced camera angle is awkward too
  • Need a swiveling plate or platform or boom
  • Up/down angle of camera was difficult to control

For Next Design:

  • Make it reusable across other types of camera and sensor platforms
  • What other kinds of cameras might we be using
  • What other kinds of control systems
  • Generic mounting brackets, mechanical connections, on a platform would be good. Screws/bolts/etc.
  • Way of positioning platform using railing.

What to change eventually:

  • Adjustable frequency and more sophistication in script
  • Motor control of plate or device to control angles and motion
  • Image processing (vivid, contrast, hue, blur, tilt shift)

Processing timelapse photos

For notes on processing timelapse photos, see Timelapse/Processing page.

Notes on time-lapse photography in general at the Timelapse page.

Next timelapse

For planning notes for Timelapse 3, see: RaspberryPi/Timelapse3

Flags