From charlesreid1

Motivation

I'm done trying to get this cheap-o Pi camera, with the clumsy ribbon cable, to try and work. The whole design, all of it reeks of cheaply-manufactured hardware incapable of anything but the most inane projects. (Hence the plethora of "how to photograph your cat" videos, and nothing more interesting or heavy-duty.)

The Hardware

I ordered a USB camera (1080 P, 2 MP?) from Amazon:

USBCamera1.jpg

USBCamera2.jpg

Fswebcam (Success)

The camera didn't come with instructions, but controlling it was insanely easy [1]:

$ apt-get install fswebcam
$ fswebcam image.jpg

aaaaand, now you have your first picture. To serve it up with a lightweight Python web server,

$ python -m SimpleHTTPServer 8000

Then point browser to 192.168.0.111:8000, and voila, the image is there and ready:

MyFirstUSBWebcamPhoto.jpg

Better Pictures

For best quality, use:

fswebcam -r 1280x720 --no-banner image.jpg

this results in a better image resolution without the obnoxious timestamp banner:

USBWebcamPhoto2.jpg

Wow! That's good enough to use for computer vision to detect cat faces.

Motion program for camera stream (Success)

You can also stream from your camera using the motion program.

The problem: can't stream images using the motion program. I know the camera works with the Pi b/c I can capture still images, so it isn't a power issue. I can see motion dumping out still images, so it isn't a communication issue with the camera.

Installing motion

$ apt-get install motion

Configuring motion

Now modify the motion config file to allow for webcam streaming:

$ sudo vim /etc/motion/motion.conf

change these lines:

stream_localhost on
webcontrol_localhost on

to these lines:

stream_localhost off
webcontrol_localhost off

Start motion daemon

To start the motion daemon:

$ sudo motion
[0] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[0] [ALR] [ALL] conf_cmdparse: Unknown config option "sdl_threadnr"
[0] [NTC] [ALL] motion_startup: Motion 3.2.12+git20140228 Started
[0] [NTC] [ALL] motion_startup: Logging to syslog
[0] [NTC] [ALL] motion_startup: Using log type (ALL) log level (NTC)
[0] [NTC] [ALL] become_daemon: Motion going to daemon mode
$ 

Ensure it is running:

$ ps aux | grep motion
root      3239 10.5  1.2  55940 10692 ?        Sl   19:30   0:01 motion

Ensure it is listening for connections:

$ sudo netstat -apt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 *:tproxy                *:*                     LISTEN      3239/motion

Monitoring stream

The monitoring stream is set in the configuration file to port 8080:

############################################################
# Live Stream Server
############################################################

# The mini-http server listens to this port for requests (default: 0 = disabled)
stream_port 8080

go to the IP address of the Pi and tack on a :8080 to view the camera stream:

MotionCameraStream.png

Control stream

Control of the camera can be done via the web interface as well (including pan and tilt controls, if you have that set up). That's also set in the configuration file:

############################################################
# HTTP Based Control
############################################################

# TCP/IP port for the http server to listen on (default: 0 = disabled)
webcontrol_port 8081

This isn't terribly interesting, but it is useful:

MotionCameraControl.png

OpenCV

Capturing Images

An image can be captured from a USB camera/webcam using OpenCV. Normally this would be a bit much for a simple webcam photo, but if you're already using OpenCV for image processing, it may be more convenient.

Here's a simple script (via Software Recommendations Stack Exchange) [2]:

import time
import cv2
camera_port = 0
camera = cv2.VideoCapture(camera_port)
time.sleep(0.1)  # If you don't wait, the image will be dark
return_value, image = camera.read()
cv2.imwrite("opencv.png", image)
del(camera)  # so that others can use the camera as soon as possible

More Advanced - Face and Feature Detection

See OpenCV/Face Detection and OpenCV/Feature Detection for more details on how to use OpenCV to do facial detection and feature detection.

See RaspberryPi/OpenCV for details on how to install OpenCV on a Raspberry Pi.

Also see the Github repository: https://github.com/charlesreid1-raspberry-pi/pi-opencv.git

Projects

Now that we have the camera up and operational, and we're happy with the preliminary results... what next?

Timelapse 4

See RaspberryPi/Timelapse 4

Projects

Now that we have the camera up and operational, and we're happy with the preliminary results... what next?

Timelapse 4

See RaspberryPi/Timelapse 4

Flags