Writelines for camera

hi. I was wondering if there was a similar thing to the “writelines” code of python on the cam.
I was trying to have the “main.py” code save a bunch of numbers even after the camera has been shut off. so I tried to create files on the cam and save the numbers on the files. the cam is not connected to the laptop , it is connected to a Arduino board.

Could you post the code you’re using so we can get a better idea of what’s going on?

Thank you.

import sensor
import time

clock = time.clock()

r = [(320 // 2) - (50 // 2), (240 // 2) - (50 // 2), 50, 50]

for i in range(60):
img = sensor.snapshot()


the parts in the “{{” and “}}” are my own code

the rest is the “Automatic RGB565 Color Tracking Example” but without the comments

this part work’s

f = open(‘color.txt’,‘r+’).readlines()

for i in range(0,len(f)):
f[i] = int(f[i])

threshold = [f[0], f[1], f[2], f[3], f[4], f[5]]


for i in range(60):
img = sensor.snapshot()
hist = img.get_histogram(roi=r)
lo = hist.get_percentile(0.01)
hi = hist.get_percentile(0.99)
threshold[0] = (threshold[0] + lo.l_value()) // 2
threshold[1] = (threshold[1] + hi.l_value()) // 2
threshold[2] = (threshold[2] + lo.a_value()) // 2
threshold[3] = (threshold[3] + hi.a_value()) // 2
threshold[4] = (threshold[4] + lo.b_value()) // 2
threshold[5] = (threshold[5] + hi.b_value()) // 2
for blob in img.find_blobs(
[threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10
img.draw_cross(blob.cx(), blob.cy())



this is basically what i want it to do


while True:
img = sensor.snapshot()
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_cross(blob.cx(), blob.cy())

I would try using a context manager to write to the file, like this:
with open('color.txt', 'w') as f:
for thr in threshold:
# print(f'thr') # for making sure that thr is actually what you think it is

Also, I’m not sure about the r+ in the open() function. For writing, just use ‘w’

Also, the ‘color.txt’ file was opened and never closed before opening again.

The context manager (with ...) will close it for you automatically.

from what I have learnt it seams that “r+” allows you to read and write in the file at the same time while “w+” does the same but if the file does not exist it creates it.
sadly I don’t have access to the camera for a few days but I will try to update if it worked.


1 Like