Python3.5 Raspberry中的排序函数错误

时间:2019-05-03 13:35:03

标签: python python-3.x opencv raspberry-pi timelapse

此代码在使用python 3.7的Windows上完美运行,但是在使用python 3.5的树莓上,sorted函数不起作用。

导致错误的代码行:

sorted_images = sorted(image_list, key=os.path.getmtime)

在Ppython文档中,我没有找到从一个版本到另一个版本的任何更改。有人可以给我替代方法吗?

我是新来的,对您的某些错误表示歉意。 ;-)

错误:

pi@raspberrypi:~/TCC/TimeLapse $ python timelapse.py
File "timelapse.py", line 53
sorted_images = sorted(image_list, key=os.path.getmtime)
        ^

SyntaxError: invalid syntax

我的代码:

import os
import numpy as np
import cv2
import time
import datetime

from utils import CFEVideoConf, image_resize
import glob

cap = cv2.VideoCapture(0) #0 pra webcam interna , 1 para externa

frames_per_seconds = 20
save_path='saved-media/timelapse.mp4'
config = CFEVideoConf(cap, filepath=save_path, res='480p')
out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims)
timelapse_img_dir = 'images/timelapse/'
seconds_duration = 60
seconds_between_shots = 1

if not os.path.exists(timelapse_img_dir):
   os.mkdir(timelapse_img_dir)

now = datetime.datetime.now()
finish_time = now + datetime.timedelta(seconds=seconds_duration)
i = 0
while datetime.datetime.now() < finish_time:
   '''
   Ensure that the current time is still less
   than the preset finish time
   '''
   ret, frame      = cap.read()
   #filename        = f"{timelapse_img_dir}/{i}.jpg"
   filename        = '{timelapse_img_dir}/{i}.jpg'.format(**locals())
   i               += 1
   cv2.imwrite(filename, frame)
   time.sleep(seconds_between_shots)
   if cv2.waitKey(20) & 0xFF == ord('q'):
       break


def images_to_video(out, image_dir, clear_images=True):
    #image_list = glob.glob(f"{image_dir}/*.jpg")
    image_list = glob.glob('{image_dir}/*.jpg'.format(**locals()))
    #sorted_images = sorted(image_list, key=lambda c: os.path.getmtime(c))
    sorted_images = sorted(image_list, key=os.path.getmtime)
    for file in sorted_images:
       image_frame  = cv2.imread(file)
       out.write(image_frame)
    if clear_images:
        '''
        Remove stored timelapse images
        '''
        for file in image_list:
            os.remove(file)

images_to_video(out, timelapse_img_dir)
# When everything done, release the capture

cap.release()
out.release()
cv2.destroyAllWindows()

0 个答案:

没有答案