Problem writing txt file

Discussion related to "under the hood" OpenMV topics.
Patrick
Posts: 5
Joined: Fri Dec 07, 2018 8:53 am

Problem writing txt file

Postby Patrick » Mon Jan 14, 2019 10:27 am

Hi all, I have a problem with the following script. I cannot write more than once to the txt file. I tried to disconnect and connect the open mv camera several times, but nothing seems to work. I can recognize the pattern images, but really it is impossile to me writing to the txt file.
Thanks for any indication and help.

Code: Select all

# Template Matching Example - Normalized Cross Correlation (NCC)
#
# This example shows off how to use the NCC feature of your OpenMV Cam to match
# image patches to parts of an image... expect for extremely controlled enviorments
# NCC is not all to useful.
#
# WARNING: NCC supports needs to be reworked! As of right now this feature needs
# a lot of work to be made into somethin useful. This script will reamin to show
# that the functionality exists, but, in its current state is inadequate.

import time, sensor, image, pyb,os
from image import SEARCH_EX, SEARCH_DS

# Reset sensor
sensor.reset()

# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
sensor.set_framesize(sensor.QQVGA)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)

# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
template = image.Image("/template.pgm")
template2 = image.Image("/template2.pgm")
clock = time.clock()

# Run template matching
while (True):
      clock.tick()
      img = sensor.snapshot()

    # find_template(template, threshold, [roi, step, search])
    # ROI: The region of interest tuple (x, y, w, h).
    # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
    # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
    #
    # Note1: ROI has to be smaller than the image and bigger than the template.
    # Note2: In diamond search, step and ROI are both ignored.
      f = img.find_template(template, 0.40, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
      g = img.find_template(template2, 0.60, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
      if f:
            img.draw_rectangle(f,thickness=1)
            t = pyb.millis()
            print('Found template 1',(f[0]+(f[2]/2)),(f[1]+(f[3]/2)))
            a=f[0]+(f[2]/2)                            # get coordinate data
            f = open('logdata.txt', 'w')
            f.write('Found_template_1',int(a))
            f.close()
      if g:
            img.draw_rectangle(g,thickness=1)
            t = pyb.millis()
            print('Found template 2',(g[0]+(g[2]/2)),(g[1]+(g[3]/2)))
            b=g[0]+(g[2]/2)                            # get coordinate data
        #log = open('1:/File.csv', 'w')
        #log.write('{},{}\n'.format(t,a))           # write data to file
        #log.close()
      print('Nothing')
      
Last edited by iabdalkader on Mon Jan 14, 2019 3:57 pm, edited 1 time in total.
Reason: Fixed formatting
User avatar
kwagyeman
Posts: 3209
Joined: Sun May 24, 2015 2:10 pm

Re: Problem writing txt file

Postby kwagyeman » Mon Jan 14, 2019 12:35 pm

Can you edit your post and use the code button on the text editor above to make your code readable? It will insert code tags into the post.

As for writing the text file. Please note that any desktop OS will not rescan the OpenMV Cam disk after it's plugged in. So, if you create a file on the camera it will not appear on the OS until you reset the camera. Further more. The desktop OSes do not respect the presence of any new file created and will overwrite them if you do any disk activity on the desktop OS and on the cam as the same time. So, if you want to create a file on the camera it's quite tricky. The best is best used for read storage. That said, if no program is accessing the camera, then the camera creates a file on itself, and then resets itself the new file should appear on the OS.
Nyamekye,
User avatar
iabdalkader
Posts: 920
Joined: Sun May 24, 2015 3:53 pm

Re: Problem writing txt file

Postby iabdalkader » Mon Jan 14, 2019 4:00 pm

Also let me just add that you truncate the file, you should use "a" mode:
The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):

``r'' Open text file for reading. The stream is positioned at the
beginning of the file.

``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.

``w'' Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.

``w+'' Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.

``a'' Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.

``a+'' Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.
Patrick
Posts: 5
Joined: Fri Dec 07, 2018 8:53 am

Re: Problem writing txt file

Postby Patrick » Tue Jan 15, 2019 7:30 am

iabdalkader wrote:
Mon Jan 14, 2019 4:00 pm
Also let me just add that you truncate the file, you should use "a" mode:
The argument mode points to a string beginning with one of the following
sequences (Additional characters may follow these sequences.):

``r'' Open text file for reading. The stream is positioned at the
beginning of the file.

``r+'' Open for reading and writing. The stream is positioned at the
beginning of the file.

``w'' Truncate file to zero length or create text file for writing.
The stream is positioned at the beginning of the file.

``w+'' Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at
the beginning of the file.

``a'' Open for writing. The file is created if it does not exist. The
stream is positioned at the end of the file. Subsequent writes
to the file will always end up at the then current end of file,
irrespective of any intervening fseek(3) or similar.

``a+'' Open for reading and writing. The file is created if it does not
exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current
end of file, irrespective of any intervening fseek(3) or similar.
Thank you for your prompt reply. It perfectly worked.
Now I am struggling over sending the data (float values) to an Influx database. Is it possible ? I guess I have to use the socket but I don't know exactly what to do.

Thanks again if you come up with some ideas.
User avatar
kwagyeman
Posts: 3209
Joined: Sun May 24, 2015 2:10 pm

Re: Problem writing txt file

Postby kwagyeman » Tue Jan 15, 2019 11:50 pm

Um, the easiest way to move data is to do http:// get requests to a PHP script that waits for being accessed and then updates the data base. Um, first, I'd write the PHP script and then connect it to the database. If you have cPanel running on your server then you just need to create a php file somewhere that's web accessible. Then create a database in your phpadmin control panel under cpanel. Then to connect to the database do something like this:

https://www.w3schools.com/php/php_mysql_insert.asp / https://www.cloudways.com/blog/custom-p ... tact-form/

Basically, the php script inserts form info into the database. To test write a simple html page that can generate the form request to the php script.

I had to spin up a system like this myself without knowing what to do. It should take you a weekend. How to do all this stuff is quite well written about if you Google a lot so it's very easy to get working.

Then, on the OpenMV Cam side you just need to connect to port 80 via a TCP socket and send an http request over the socket: https://stackoverflow.com/questions/106 ... via-socket - You just need to have like the same type of string. Then the webserver will pass the request to the PHP script which will insert it into the database.
Nyamekye,
Patrick
Posts: 5
Joined: Fri Dec 07, 2018 8:53 am

Re: Problem writing txt file

Postby Patrick » Thu Feb 07, 2019 7:40 am

kwagyeman wrote:
Tue Jan 15, 2019 11:50 pm
Um, the easiest way to move data is to do http:// get requests to a PHP script that waits for being accessed and then updates the data base. Um, first, I'd write the PHP script and then connect it to the database. If you have cPanel running on your server then you just need to create a php file somewhere that's web accessible. Then create a database in your phpadmin control panel under cpanel. Then to connect to the database do something like this:

https://www.w3schools.com/php/php_mysql_insert.asp / https://www.cloudways.com/blog/custom-p ... tact-form/

Basically, the php script inserts form info into the database. To test write a simple html page that can generate the form request to the php script.

I had to spin up a system like this myself without knowing what to do. It should take you a weekend. How to do all this stuff is quite well written about if you Google a lot so it's very easy to get working.

Then, on the OpenMV Cam side you just need to connect to port 80 via a TCP socket and send an http request over the socket: https://stackoverflow.com/questions/106 ... via-socket - You just need to have like the same type of string. Then the webserver will pass the request to the PHP script which will insert it into the database.
I installed Open MV on a Raspberry, from there run a script in Python3 to send the csv file generated by the script in Mycropython (recording the camera output), to an Influx DB.

A problem: the csv file creation happens but I cannot visualize the file unless I disconnect and reconnect the camera. How can I avoid this, so that I can ensure continuity in the file reading ? Thanks again

Code: Select all


import time, sensor, image, pyb, os, utime, machine, usocket
from image import SEARCH_EX, SEARCH_DS

# Reset sensor
sensor.reset()
# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
sensor.set_framesize(sensor.QQVGA)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)

# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
template = image.Image("/ut.pgm")
clock = time.clock()

# Run template matching
while (True):
    clock.tick()
    img = sensor.snapshot()

    f = img.find_template(template, 0.8, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if f:
        img.draw_rectangle(f,thickness=1)
        #print('Found template 1',(f[0]+(f[2]/2)),(f[1]+(f[3]/2)))
        #rtc = pyb.RTC()
        #rtc.datetime()
        now = utime.time()
        tm = utime.localtime()
        a = f[0]+(f[2]/2)  # get coordinate data
        c = (f[1]+(f[3]/2)) # get coordinate data
        h = open('streaming.csv', 'a')
        a = str(a)
        c = str(c)
        h.write('\n')
        h.write(str(now))
        h.write(',')
        h.write(a)
        h.write(',')
        h.write(c)
        h.close()
        print(a)

    print('Nothing')

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

Re: Problem writing txt file

Postby kwagyeman » Thu Feb 07, 2019 12:11 pm

Hi, there's no way to fix this. It's because the OS assumes disk drives can't create files by themselves so the OS will never rescan the flash drive to see if a new file is there. We've really tried to fix this using sys calls with OpenMV IDE but there's nothing we can do. If you want to stream data to an application on the PC you have to use a UART on the camera.
Nyamekye,
Patrick
Posts: 5
Joined: Fri Dec 07, 2018 8:53 am

Re: Problem writing txt file

Postby Patrick » Fri Feb 08, 2019 1:36 am

Thank you very much for your answer.

Regards,
Patrick.

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 46 guests