A10s application : Closed-circuit television

Hardware

I’ll use a A10S olimex card with linux, i’ll connect a webcam or an pinacle USB acquisition module + a camera Olimex default kernel doesn’t include a lot of drivers , you should replace the default kernel with your own which includes device drivers for USB webcam or acquisition module. see the link http://olimex.wordpress.com/2013/10/28/building-debian-sd-card-for-linux-with-kernel-3-4-from-scratch-for-a10s-olinuxino-micro/

Video acquisition

mencoder is a linux application able to compress and write video stream to hard drive, for example :

mencoder tv:// -tv driver=v4l2:input=1:width=720:height=576:device=/dev/video1 -nosound -ovc nuv -o output.avi<br />

grayscale recording, using mpeg4 codec :

mencoder tv:// -tv driver=v4l2:input=1:width=720:height=576:device=/dev/video1 -nosound -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1800:gray:vhq:keyint=250 -o webcam_30s_lav.avi<br />
  • device, shall be modified according your configuration
  • input shall be set if there is there is many video input, see log when you launch mencoder
  • ovc , can be for example
    • nuv for nuppel real time acquisition
    • lavc for ffmpeg compression see link or link_French
  • -nosound is important

how to generate RTP stream using VLC (see link)

after that you need to manage you webcam using opencv for example (here is a good example)

### python webcam acquisition script
#!/usr/bin/python import cv2.cv as cv
import time
import sys
cv.NamedWindow(&quot;camera&quot;, 1)
capture = cv.CaptureFromCAM(0)<br />
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,320)<br />
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,240)<br />
while True:<br />
 img = cv.QueryFrame(capture)<br />
 cv.ShowImage(&quot;camera&quot;, img)<br />
 sys.stdout.write( img.tostring() )<br />
 k = cv.WaitKey(10)<br />
 if 0xFF &amp; k == 27:<br />
  break<br />
cv.DestroyAllWindows()<br />

then pipe the output of this script to VLC (with different codec and compress ratio) :

# python camera_opencv1.py | cvlc --demux=rawvideo --rawvid-fps=10 --rawvid-width=320 --rawvid-height=240 --rawvid-chroma=RV24 - --sout "#transcode{vcodec=h264,vb=2000,fps=10,width=320,height=240}:rtp{port=1234,sdp=rtsp://:8123/test.sdp}"
#python camera_opencv1.py | cvlc --demux=rawvideo --rawvid-fps=10 --rawvid-width=320 --rawvid-height=240 --rawvid-chroma=RV24 - --sout "#transcode{fps=10,vcodec=mp4v,vb=2048,width=320,height=240}:rtp{port=1234,sdp=rtsp://:8123/test.sdp}"
python camera_opencv1.py | cvlc --demux=rawvideo --rawvid-fps=10 --rawvid-width=320 --rawvid-height=240 --rawvid-chroma=RV24 - --sout "#transcode{fps=10,vcodec=mp4v,vb=1024,width=320,height=240}:rtp{port=1234,sdp=rtsp://:8123/test.sdp}"

first mencoder & vlc coupling , there is many error for the momment

mencoder tv:// -tv driver=v4l2:input=1:width=720:height=576:device=/dev/video1 -nosound -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1800:vhq:keyint=250 -o - | cvlc - :demux=mp4v

demux vlc option need to be set to indicate how to demux another option, directly acquire and stream from vlc :

cvlc v4l2:// :v4l2-vdev=&quot;/dev/video0 --sout "#transcode{fps=10,vcodec=mp4v,vb=1024,width=320,height=240}:rtp{port=1234,sdp=rtsp://:8123/test.sdp}" --no-sout-audio

then to read from another workstation

mplayer rtsp://xx.xx.xx.xx:8123/test.sdp

VLC usefull command

Vlc command line for video display :

cvlc v4l2:///dev/video1:width=320:height=240

Vlc command line for http streaming (x264)

cvlc v4l2:// :v4l2-vdev=/dev/video0 --sout "#transcode{fps=10,vcodec=x264,vb=800}:standard{access=http,mux=ts,dst=:8080}" --no-sout-audio

Vlc command line for http streaming (mp4)

cvlc v4l2:// :v4l2-vdev=/dev/video0 --sout "#transcode{fps=10,vcodec=mp4v,vb=800}:standard{access=http,mux=ogg,dst=:8080}" --no-sout-audio

V4L2 command line configuration

my webcam driver is automatically loaded with autogain enabled, you can control this feature using v4l2ctrl. First connect the camera and save a configuration file

v4l2ctrl -d /dev/video0 -s myconfig

then edit the generated file « myconfig » and edit « Auto Gain » (set it to 0) parameter then save and close

9963779:                            Hue:64
9963788:        Whitebalance (software):0
9963792:               Gamma (software):1000
9963793:                       Exposure:254
9963794:           Auto Gain (software):0
9963795:                           Gain:63
9963796:           Horizontal flip (sw):0
9963797:             Vertical flip (sw):0
9969664:               Auto Gain target:100

and finaly , load the file

v4l2ctrl -d /dev/video0 -l myconfig

read a vlc stream using opencv (french):  http://fr.openclassrooms.com/forum/sujet/lire-une-video-en-streaming-avec-opencv?page=1

 

Read a RTSP stream using opencv

here is a opencv example modified to read rtsp stream


#!/usr/bin/env python2

"""
OpenCV example. Show webcam image and detect face.
"""

import cv2

TRAINSET = "face.xml"
DOWNSCALE = 4

webcam = cv2.VideoCapture("rtsp://192.168.1.15/live0.264")
while webcam.isOpened()==False:
 print webcam
 print "cv2.VideoCapture.isOpened() ",webcam.isOpened()

cv2.namedWindow("preview")
classifier = cv2.CascadeClassifier(TRAINSET)

rval = False
while rval==False:
 rval, frame = webcam.read()
 #print rval

if webcam.isOpened(): # try to get the first frame
 rval, frame = webcam.read()
else:
 rval = False
print "rval",rval
while rval:
 print rval
 # detect faces and draw bounding boxes
 minisize = (frame.shape[1]/DOWNSCALE,frame.shape[0]/DOWNSCALE)
 miniframe = cv2.resize(frame, minisize)
 faces = classifier.detectMultiScale(miniframe)
 for f in faces:
 x, y, w, h = [ v*DOWNSCALE for v in f ]
 cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255))

 cv2.putText(frame, "Press ESC to close.", (5, 25),
 cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255,255,255))
 cv2.imshow("preview", frame)

 # get next frame
 rval, frame = webcam.read()

 key = cv2.waitKey(20)
 if key in [27, ord('Q'), ord('q')]: # exit on ESC
 break

launch of ffmpeg integrated into python , and read raw input
http://zulko.github.io/blog/2013/09/27/read-and-write-video-frames-in-python-using-ffmpeg/

Publicités

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s