def saveImage(self,settings, width, height, quality, diskSpaceToReserve): self.keepDiskSpaceFree(diskSpaceToReserve) time = datetime.now() filename = filepath + "/" + filenamePrefix + "-%04d%02d%02d-%02d%02d%02d.jpg" % (time.year, time.month, time.day, time.hour, time.minute, time.second) subprocess.call("raspistill %s -w %s -h %s -t 200 -e jpg -q %s -n -o %s" % (settings, width, height, quality, filename), shell=True) #print "Captured %s" % filename Helper.log("Captured %s" % filename)
def keepDiskSpaceFree(self,bytesToReserve): if (self.getFreeSpace() < bytesToReserve): for filename in sorted(os.listdir(filepath + "/")): if filename.startswith(filenamePrefix) and filename.endswith(".jpg"): os.remove(filepath + "/" + filename) #print "Deleted %s/%s to avoid filling disk" % (filepath,filename) Helper.log("Deleted %s/%s to avoid filling disk" % (filepath,filename)) if (self.getFreeSpace() > bytesToReserve): return
def check_inactivity(self): try: while (time.time() - self.latestActivity) < self.maxInactivityTime: print "Inaktivitaet: Client aktiv?" time.sleep(5) Helper.log("Client inactive, diconncted.") self.write_message("Inactive.. Disconnected!") self.close() self.on_close() except tornado.websocket.WebSocketClosedError: print "Inaktivitaet: Inaktivter Client hat Seite bereits verlassen.."
def open(self): #Globals.COUNT_CLIENTS #print "WebSocket opened" Helper.log("WebSocket opened") Globals.COUNT_CLIENTS = Globals.COUNT_CLIENTS + 1 # Variable mit Timestamp der letzten Aktion, wird in on_message aktualisiert self.latestActivity = time.time() # Nur 1 Client darf Kamera kontrollieren if Globals.COUNT_CLIENTS > 1: self.write_message("Camera already in use.. Disconnected!") Globals.COUNT_CLIENTS = Globals.COUNT_CLIENTS - 1 self.close() # Pruefen ob der Client inactiv wird self.activityThread = thread.start_new_thread(self.check_inactivity,())
def on_message(self, message): #global PAN_START_ANGLE,TILT_START_ANGLE self.latestActivity = time.time() return_message = "Camera moved!" try: result = json.loads(message) Helper.log("JSON erhalten: %s" % (result)) #print result delta = float(result['delta']) #Horizontale Bewegung if result['direction'] == 'h': #print "Horizontal:" if (Globals.PAN_START_ANGLE + delta) > self.MAX_ANGLE or (Globals.PAN_START_ANGLE + delta) < self.MIN_ANGLE: return_message ="Maximum pan stop reached!" else: Globals.PAN_START_ANGLE = Globals.PAN_START_ANGLE + delta #print "Horizontal: Move to " + str(Globals.PAN_START_ANGLE) Helper.log("Horizontal: Move to " + str(Globals.PAN_START_ANGLE)) Globals.panServoPWM.setCommand(Globals.PAN_START_ANGLE) elif result['direction'] == 'v': print "Vertikal:" #Vertikale Bewegung if (Globals.TILT_START_ANGLE + delta) > self.MAX_ANGLE or (Globals.TILT_START_ANGLE + delta) < self.MIN_ANGLE: return_message ="Maximum tilt stop reached!" else: Globals.TILT_START_ANGLE = Globals.TILT_START_ANGLE + delta Helper.log("Vertikal: Move to " + str(Globals.PAN_START_ANGLE)) Globals.tiltServoPWM.setCommand(Globals.TILT_START_ANGLE) else: print "initialize.." # Ergebnis zurueckgeben self.write_message(return_message) except ValueError: #print "invalid json.." Helper.log("Received invalid JSON")
def exitThreads(): Helper.log("Hoticam duch Signal beendet") global picamThread global cameraStream print ("You pressed Ctrl+C!") # Threads beenden try: if cameraStream.isAlive(): cameraStream.stop() cameraStream.join() except AttributeError: Helper.log("CameraStream Undefined..") try: if picamThread.isAlive(): picamThread.stop() except AttributeError: print "picamThread Undefined.." Helper.log("picamThread Undefined..") sys.exit(0)
def switchStreamOrMotion(): global picamThread global cameraStream picamThread = PicamThread() cameraStream = CameraStream() while True: if Globals.COUNT_CLIENTS > 0: if not cameraStream.isAlive(): Helper.log("Stope Bewegungserkennung") picamThread.stop() picamThread.join() Helper.log("Schalte um auf Streaming") cameraStream.start() else: Helper.log("Stream laeuft..") else: # Wenn Stream laeuft, beenden if cameraStream.isAlive(): cameraStream.stop() # Wenn Thread beendet wurde, neuen Thread erstellen if picamThread.stopped(): picamThread = PicamThread() if not picamThread.isAlive(): Helper.log("Stoppe Streaming") picamThread.start() Helper.log("Schalte um auf Bewegungserkennung") else: # print "motion laeuft bereits!" Helper.log("Bewegungserkennung laeuft") time.sleep(5)
Helper.log("CameraStream Undefined..") try: if picamThread.isAlive(): picamThread.stop() except AttributeError: print "picamThread Undefined.." Helper.log("picamThread Undefined..") sys.exit(0) application = tornado.web.Application([(r"/", WebSocketServer)]) if __name__ == "__main__": try: Helper.log("Starte Hoticam") # Create ServoPWM instances to control the servos Globals.tiltServoPWM = ServoPWM( TILT_PWM_PIN, minAnglePulseWidthPair=(0.0, 2350), midAnglePulseWidthPair=(90.0, 1500), maxAnglePulseWidthPair=(180.0, 500.0), ) Globals.panServoPWM = ServoPWM( PAN_PWM_PIN, minAnglePulseWidthPair=(0.0, 610.0), midAnglePulseWidthPair=(90.0, 1500), maxAnglePulseWidthPair=(180.0, 2350), )
def on_close(self): #global COUNT_CLIENTS Globals.COUNT_CLIENTS = Globals.COUNT_CLIENTS - 1 Helper.log("WebSocket closed")
def __init__(self): self.alive = False self.process = None self.FNULL = None self.command = "raspivid -t 9999999 -w {} -h {} -fps {} -b {} -o - | ffmpeg -i - -vcodec copy -an -r 25 -f flv -metadata streamName=myStream tcp://0.0.0.0:6666".format(Helper.get("width"),Helper.get("height"),Helper.get("fps"),Helper.get("bitrate"))
import time import threading from datetime import datetime from PIL import Image from Globals import Helper # Motion detection settings: # Threshold - how much a pixel has to change by to be marked as "changed" # Sensitivity - how many changed pixels before capturing an image, needs to be higher if noisy view # ForceCapture - whether to force an image to be captured every forceCaptureTime seconds, values True or False # filepath - location of folder to save photos # filenamePrefix - string that prefixes the file name for easier identification of files. # diskSpaceToReserve - Delete oldest images to avoid filling disk. How much byte to keep free on disk. # cameraSettings - "" = no extra settings; "-hf" = Set horizontal flip of image; "-vf" = Set vertical flip; "-hf -vf" = both horizontal and vertical flip threshold = 40 sensitivity = int(Helper.get("sensitivity")) forceCapture = False forceCaptureTime = 60 * 60 *6 # Every six hours filepath = Helper.get("filepath") #"/home/pi/elektro/webcam/webinterface/picam" filenamePrefix = "capture" diskSpaceToReserve = int(Helper.get("diskSpaceToReserve")) # Keep 40 mb free on disk cameraSettings = "" # settings of the photos to save saveWidth = int(Helper.get("saveWidth")) saveHeight = int(Helper.get("saveHeight")) saveQuality = int(Helper.get("saveQuality")) # Set jpeg quality (0 to 100) # Test-Image settings testWidth = 100 testHeight = 75