Couple questions about the H7

Discussion related to "under the hood" OpenMV topics.
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Couple questions about the H7

Postby ElektronsLibres » Fri Mar 06, 2020 12:22 pm

Hello! i have couple questions:

1st: i want to set a autostart when the camera is powered; i already svae in the camera and reseted and still it doesn't start, did i miss something ?

2nd: i also want to get the output of the program by a usb serial port, in a python script i wondered how to do that ?

Thanks!
GL-ITC
Posts: 28
Joined: Tue Feb 25, 2020 7:09 am

Re: Couple questions about the H7

Postby GL-ITC » Fri Mar 06, 2020 12:54 pm

I am not as qualified to answer the first question as the admins (Kwagyeman and iabdalkader) are but as regards sending images via USB serial port see this discussion: https://forums.openmv.io/viewtopic.php?f=6&t=1764 it should help with the basics. If you can be more specific with your issues I may be able to assist you more with your second question.
User avatar
kwagyeman
Posts: 3985
Joined: Sun May 24, 2015 2:10 pm

Re: Couple questions about the H7

Postby kwagyeman » Fri Mar 06, 2020 1:02 pm

1) Just do Tools->Save script to OpenMV Cam . In the IDE.

2) when you print() the data is output to the serial port if any program opens the serial port.
Nyamekye,
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Sat Mar 07, 2020 4:15 am

1) Acutally I can't the button is blocked... EDIT : I updated my IDE and it worked ! Thank you !

2) But how do i open the USB serial port in a python program ?
GL-ITC
Posts: 28
Joined: Tue Feb 25, 2020 7:09 am

Re: Couple questions about the H7

Postby GL-ITC » Sat Mar 07, 2020 6:25 am

Install the pyserial package on the host system and do the following to find the USB port the openmv board is attached to, create a serial port object and then check that it is open. This script assumes there is only one openmv board attached to the host system that is running your python script.

Code: Select all

import serial

port = ''
for device_name in ports.comports():
	if 'OpenMV' in str(device_name): #Need to convert device name to str as its a linux sysFS object by default
		port = device_name.device
            	print(device_name.device)
        



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)

if sp.isOpen() == True:
            sp.setDTR(True)
            print('Serial Port is Open')
           # Add your write and receive logic in here.
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Sat Mar 07, 2020 7:21 am

But I saw a question on the forum but I didn't quite understood the answer basically I want to get the data's that appears in the serial terminal in the IDE in a python program beside, is there any way to do this?
GL-ITC
Posts: 28
Joined: Tue Feb 25, 2020 7:09 am

Re: Couple questions about the H7

Postby GL-ITC » Sat Mar 07, 2020 7:36 am

Ok so first of all you can't run a python program that tries to access the serial port if the IDE is already connected to the camera. The camera must be disconnected form the IDE if you want another python program to communicate/get data from the serial port with the camera.

Can you be more specific what you are trying to do and possibly post the code you are currently having difficulty with. This will help everyone to debug your issue.
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Sat Mar 07, 2020 2:30 pm

Yes of course ! I binded the autorun, i don't need the IDE anymore to run my micro pytohn program.
Here's my micro python program,
So my point is to implement the 'c' variable,in another python program which contains a tuple that constitute coordinates of a point ( here the center of an orange area), by using a USB serial port.

Code: Select all


import sensor, image, time, pyb

red_led = pyb.LED(1)
green_led = pyb.LED(2)
blue_led = pyb.LED(3)
ir_leds = pyb.LED(4)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

clock = time.clock()

seuils_Lab = (60, 85, 18, 50, 30, 70) # seuil de la couleur balle
while True:
    img=sensor.snapshot()
    blobs = img.find_blobs([seuils_Lab])
    red_led.on()
    green_led.off()
    if blobs:
        for b in blobs:
            tmp=img.draw_rectangle(b[0:4])
            c=(b[5], b[6])
            print(c)
            tmp=img.draw_cross(b[5], b[6])
            red_led.off()
            green_led.on()
GL-ITC
Posts: 28
Joined: Tue Feb 25, 2020 7:09 am

Re: Couple questions about the H7

Postby GL-ITC » Sat Mar 07, 2020 3:41 pm

To send the c variable to another python program, you need to set up your other python program to receive data via the serial port. You could send the c variable as a JSON object. I haven't got the OpenMv board with me at the minute so I cannot verify if the code will work, but it think this should do it if your using a USB. Note: You need to install the pyserial package for the other program to work on your system


main.py

Code: Select all

import sensor, image, time, pyb, ujson
from pyb import USB_VCP

usb = USB_VCP()

red_led = pyb.LED(1)
green_led = pyb.LED(2)
blue_led = pyb.LED(3)
ir_leds = pyb.LED(4)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

clock = time.clock()

seuils_Lab = (60, 85, 18, 50, 30, 70) # seuil de la couleur balle
while True:
    data = {}
    img=sensor.snapshot()
    blobs = img.find_blobs([seuils_Lab])
    red_led.on()
    green_led.off()
    if blobs:
        for b in blobs:
            tmp=img.draw_rectangle(b[0:4])
            c=(b[5], b[6])
            
            data['c'] = c
            data = ujson.dumps(data)
            usb.send(data)
            
            tmp=img.draw_cross(b[5], b[6])
            red_led.off()
            green_led.on()

other python program:

Code: Select all

import serial, json
import serial.tools.list_ports as ports

#Finds the port the openmv board is connected to
port = ''
    for device_name in ports.comports():
        if 'OpenMV' in str(device_name): #Need to convert device name to str as its a linux sysFS object by default
            port = device_name.device
            

#Create the serial port
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)


if sp.isOpen() == True:
            sp.setDTR(True)
            data = sp.read(sp.inWaiting()).decode('utf-8')
	    data = json.loads(data)
	    print(data['c'])
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Sun Mar 08, 2020 1:19 am

Ok I'll have a look that ' you very much !
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Mon Mar 09, 2020 8:08 am

I have tried both your programs for the micro python one it gives an error for line 32 : "TypeError: 'str' object doesn't support item assignment", i don't really get it.

And so on since the cam stops running the python program returns also an error but it sounds logical
GL-ITC
Posts: 28
Joined: Tue Feb 25, 2020 7:09 am

Re: Couple questions about the H7

Postby GL-ITC » Mon Mar 09, 2020 8:15 am

Which line shows up as line 32 in your text editor/IDE?
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Mon Mar 09, 2020 1:29 pm

Hey!
This one shows as line 32 : "data['c'] = c"

I got something barely running :

Code: Select all

import sensor, image, time, pyb, ujson
from pyb import USB_VCP

usb = USB_VCP()

red_led = pyb.LED(1)
green_led = pyb.LED(2)
blue_led = pyb.LED(3)
ir_leds = pyb.LED(4)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

clock = time.clock()

seuils_Lab = (60, 85, 18, 50, 30, 70) # seuil de la couleur balle
while True:
    data = {}
    img=sensor.snapshot()
    blobs = img.find_blobs([seuils_Lab])
    red_led.on()
    green_led.off()
    if blobs:
        for b in blobs:
            tmp=img.draw_rectangle(b[0:4])
            c=(b[5], b[6])
            print(c)
            data = ujson.dumps(c)
            usb.send(data)

            tmp=img.draw_cross(b[5], b[6])
            red_led.off()
            green_led.on()
but running my other python script :

Code: Select all

import serial, json
import serial.tools.list_ports as ports

#Finds the port the openmv board is connected to
port = '/dev/ttyACM0/'
for device_name in ports.comports():
        if 'OpenMV' in str(device_name): #Need to convert device name to str as its a linux sysFS object by default
            port = device_name.device
            

#Create the serial port
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)


if sp.isOpen() == True:
            sp.setDTR(True)
            data = sp.read(sp.inWaiting()).decode('utf-8')
            if data:
                data = json.loads(data)
                print(data)
it makes sometimes an error sometimes not
GL-ITC
Posts: 28
Joined: Tue Feb 25, 2020 7:09 am

Re: Couple questions about the H7

Postby GL-ITC » Mon Mar 09, 2020 2:00 pm

On the code you are running on the openmv board you can remove the print statement because that outputs data to the serial/usb port. See if that fixes the error your getting now
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Mon Mar 09, 2020 2:15 pm

Alright it works! thank you very much!
Also i had another question i noted that on this program sometimes tons of blobs are found i know about the largestblob line but i can't manage to find the largest blob and keeping this only one would you have any reccomandation ?
GL-ITC
Posts: 28
Joined: Tue Feb 25, 2020 7:09 am

Re: Couple questions about the H7

Postby GL-ITC » Mon Mar 09, 2020 2:31 pm

Sorry just to be clear, you want to send the largest blob back as well or am I misunderstanding you?
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Mon Mar 09, 2020 3:12 pm

Sorry for being unclear
As you can see for each blob seen my micro python program draws a rectangle around the orange area, sometimes it detects a major area and some smaller areas so on I would like to keep only the major one
GL-ITC
Posts: 28
Joined: Tue Feb 25, 2020 7:09 am

Re: Couple questions about the H7

Postby GL-ITC » Tue Mar 10, 2020 5:40 am

You could use the blob.pixels() function to find how many pixels are in each blob and then just send back/display the one with the most pixels in it.
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Sat Mar 14, 2020 10:00 am

Alright everything's alright ... until i realised how json worked ; i wasn't famliar with this library so actually i get a string chain of like '[29 , 128]' (this is just an example of coordinates) and if i convert it as a tuple i get a long stringle tuple of each characters, i've been trying hard to get it as an integer tuple : (29, 128) in the receiver program, and the only idea i came up with is a for loop looking one by one each character of the long tuple but that is kind of hideous, is there anyway to directly send a tuple ? Here are the programs:

Micro python OpenMV cam:

Code: Select all

import sensor, image, time, pyb, ujson
from pyb import USB_VCP
usb = USB_VCP() 
red_led = pyb.LED(1)
green_led = pyb.LED(2)
blue_led = pyb.LED(3)
ir_leds = pyb.LED(4)

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

clock = time.clock()

seuils_Lab = (60, 85, 18, 50, 30, 70) # seuil de la couleur balle
while True:
    a=0
    n=0
    data = {}
    img=sensor.snapshot()
    blobs = img.find_blobs([seuils_Lab])
    red_led.on()
    green_led.off()
    if blobs:
        for b in blobs:
            if b.pixels()>>a:
                a=b.pixels()
                n=b
        tmp=img.draw_rectangle(n[0:4])
        c=(n[5],n[6])
        data = ujson.dumps(c) #création du fichier micro json avec la variable que tu veux envoyer, en argument
        usb.send(data) #envoie de la variable
        tmp=img.draw_cross(n[5], n[6])
        red_led.off()
        green_led.on()
ANd receiver program :

Code: Select all

import serial, sys,json, time
import serial.tools.list_ports as ports


port = '/dev/ttyACM1/'
for device_name in ports.comports():
        if 'OpenMV' in str(device_name): 
            port = device_name.device
            

sp = serial.Serial(port, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,xonxoff=False, rtscts=False, stopbits=serial.STOPBITS_ONE, timeout=None, dsrdtr=False)
if sp.isOpen() == True:
            sp.setDTR(True)
            time.sleep(0.5)
            data = sp.read(sp.inWaiting()).decode('utf-8') 
            print(data)
            sys.stdout.flush() 
User avatar
kwagyeman
Posts: 3985
Joined: Sun May 24, 2015 2:10 pm

Re: Couple questions about the H7

Postby kwagyeman » Sat Mar 14, 2020 11:42 am

Sending via json is the best method. Note that the str() representation of all OpenMV objects is valid json. You should just send json output and then decide that received json in the other program.
Nyamekye,
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Sat Mar 14, 2020 12:10 pm

Thnkas for the fast reply but i managed to figure out something just after posting ! Actually sometimes the cam send some kind of bugged datas like "[9" and nothing after or just "]' so in my receiver program i just made :

Code: Select all

 time.sleep(0.2)
            data = sp.read(sp.inWaiting()).decode('utf-8') 
            if len(tuple(data))>=5:
                data= json.loads(data)
                print(data[0])
                sys.stdout.flush() 
NOTE: the time.sleep(0.2) is also in the cam micro python program, if those datas are different the json.loads will send an error.
User avatar
kwagyeman
Posts: 3985
Joined: Sun May 24, 2015 2:10 pm

Re: Couple questions about the H7

Postby kwagyeman » Sat Mar 14, 2020 12:19 pm

If you are getting weird bugs like that it's a programming error on the camera. The last user who had an issue like that was referencing objects that had gone out of scope and we're being garbage collected. I noticed you just do a general read all command on the PC side which isn't really the most robust thing to do.
Nyamekye,
ElektronsLibres
Posts: 12
Joined: Fri Mar 06, 2020 11:16 am

Re: Couple questions about the H7

Postby ElektronsLibres » Sat Mar 14, 2020 12:28 pm

What do you mean by a general reading on the PC side (sorry i'm new to computing, i've been doing it for a year or a bit less maybe i have dumb questions )
User avatar
kwagyeman
Posts: 3985
Joined: Sun May 24, 2015 2:10 pm

Re: Couple questions about the H7

Postby kwagyeman » Sat Mar 14, 2020 2:12 pm

The code on the PC side to read data doesn't have any concept of data framing. It just grabs whatever data has been received and then processes that. Which means you have no idea how many packets you grabbed in one go and etc. So, when you are trying to parse the data you have no idea what's at the head of the buffer or etc.

If you want to make the data transfer better you'd compute the length of bytes to send on the H7 side, send that length count which is of a fixed size. Then the PC side receives that length, and the reads the data count expected afterwards.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 15 guests