Finally some what of a break through,
Using OpenCv for just capturing a web cam seems wasteful and it doesn’t work well with the software decoding and re encoding jpegs from a mjpeg stream. Also all the steps to set up pwm pins a / uart pins on the beaglebone that Ive take to date seem to start other problems.
For web cam capture via a USB camera I found a v4l2 library that makes it fast and simple. Install the v4l2capture library, it takes a few iterations to get all the dependencies installed. see the camera script and web server example pasted below.
for pin control i’ve been leaving the uEnv.txt file alone and using the univeral(x) cape manager and config-pin (ex config-pin -a P9.24 uart) to set pin function. there are still quirks and you have to reset pins on reboot, but its been manageable. for instance if you load the cape manger overlay directly it doesnt always work, but if it loads itself after finding its not loaded when executing config-pin - a Px.xx nnnn it works fine, also if you run config-pin -a to set a pin to uart it auto loads cape-universal which doesnt let some pwm pins work, so its best to get universala loaded first as one does not replace the other and I’m not sure they can be unloaded with out cycling power.
https://github.com/cdsteinkuehler/beaglebone-universal-io
#camera.py
import select
import v4l2capture
import time
class VideoCamera(object):
#globals for testing
size_x=0
size_y=0
intitialize
def init(self):
grab camera
self.video = v4l2capture.Video_device("/dev/video0")
#set format the python version of v4l2 and v4l2capture API’s are not the great
size_x, size_y = self.video.set_format(640, 480, fourcc=‘MJPG’)
print (‘size :’,size_x,size_y)
#create a memory buffer for the jpgs
self.video.create_buffers(1)
self.video.queue_all_buffers()
start the vidoe capture process
self.video.start()
destructor
def del(self):
self.video.close()
capture image
def get_frame(self):
#before = time.clock()
#not sure what select.select is doing
select.select((self.video,), (), ())
read and queue set the motion so to speak, with read alone you only get the first image on start
jpeg = self.video.read_and_queue()
#print 1/(time.clock()-before)
return jpeg
#!/usr/bin/env python
main.py
Project: Video Streaming with Flask
Author: Log0 <im [dot] ckieric [at] gmail [dot] com>
Date: 2014/12/21
Description:
Modified to support streaming out with webcams, and not just raw JPEGs.
Most of the code credits to Miguel Grinberg, except that I made a small tweak. Thanks!
Usage:
1. Install Python dependencies: cv2, flask. (wish that pip install works like a charm)
2. Run “python main.py”.
3. Navigate the browser to the local webpage (ex 192.168.8.100:5000).
from flask import Flask, render_template, Response
from camera import VideoCamera
import time
app = Flask(name)
@app.route(’/’)
def index():
return render_template(‘index.html’)
def gen(camera):
the timestart variable is an attempt to limit calls to get a camera frame 1/.0333 (30) times per second to match the camera limit
timestart =time.clock()+.033333
while True:
if time.clock() > timestart:
timestart=time.clock()+.033333
frame = camera.get_frame()
yield (b’–frame\r\n’b’Content-Type: image/jpeg\r\n\r\n’ + frame + b’\r\n\r\n’)
@app.route(’/video_feed’)
def video_feed():
return Response(gen(VideoCamera()),
mimetype=‘multipart/x-mixed-replace; boundary=frame’)
if name == ‘main’:
app.run(host=‘0.0.0.0’, debug=True)