Skip to content

Commit 11fafe0

Browse files
committed
-
1 parent 1e684cf commit 11fafe0

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

camera.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Camera(Thread):
4242

4343
_instance = None
4444
_img_template = image.Image.load("coderdojo-logo.png")
45-
stream_port = 9080
45+
stream_port = 8080
4646

4747
@classmethod
4848
def get_instance(cls):
@@ -55,7 +55,7 @@ def __init__(self):
5555
logging.info("starting camera")
5656
cam_props = {"width":640, "height":480, "cv_image_factor":config.Config.get().get("cv_image_factor", 4), "exposure_mode": config.Config.get().get("camera_exposure_mode"), "jpeg_quality": int(config.Config.get().get("camera_jpeg_quality", 20))}
5757
self._camera = camera.Camera(props=cam_props)
58-
self._streamer = streamer.JpegStreamer("0.0.0.0:"+str(self.stream_port), st=0.1)
58+
#self._streamer = streamer.JpegStreamer("0.0.0.0:"+str(self.stream_port), st=0.1)
5959
self.recording = False
6060
self.video_start_time = time.time() + 8640000
6161
self._run = True
@@ -107,9 +107,12 @@ def get_image(self, maxage = MAX_IMAGE_AGE):
107107
return image.Image(self._camera.get_image_bgr())
108108

109109
def save_image(self, image_jpeg):
110-
self._streamer.set_image(image_jpeg)
110+
#self._streamer.set_image(image_jpeg)
111111
self._image_time=time.time()
112112

113+
def get_image_jpeg(self):
114+
return copy.copy (self._camera.get_image_jpeg())
115+
113116
def set_text(self, text):
114117
self._camera.set_overlay_text(str(text))
115118

@@ -186,8 +189,8 @@ def delete_photo(self, filename):
186189
self._photos.remove(filename)
187190

188191
def exit(self):
189-
self._streamer.server.shutdown()
190-
self._streamer.server_thread.join()
192+
#self._streamer.server.shutdown()
193+
#self._streamer.server_thread.join()
191194
self._run = False
192195
self.join()
193196

main.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import os
2121
import json
2222
import logging
23+
import time
2324
import logging.handlers
2425

2526
from coderbot import CoderBot, PIN_PUSHBUTTON
@@ -29,7 +30,7 @@
2930
from program import ProgramEngine, Program
3031
from config import Config
3132

32-
from flask import Flask, render_template, request, send_file, redirect
33+
from flask import Flask, render_template, request, send_file, redirect, Response
3334
from flask.ext.babel import Babel
3435
#from flask_sockets import Sockets
3536

@@ -136,6 +137,40 @@ def handle_bot_status():
136137
logging.info( "bot_status" )
137138
return json.dumps({'status': 'ok'})
138139

140+
def video_stream(cam):
141+
refresh_timeout = float(app.bot_config.get("camera_refresh_timeout", "0.1"))
142+
while True:
143+
last_refresh_time = time.time()
144+
frame = cam.get_image_jpeg()
145+
yield ("--BOUNDARYSTRING\r\n" +
146+
"Content-type: image/jpeg\r\n" +
147+
"Content-Length: " + str(len(frame)) + "\r\n\r\n" +
148+
frame + "\r\n")
149+
now = time.time()
150+
if now - last_refresh_time < refresh_timeout:
151+
time.sleep(refresh_timeout - (now - last_refresh_time))
152+
153+
@app.route("/video")
154+
def handle_video():
155+
return """
156+
<html>
157+
<head>
158+
<style type=text/css>
159+
body { background-image: url(http://github.com/video/stream); background-repeat:no-repeat; background-position:center top; background-attachment:fixed; height:100% }
160+
</style>
161+
</head>
162+
<body>
163+
&nbsp;
164+
</body>
165+
</html>
166+
"""
167+
168+
@app.route("/video/stream")
169+
def handle_video_stream():
170+
try:
171+
return Response(video_stream(cam), mimetype="multipart/x-mixed-replace; boundary=--BOUNDARYSTRING")
172+
except: pass
173+
139174
@app.route("/photos", methods=["GET"])
140175
def handle_photos():
141176
logging.info("photos")

templates/config_params.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</script>
3232
<style>
3333
.ui-content-stream {
34-
background:url('http://{{host}}:{{stream_port}}/stream') no-repeat;
34+
background:url('http://{{host}}:{{stream_port}}/video/stream') no-repeat;
3535
background-size: auto 100% !important;
3636
background-position: center;
3737
padding:0px;

0 commit comments

Comments
 (0)