#!/usr/bin/python3 # Mostly copied from https://picamera.readthedocs.io/en/release-1.13/recipes2.html # Run this script, then point a web browser at http::8000 # Note: needs simplejpeg to be installed (pip3 install simplejpeg). # partly also taken from: # https://github.com/raspberrypi/picamera2/blob/main/examples/mjpeg_server.py #BSD 2-Clause License # #Copyright (c) 2021, Raspberry Pi #All rights reserved. # #Redistribution and use in source and binary forms, with or without #modification, are permitted provided that the following conditions are met: # #1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # #2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" #AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE #IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE #FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL #DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER #CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, #OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE #OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import io import logging import socketserver import time import sys import numpy as np import cv2 from pylepton import Lepton from http import server from threading import Condition device = "/dev/spidev0.0" PAGE = """\ Kamera

Livebild

""" class StreamingHandler(server.BaseHTTPRequestHandler): def do_GET(self): if self.path == '/': self.send_response(301) self.send_header('Location', '/index.html') self.end_headers() elif self.path == '/index.html': content = PAGE.encode('utf-8') self.send_response(200) self.send_header('Content-Type', 'text/html') self.send_header('Content-Length', len(content)) self.end_headers() self.wfile.write(content) elif self.path == '/stream.mjpg': self.send_response(200) self.send_header('Age', 0) self.send_header('Cache-Control', 'no-cache, private') self.send_header('Pragma', 'no-cache') self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') self.end_headers() try: while True: with Lepton(device) as l: a,_ = l.capture() cv2.normalize(a, a, 0, 65535, cv2.NORM_MINMAX) np.right_shift(a, 8, a) bild = np.uint8(a) bild2 = np.zeros((60,80,3), dtype=np.uint8) bild3 = np.zeros((64,80,3), dtype=np.uint8) np.copyto(bild2,bild) for i in range (len(bild)): for j in range(80): val = bild2[i][j][0] bild3[i][j][2] = val bild3[i][j][0] = (255-val) bild3[i][j][1] = (val<<1)/4 jpeg = cv2.imencode('.jpg', bild3)[1] jpegtuple = np.array(jpeg) frame = jpegtuple.tobytes() self.wfile.write(b'--FRAME\r\n') self.send_header('Content-Type', 'image/jpeg') self.send_header('Content-Length', len(frame)) self.end_headers() self.wfile.write(frame) self.wfile.write(b'\r\n') time.sleep(0.14) except Exception as e: logging.warning( 'Removed streaming client %s: %s', self.client_address, str(e)) else: self.send_error(404) self.end_headers() class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer): allow_reuse_address = True daemon_threads = True try: address = ('', 8000) server = StreamingServer(address, StreamingHandler) server.serve_forever() finally: print("Ahoi")