ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

How to stream webcam after OpenCV calculation

Thu Nov 26, 2015 6:23 am

Hi,
by copy and past some code examples, I did a tipical exercise , building a rover:

I can access my picam,
get the image , lets call it img
track a ball inside my img,
draw a circle around my ball in the img
find ball center and use this info to rotate my rover and move it in the direction of the ball.

I also have a websocket server (using tornado) that manage a web page where i can push buttons and command the rover manually

Now I want to stream my modified img (including some text comments and circle drawing) to the webpage.
Do yuo have some example to follow?
BR
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com

ofe
Posts: 47
Joined: Wed Jun 05, 2013 4:05 am
Location: Italy,Milan
Contact: Website

Re: How to stream webcam after OpenCV calculation

Thu Dec 10, 2015 5:58 am

HI,
I reach the goal to set up a webserver.
Now it is enogh to save the my_stream fo r oc2 analisys ( I hope so)
import threading
import picamera
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import StringIO
import time

class CamHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith('cam.mjpg'):
self.send_response(200)
self.send_header('Content-type', 'multipart/x-mixed-replace; boundary=--jpgboundary')
self.end_headers()
while True:
try:
my_stream = StringIO.StringIO()
self.camera.capture(my_stream,'jpeg', use_video_port = True)
self.wfile.write("--jpgboundary")
self.send_header('Content-type','image/jpeg')
self.send_header('Content-length',len(my_stream.getvalue()))
self.end_headers()
self.wfile.write(my_stream.getvalue())
time.sleep(0.01)
except KeyboardInterrupt:
break
return

if self.path.endswith('.html'):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write('<html><head></head><body>')
self.wfile.write('<img src="http://127.0.0.1:9094/cam.mjpg"/>')
self.wfile.write('</body></html>')
return

class cameraserver(threading.Thread):
def __init__(self,camera):
threading.Thread.__init__(self)
self.lastpic = None
CamHandler.camera = camera
self.server = HTTPServer(('',9094),CamHandler)

def run(self):
try:

print "cameraserver started"
self.server.serve_forever()
except KeyboardInterrupt:
self.server.socket.close()

def stop(self):
self.server.socket.close()
print "cameraserver stopped"

def main():


with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.framerate = 24
camera.start_preview()
# Camera warm-up time
time.sleep(2)
try:
camserver = cameraserver(camera)
camserver.start()
res = raw_input()
camserver.stop()
camera.close()
except KeyboardInterrupt:
camserver.socket.close()

if __name__ == '__main__':
main()
Oscar

Visit my quadcopter blog
http://solenerotech1.wordpress.com


Return to “Python”