1

This code works perfectly on Windows with Python 3.7, but on raspberry with Python 3.5 the sorted function does not work.

Line of the code that causes the error:

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

In thePpython documentation I did not find any changes from one version to another. Can someone give me an alternative?

I'm new here, sorry for some mistake. ;-)

Error:

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

SyntaxError: invalid syntax

My code:

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()
nnnmmm
  • 7,964
  • 4
  • 22
  • 41
  • Are you sure that `image_list = glob.glob('{image\_dir}/*.jpg'.format(**locals(` **`)))`** ends with **exactly** 3 (not less) closing parentheses? – CristiFati May 03 '19 at 13:39
  • **That** code doesn't throw that error. Your line in the error is different than the actual line in your code, that tells me that something else might be different between the code you've shown and the code you're actually running. – ruohola May 03 '19 at 13:41
  • I think what @ruohola is trying to point out is that the line of code in the error message does not actually appear in the code block that you have posted. – GPPK May 03 '19 at 14:37
  • @GPPK Yes, the line in the error is: `sorted_images = sorted(image_list, key=os.path.getmtime)` and the closest looking line in the posted code is: `sorted_images = sorted(image_list, key=lambda c: os.path.getmtime(c))`. – ruohola May 03 '19 at 14:40
  • Sorry for the error, I got the error code after a test. however on windows (python3.7) both codes work, already on raspberry none. – Alex freires marques May 03 '19 at 17:11
  • @CristiFati Yes, one from Locals, another format and the last one from glob. It's not right ? I am new to python the chance to miss syntax is great – Alex freires marques May 03 '19 at 17:22
  • Yes it's right. But usually your error is because of these kind of things (I reproduced it by deleting one "***)***"). You should add *print* statements before the line throwing the error, to see where the problem is. – CristiFati May 03 '19 at 17:39

0 Answers0