ich Versuche zur Zeit mit meinem Raspberry und einer Picamera einen Livestream in meinem Heimnetz zu starten. Das funktioniert soweit auch ganz gut, aber über die selbe Kamera würde ich gerne auch regelmäßig Bilder speichern lassen um später daraus eine Zeitraffer Aufnahme erstellen zu können.
Mit dem folgenden Code hat es funktioniert, aber jetzt nach einer Testnacht hat das Script einfach aufgehört zu arbeiten, aber auch keinen Fehler ausgegeben.
Vielleicht kann einer von euch mir weiterhelfen oder hat eine bessere Idee.
Hier die beiden Scripts, hab das meiste aus dem Netz, da ich noch ein Neuling bin.
MfG
G
Stream.py
Code: Alles auswählen
# W# Web streaming example
# Source code from the official PiCamera package
# http://picamera.readthedocs.io/en/latest/recipes2.html#web-streaming
import io
import threading, os, signal
import picamera
import logging
import socketserver
import subprocess
import sys
import glob
from threading import Condition
from http import server
from datetime import datetime, timedelta
from subprocess import check_call, call
from select import select
PAGE="""\
<html>
<head>
<title>G-Cam</title>
</head>
<body>
<center><h1>G-Cam</h1></center>
<center><img src="stream.mjpg" width="1280" height="720"></center>
</body>
</html>
"""
ipath = "/home/pi/Capture.py" #CHANGE PATH TO LOCATION OF mouse.py
def thread_second():
call(["python3", ipath])
def check_kill_process(pstring):
for line in os.popen("ps ax | grep " + pstring + " | grep -v grep"):
fields = line.split()
pid = fields[0]
os.kill(int(pid), signal.SIGKILL)
class StreamingOutput(object):
def __init__(self):
self.frame = None
self.buffer = io.BytesIO()
self.condition = Condition()
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# New frame, copy the existing buffer's content and notify all
# clients it's available
self.buffer.truncate()
with self.condition:
self.frame = self.buffer.getvalue()
self.condition.notify_all()
self.buffer.seek(0)
return self.buffer.write(buf)
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 output.condition:
output.condition.wait()
frame = output.frame
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')
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
with picamera.PiCamera() as camera:
output = StreamingOutput()
# Uncomment the next line to change your Pi's Camera rotation (in degrees)
# camera.rotation = 90
camera.start_recording(output, format='mjpeg')
try:
address = ('', 8000)
server = StreamingServer(address, StreamingHandler)
check_kill_process('Capture.py')
processThread = threading.Thread(target=thread_second)
processThread.start()
server.serve_forever()
finally:
camera.stop_recording()
Code: Alles auswählen
#!/usr/bin/python3
import io
import threading
import os
import signal
import picamera
import logging
import socketserver
import time
from datetime import datetime, timedelta, date
from select import select
from threading import Condition
from http import server
import subprocess
from subprocess import check_call, call
import sys
import glob
ipath = "/home/pi/Stream.py"
def thread_second():
call(["python3", ipath])
def check_kill_process(pstring):
for line in os.popen("ps ax | grep " + pstring + " | grep -v grep"):
fields = line.split()
print(fields)
pid = fields[0]
print(pid)
os.kill(int(pid), signal.SIGKILL)
# run script continuosly
while True:
now = date.today() and datetime.now()
dt_string = now.strftime("%d_%m_%Y_%H_%M_%S")
# only activate code when button is pressed (not released)
print('cap active')
# time.sleep(21600) # Alle 6std
time.sleep(1800) # Alle halbe Stunde
print('timer um')
# wait()
# end livestream
check_kill_process('Stream.py')
print("Stream ended.")
# take picture with camera
with picamera.PiCamera() as camera:
camera.start_preview()
time.sleep(2)
camera.capture("/home/pi/Pictures/" + str(dt_string) + '.jpg')
print('Captured' + str(dt_string))
# run live stream again
processThread = threading.Thread(target=thread_second)
processThread.start()
print("Stream running. Refresh page.")
# print in the command line instead of file's cons
if __name__ == '__main__':
main()