Timestamp filenames

Nice! it finally works.
I had to modify “wb” instead of “w” because we are trying to write binary, but it works.

with open("img.jpg", "wb") as f:
    f.write(img)

Now I’ll to adapt this to the actual use of sending a date

Glad to hear its working.

Update:

After a significant amount of struggle, here’s a code that would set the time on the board. I didn’t add the break yet, I was wondering whether it would ever be possible to see the camera or I would need to have a separate script to check the camera.
Dealing with openMV is great if I can use it for debug, in my case, because I can’t run this on the IDE, it’s quite difficult to debug. If my goal is to point the camera at something, I will probably maintain 2 different scripts, one with camera only and one with trigger(time setting) + camera.

Again, I love your product, but this is quite a lot of trouble just to set the time properly…

Micropython side

import sensor, image, time, pyb
def year(datetime):
    return str(datetime[0])
def month(datetime):
    return str('%02d' % datetime[1])
def day(datetime):
    return str('%02d' % datetime[2])
def hour(datetime):
    return str('%02d' % datetime[4])
def minute(datetime):
    return str('%02d' % datetime[5])
def second(datetime):
    return str('%02d' % datetime[6])
def timestamp(datetime):
    date_string = '-'.join([year(datetime), month(datetime), day(datetime)])
    time_string = '-'.join([hour(datetime), minute(datetime), second(datetime)])
    return "T".join([date_string, time_string])
def create_filename(datetime, ext=".jpg"):
    return timestamp(datetime) + "_capture" + ext
def blink(led_number, sleep_time = 300):
    pyb.LED(led_number).on()
    pyb.delay(sleep_time)
    pyb.LED(led_number).off()

def parse_date(data):
    # date will come in a string with (datetime object)"
    data = data.replace("(", "")
    data = data.replace(")", "")
    # split using the comma and transform the string into integers
    timestamp = [int(x) for x in data.split(",")]
    # the idea is to return a tupple we could give to the rtc
    timestamp = tuple(timestamp)
    return (timestamp)

def write_binary_time(string):
    # mind that the string will be binary, hence "wb"
    # mind the '/', without it it will write to the PC instead of the openmv SD card
    f=open('/binary_time.txt','wb')
    f.write(string)
    f.close()

def write__time(string):
    # mind the '/', without it it will write to the PC instead of the openmv SD card
    f=open('/time.txt','w')
    f.write(string)
    f.close()


# micropython does not have decode, this is a hack
def decode(binary_string):
    return "".join([chr(char) for char in binary_string])


sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
rtc = pyb.RTC()
from pyb import USB_VCP
usb = USB_VCP()

blink(3)
pyb.delay(500)
blink(3)

while(True):
    pyb.LED(3).on()
    clock.tick()
    data = usb.read()
    if data != None:
        if len(data) >= 4:
            # we want to check that starts with date and last element is ")"
            if data[:4] == b'date':
                pyb.LED(3).off()
                # now we parse the time
                time_stamp = data[5:]
                write_binary_time(time_stamp)
                # now decode into string
                time_stamp = decode(time_stamp)
                # and transform to tuple
                tuple_time = parse_date(time_stamp)
                # set the clock!
                rtc.datetime(tuple_time)
                blink(2)
                pyb.delay(100)
                blink(2)
        else:
            continue
    else:
        continue

Python side (Ubuntu)

import sys, serial, struct
import datetime
import time

port = '/dev/ttyACM0'
# port might be having issues, like being busy/delayed
prort_ready = False
while(prort_ready is False):
	try:
		sp = serial.Serial(port, baudrate=115200, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,
    	        xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=False)
		# break
		prort_ready = sp.isOpen()
	except:
		print("Port busy or unplugged, retrying in two seconds")
		time.sleep(2)
		pass

while(True):
	if sp.isOpen() == True:
		sp.setDTR(True) # dsrdtr is ignored on Windows.
		#print('Sending command')

		# get the date 
		now = datetime.datetime.now()#.isoformat()
		# we send subsecond 0 for simplicity, we don't need to be THAT accurate
		# year, month, day, weekday, hour, minute, second, subsecond
		message = (now.year, now.month, now.day, now.isoweekday(), now.hour, now.minute, now.second, 0)
		# paste date so we can parse
		message = "date: " + str(message)
		print(message.encode())
		sp.write(message.encode())
		time.sleep(1)

I understand your pain. However, it wasn’t a design goal. It can just do it.

Sigh, so, the IDE could technically do this for you. It’s not hard for us to add a command where the IDE tells the camera what time it is.

If you would like this please add a bug in GitHub for it and it may be added. Note that generally we are quite busy with many other things so unless you are hounding in us getting this done it may take a long time to get worked on however.

Setting the time with the IDE is possible but it still won’t help with the issue of maintaining the time…It’s just much easier if you use NTP (with our WiFi shield or ESP) or use an external I2C RTC. I found cheaper alternatives on ebay ($0.5-$1.5 range) ex: 2PCS I2C RTC DS1307 AT24C32 Real Time Clock Module For AVR ARM PIC NEW | eBay
I think in a future revision, we could break out VBAT on a pin and add a solder jumper so you can cut the VBAT trace and connect it to an external battery.

Thank you both. Your work is really great!

I think that for anybody running the cam out of the IDE, setting the cam clock would be nice. I opened an issue in GitHub, I understand you guys have other stuff of higher priority, but just for keeping it there.
It’s true that relying on the IDE does not solve the keeping time option. I need to deploy this ASAP and still waiting for the lepton 3.5 to ship…but will explore the real time clock option for later. Since my application needs a “begin trigger” I would anyhow need to run this connected to a computer (on-site raspberry Pi) so my hack can bypass the need for keeping time when unplugged. Without a trigger I would just save a bunch of worthless pictures and create a mess for myself to clean later.

Hi, I am sorry this is really a basic question. I am trying to connect my pc to openMV H7 cam with USB_VCP. I followed the link on Github about USB VCP.
I saved the uncommented code in this file in the main.py file and ran it on openMV cam. And I ran the commented part in python 3 on cmd.
But I have an error
‘’ serial.serialutil.SerialException: could not open port ‘/dev/ttyACM0’: FileNotFoundError(2, 'The system cannot find the path specified" on python side.
Could you please guide me, what should I do next?

The camera is not named ACM0. Please check your serial ports on the Mac. E.g. do:

ls /dev/tty*

I am sorry I forgot to mention that I am working on windows. When I checked for the serial port it says COM4 and i changed the name and the error I am getting is
" serial.serialutil.SerialException: could not open port ‘COM4’: PermissionError(13, 'Access is denied."

Is OpenMV IDE connected to the camera at the same time? It cannot be to use the VCP port.

Thank you. The code is working now. It says ‘sending command’.
I have one more question.
Now what should I do to transfer images from cam to pc?

Hi, we built a new library to do all this:

That trace/jumper/pin for VBAT would be very helpful. I am having to add only two things to the H7 Plus for my project: a reset button and a DS3231 module.