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: Timestamp filenames - OpenMV Products - OpenMV Forums 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.
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.
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.
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?
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.
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.
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()
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
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:
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'])
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
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 :
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)
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
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 ?
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
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:
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 :
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()
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.