Script autostart

Discussion related to "under the hood" OpenMV topics.
DCON81
Posts: 9
Joined: Wed Nov 01, 2017 4:14 pm

Script autostart

Postby DCON81 » Sun Mar 25, 2018 6:41 am

I've no sd card.
I want the script to start when board powered.
In the IDE i've done "Save open script to OPENMV Cam" but no autostart ...

I'm missing something ?
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Sun Mar 25, 2018 1:54 pm

Hi, after saving the script to the OpenMV Cam please click the reset OpenMV Cam button right under the save script button. The script should be flushed to the camera then and it should auto-start.
Nyamekye,
DCON81
Posts: 9
Joined: Wed Nov 01, 2017 4:14 pm

Re: Script autostart

Postby DCON81 » Wed Mar 28, 2018 10:56 am

Issue was solved updating the IDE and updating the firmware of the cam.
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Thu Jun 28, 2018 5:15 am

Hello! I connected OpenMV M7 to my Raspberry Pi... I want to open OpenMV IDE using python scripts, CONNECT to the camera, and START running the script... Does anyone know how to do it? Thanks
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Thu Jun 28, 2018 12:00 pm

Hi, are you talking about scripting up OpenMV IDE? Um, so, if you launch OpenMV IDE via the command line. You can pass it a file path as it's first argument and it will open that file. Then, if you can emulate pressing ctrl+e and then ctrl+r it will run the script.

Alternatively, you can save a script on the camera permanently and then launch the IDE using command line options which will allow you to create terminal windows to show the debug output for demo purposes. I built this feature into the IDE for folks. Does this sound like what you want? The script needs to be on the camera before hand... but, otherwise, you can launch the IDE and have it display debug output (without the text editor appearing and other buttons) via the command line.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Fri Jun 29, 2018 1:36 am

Thank you for the prompt reply sir kwagyeman!

Well what I want to happen is to automatically run the python script for the camera, not necessarily open the OPENMV IDE... Thanks!
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Fri Jun 29, 2018 1:55 am

1. Open the script in the IDE.
2. Go to Tools -> Save Script to OpenMV Cam
3. Go to Tools -> Reset OpenMV Cam

The script will now start when the camera starts.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Fri Jun 29, 2018 2:32 am

Thank you again for the prompt reply.... That information is very useful sir, however my code is still a work in progress... If "Save Open Script to OpenMV Cam" now and do it again with newly updated code, which code will it run when I boot it up?

Will it follow the most recent one? Thanks!
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Fri Jun 29, 2018 2:36 am

Yes. All this does is create/update a file called main.py on the OpenMV Cam. All micropython boards run a main.py script on startup.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Fri Jun 29, 2018 2:43 am

Hello! Last questions... I tried it out and it did run the autostart... However:

1. Is there a way that I may be able to see the images it sees?

2. I cant seem to stop the autostarted script since it boots up. How do I stop it without turning it off?

Thanks
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Fri Jun 29, 2018 2:47 am

Yes,

Go to Tools:

Open Terminal -> New Terminal -> Serial Port -> Select the right comm port -> Any baud rate (default is fine).

This pops open a terminal which will show you the

Code: Select all

print()
output from the camera. If you want to also see what the camera sees in this view you need to add

Code: Select all

print(img.compressed_for_ide(quality=50), end='')
to your script near the end of your while loop to tell the camera to send serial data over the USB port.

If you connect to the OpenMV Cam using the normal connect button the auto started script stops. If you'd like to permanently stop the auto start behavior delete the main.py file on your OpenMV Cam.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Fri Jun 29, 2018 3:01 am

Ok! Thank you sir!
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Fri Jul 06, 2018 3:55 am

Hello! I'm sorry for the additional questions... I'm currently attaching my OPENMV cam to my laptop and I'm running a GUI on python as can be seen on the attached image.

In the attached image, you can see the GUI. When the users clicks "enable camera", I would like the openmv to start its facial recognition script without having to open the OPENMV IDE and connecting to it.

Is there a way to run the scripts of the code by triggering it in a python script (GUI)? Thanks
Attachments
attach.png
attach.png (9.43 KiB) Viewed 1418 times
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Fri Jul 06, 2018 1:12 pm

Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Mon Jul 09, 2018 2:35 am

Hello! Sorry I'm kinda having a hard time trying to understand that code... Do you mind helping me out a bit on how to set it up?

Let's say I connected my cam to my linux laptop in this port: "ttyACM0"

and have to run the script in this script: "home/pythonfiles/GUI/GUI_tester.py"

any help would be appreciated. Thanks!
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Mon Jul 09, 2018 1:33 pm

Ah, so, the script uses the /dev/openmvcam port which is created only if you install the openmv udev rules. In the top level of the OpenMV repo you need to install the udev rules. Once you do that then on Linux the camera will appear as a /dev/openmvcam device.

Please let me know if this works for you. Then, you can just run that script I linked to and provide it with a path to the script you want the camera to run.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Tue Jul 10, 2018 12:47 am

How do I do that exactly? I'm sorry I'm still new at this... I cloned the openmv repository... but I don't know how to install udev.
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Tue Jul 10, 2018 1:10 am

Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Tue Jul 10, 2018 3:50 am

Yes I did that and donwloaded it for RPi however I only have these files in the openmv/openmvide directory (see image attached)... there's no udev folder...
Attachments
OPEN.png
OPEN.png (112.6 KiB) Viewed 1387 times
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Wed Jul 11, 2018 1:52 am

Go to share/qtcreaotr/pydfu.

However, if you just run the ./setup.sh script it takes care of the install for you.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Wed Jul 11, 2018 3:40 am

I did that before.. I tried it again but still no udev folder

I did:

cd openmv/openmvide

./setup.sh

on the terminal.. I also tried double clicking it an executing it, but nothing changed
Attachments
POST.png
POST.png (347.31 KiB) Viewed 1369 times
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Wed Jul 11, 2018 12:07 pm

A udev folder will not appear. However, if you do "ls /dev" you should now see a device called "/dev/openmvcam".
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Thu Jul 12, 2018 1:36 pm

Hello! THank you! I will try that later. In the meantime, is there a way to send each frame the camera takes in real time to display in this GUI in Raspberry Pi?

maybe using the USB_VCP.send() function to send to the RPi through the usb port and the image.compress() function? Any help would be appreciated. Thank you!
Attachments
Untitled.png
Untitled.png (18.67 KiB) Viewed 1348 times
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Thu Jul 12, 2018 3:35 pm

Yes, just do:

Code: Select all

print(img.compress(quality=90),end="")
You'll get a JPG byte stream. You may wish to send the size of the jpg byte stream first however.

Code: Select all

img.compress(quality=90)
print(str(img.size()) + \n)
print(img,end="")
You'd then scanf to get the size in bytes and then read that many bytes next.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Fri Jul 13, 2018 12:26 pm

Thank you! I see the dev/openmvcam did appear on my /dev.. I'm sorry for the additional questions, but I'm attaching my openmv to a gimbal and it doesnt really fit unless it is attached horizontally which means the view of the camera would be rotated about 90 degrees... I found the image.rotation_corr(z_rotation=90) but unfortunately, the rotated image was cropped because of the rotation... I'm currently using a 240x160 frame size if that's helpful... I also tried zooming to get rid of the black margins of the new rotated image (caused by the rotation), but it gets too zoomed

Is there a way to configure the sensor or change the image so that it is rotated 90 degrees without affecting the dimensions of the image or getting the image cropped. Thanks again for the help!
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Sat Jul 14, 2018 3:07 pm

As of right now there is not. However, if you set the resolution to something like QVGA and then set_windowing to a square res and then rotate that and then use an ROI of 240x160 you should get the results you want on the image.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Sat Jul 14, 2018 3:16 pm

yes... i thought of using a square dimensions too so that rotating it wouldn't be a problem... but I saw in the comments of the facial detection examples code that the 240x160 was the best for facial recognition... What can you suggest to be the best square dimensions for facial recognition... Thanks!
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Sat Jul 14, 2018 5:45 pm

Hi, that was the case of the M4. Not anymore. Um, just make sure the res is some form of 3:2.
Nyamekye,
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Sat Jul 14, 2018 5:47 pm

So, the method takes an ROI. Make the ROI a 3:2 area (240x160).
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Sun Jul 15, 2018 4:10 am

Hello! I'm sorry, I had no idea how to set the ROI to 240x120 after the image.snapshot() and image.rotation_corr() have been called... I couldn't find a method that set the windowing to 240x120... I tried image.copy() and printed it on the Open Terminal, but I didn't quite get the dimensions I needed... Thanks
Attachments
Untitled.png
Untitled.png (272.58 KiB) Viewed 1315 times
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Sun Jul 15, 2018 1:13 pm

Find features takes an ROI: Pass roi=(0, 0, 240, 120) to find_features. Note that the ROI is in the upper left of the image. You'll want to adjust the x/y values to fix that if you want the ROI to start elsewhere.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Mon Jul 16, 2018 4:16 am

I tried it but unfortunately, I added the roi=[0,0,240,160] to the find_features() method, but the image is still cropped...
Attachments
Untitled.jpg
Untitled.jpg (296.88 KiB) Viewed 1306 times
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Mon Jul 16, 2018 1:44 pm

The ROI passed to find features is not displayed. It just tells the method where to work on in the image.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Sun Jul 22, 2018 11:34 am

Hello! I bought multiple of these openmv cameras, and I wanted to use them in different ways... Since I attached my openmv cam to a gimbal of a drone, it is extremely difficult for the gimbal to carry the camera with a usb connected to it (used for real-time image streaming)... so I wanted to ask if there is a way to send the image.snapshot() images over the UART port so that the camera would no longer be heavy?

Thanks
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Sun Jul 22, 2018 1:18 pm

Hi, just do:

uart.write(img.compress())

On a valid uart object (see example scripts to make one). Or, alternatively, you can do img.compressed_for_ide() if you're sending the data back to the IDE. Note that the data rate is so high that USB is really the other thing that cuts it for sending video. WiFi may also.

Ibrahim is finally working on WiFi programming support.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Mon Jul 23, 2018 9:34 am

Thanks! Will try that soon! Anyway, I'm trying to determine the distance of a human from the camera through the image it sees, and I've tried doing some experiments on determining the focal length of the camera, however my experiments were not that consistent... Any chance you guys know what the focal length of the camera is? I'm currently using the OpenMV M7 camera... Thanks
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Mon Jul 23, 2018 1:14 pm

All the lens specs are on the product page. It's 2.8mm.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Tue Jul 24, 2018 5:38 am

Hello! I tried streaming the image through the USB VCP class and the byte size of the image through the UART class as seen on the first image... But when I try to read the amount of bytes in the usb connection (as seen in the 2nd image), it says the buffer is not large enough... I searched for the specs of the camera and got 300KB++ buffer size... as seen on the 2nd image, the byte size of each snapshot is around 2000+ bytes, so I'm not sure what's going on... I have a 16GB sd card attached to the openmv. Can you help me out? Thanks!
Attachments
POST.png
POST.png (168.05 KiB) Viewed 1195 times
post2.png
post2.png (205.64 KiB) Viewed 1195 times
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Tue Jul 24, 2018 12:07 pm

Hi, the error is coming from your Pi. Not the camera. The code on the camera looks fine.

The issues is the frombuffer call. Please note that the image is in JPG format. So:

Code: Select all

import os
import io
import Image
from array import array

image = Image.open(io.BytesIO(image_data))
image.show()
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Wed Jul 25, 2018 7:33 am

Hello! I tried doing what you suggested with the following revisions of the code... So the serial port delivers the length of the bytes of the image and the USBVCP delivers the image in bytes (as seen in the first picture)... The python script receiving this takes the bytes and stores it in the string... I tried to print these bytes in the openmv ide and got that the bytes were:

\xff\x00\ and further stuff like this

However these characters seem to be stored in a string instead of a byte object as seen on the 3rd picture... I tried adding the Image.open(io.BytesIO()) but it got me an error saying "cannot identify image file <_io.BytesIO object at 0x75784960>"

and I also tried replacing it with Image.open(io.StringIO()) but still nothing...

Sorry for the inconvenience.. Thanks!
Attachments
POST.png
POST.png (130.46 KiB) Viewed 1167 times
POST 2.png
POST 2.png (152.07 KiB) Viewed 1167 times
POST 3.png
POST 3.png (441.45 KiB) Viewed 1167 times
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Thu Jul 26, 2018 1:10 am

Hi, I got the answer I posted from here:

https://stackoverflow.com/questions/329 ... -byte-file

Unfortunately, I can't really debug this for you more than to point you in the right place. As long as you received the right number of bytes on the Pi side you should have a valid image file that is a jpg.

From the debug output it looks like you received the image fine. So, jpg images always end with FFD9 and begin with FFD8. So, it looks like you received the full image.

Mmm, as a test try writing the binary byte object to disk and then open it.

Code: Select all

file = open("image.jpg", "wb")
file.write(image_data[0])
file.close()
The file should be in binary on disk... if you notice that the file grew in size from what you script printed out then that means you're silently converting the bytes from binary form to text strings of what the binary data is...
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Mon Aug 06, 2018 10:45 am

Hello! Sorry for the late response, but I had it working already. Thanks for the many help!... The serial.read(# of bytes) didnt really read accurately so I just assigned a value bigger than the number of bytes of the image...

I was able to get the right data by searching for the start bytes: '\xff\xd8'
and end bytes: '\xff\xd9'
of the jpeg data...

Anyway... I tried just streaming the images from the openMV to the Raspberry Pi, and let the face detection be done by the Raspberry Pi through OpenCV, however its very slow in image processing (even though I used LBP cascade over haar).. I only get a maximum of 5 FPS from the face detection.. so I tried making the OpenMV do the face detection again... But the problem is that the face detection of the OpenMV (the one in the examples) seem to be unable to detect a face about 2m away...

I'm currently using the QVGA resolution and windowing of 240x240... Is there any way that the openmv be able to do a facial detection at least 2m away? Or maybe a different facial detection algorithm? Thanks
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Mon Aug 06, 2018 10:27 pm

Hi, by chaining the scale value you change how many scales the algorithm tries.

https://github.com/openmv/openmv/blob/m ... aar.c#L108

Basically, by making the value closer to 1.0 you make the algorithm scale more slowly down in size.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Tue Aug 07, 2018 12:17 am

Hello sir! Thank you for the help! However I encountered another problem. I have attached one of my cams to a gimbal as seen on the attachment... however my USB connection to the RPi gets cut off suddenly. Do u have any suggestions for this? Thanks
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Tue Aug 07, 2018 1:10 am

What?
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Tue Aug 07, 2018 6:00 am

Sorry for that previous reply.. Please disregard this... Is there a way I can run the python script of the OpenMVIDE using the pyopenmv.py through the serial connection?

I tried changing the /dev/openmvcam port of the pyopenmv.py to /dev/ttyS0 which is the serial port, but it didn't work. Thanks
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Tue Aug 07, 2018 12:40 pm

Hi, you have to install udev drivers for the /dev/openmvcam device to appear. That said, this isn't actually needed except if you want to use DFU. All you need to do is change the serial port to the /dev/ACM whatever port that appears when you connect the camera.
Nyamekye,
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Wed Sep 19, 2018 4:57 am

Hello! Thank you for the many many help... I've accomplished a lot of projects and features with this camera!

I was wondering, by the way, is there any way I could import my own classifier xml file. Let's say I want to do Upper Body Detection using an upper body xml, how would I import that? I couldn't find a frontal xml file used for your facial detection, so I don't really know how the classifiers are imported into the code on OpenMV. Thank you!
levinpqr
Posts: 26
Joined: Thu Jun 28, 2018 5:09 am

Re: Script autostart

Postby levinpqr » Wed Sep 19, 2018 6:25 am

Hello! I created a cascade file using the cascade_convert.py I found online. So, I placed it in the /home/openmvide folder. I tried to run the code (using the new cascade) as seen on the attached image but it had an error that said it can't find the path. Sorry I know its a basic question... I tried to use its whole path which is /home/uavdlsu/openmvide/haarcascade_mcs_upperbody.cascade but it also did not work.. am I doing something wrong? Thank you
Attachments
Untitled.png
User avatar
kwagyeman
Posts: 2205
Joined: Sun May 24, 2015 2:10 pm

Re: Script autostart

Postby kwagyeman » Wed Sep 19, 2018 11:26 am

Let me ask Ibrahim.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests