0

我在凌晨 3 点从树莓派上的 crontab 触发了这个脚本。这个想法是在一天的过程中拍摄延时视频,但仅限于太阳升起和日落之间。触发时的代码计算日落和日出时间,然后等待日出。然后它应该开始拍照,但由于某种原因它似乎没有在日出时触发。我认为这可能是因为“如果 home.date == 日出:”太准确了?我怎样才能让它忽略几分之一秒,甚至完全忽略几秒钟?

这是完整的代码-

import os
import time
import ephem

# find time of sun rise and sunset
sun = ephem.Sun()
home = ephem.Observer()
home.lat, home.lon = '52.8709', '-1.797' #your lat long
sun.compute(home)
sunrise, sunset = home.next_rising(sun)),
                   home.next_setting(sun))
daylightminutes = (sunset - sunrise) * 1440 # find howmany minutes of daylight there are
daylightminutes = (round(daylightminutes))
def dostuff() :
        if home.date == sunrise:
                import datetime
                import sys
                FRAMES = 60#daylightminutes -600 # number of images you want in timelapse video
                FPS_IN = 4 # number of images per second you want in video
                FPS_OUT = 4 # number of fps in finished video 24 is a good value
                TIMEBETWEEN = 6 # number of seconds between pictures, 60 = 1 minute
                #take the pictures needed for the time lapse video
                frameCount = 0
                while frameCount < FRAMES:
                    imageNumber = str(frameCount).zfill(7)
                    os.system("raspistill -rot 270 -o /home/pi/image%s.jpg"%(imageNumber))
                    frameCount += 1
                    time.sleep(TIMEBETWEEN - 6) #Takes roughly 6 seconds to take a picture
                #record current time and date in variable datetime
                datetime = time.strftime("%Y%m%d-%H%M")
                # make the timelapse video out of the images taken
                os.system("avconv -r %s -i /home/pi/image%s.jpg -r %s -vcodec libx264 -crf 20 -g 15 -vf crop=2592:1458,scale=1280:720 /home/pi/timelapse%s.mp4" %(FPS_IN,'%7d',FPS_OUT,datet$
                #send the timelapse video to dropbox
                from subprocess import call
                photofile = "/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload /home/pi/timelapse%s.mp4 /home/pi/timelapse%s.mp4" %(datetime,datetime)
                call ([photofile], shell=True)
                #remove the timelapse video copy and all images it is made up of that are held localy on the Rpi
                os.system("rm /home/pi/timelapse%s.mp4"%(datetime))
                os.system("rm /home/pi/image*")
                sys.exit()
while home.date <= sunrise:
        dostuff()

如果您发现任何其他可能导致问题的问题,请告诉我。

干杯

史蒂夫

4

1 回答 1

0

(a) 一旦你创建了一个Observer,它date会一直保持不变,直到你手动进入并更改它。所以home.date你一直检查的sunrise永远不会改变,因此永远不会真正到达日出。您可能希望改为调用 PyEphemnow()例程,以便您对当前时间的了解可以推进。

(b) 你是对的,你不太可能碰巧==在日出的毫秒内进行比较,所以你可能想用不等式来比较。

(c) 在日出到来之前,您的循环似乎会以 100% 的速度驱动 CPU,每秒检查数千次日出是否已经到来。您可能希望time.sleep(1)在等待保持 Raspberry Pi 凉爽的同时。

于 2014-03-09T16:39:24.887 回答