H7 plus disconnect from IDE

Hi Kwagyeman,

Im running my script and, often, after 5min aprox, some other times after 15min… my h7 plus camera is disconnected from the IDE.

Do you have a guess of what am I doing wrong?

My scripts are not called “main.py” but i guess thats fine because while running the script the H7 plus is connected to the PC. (Please correct me if I am wrong)
I do not know if the high temperature of the hardware has something to do… or if the framesize that im using is a problem because it is too big.

Thanks in advance!

Code is below:

import sensor, image, time, os, tf, pyb, math

L_mean = 0
A_mean = 8
B_mean = 16

sensor.reset()                         # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.WQXGA2) #2592x1944

sensor.set_windowing((1775,1450)) 
sensor.set_saturation(-3)
sensor.set_contrast(-3)
sensor.set_brightness(0)
sensor.skip_frames(time=2000)          # Let the camera adjust.

net = "trained.tflite"
labels = [line.rstrip('\n') for line in open("labels.txt")]


clock = time.clock()

base_image = sensor.snapshot()
start = True
decision = [0,0,0]

if not "temp" in os.listdir(): os.mkdir("temp") # Make a temp directory

while(True):

    clock.tick()

    img = sensor.snapshot()

    if  start == False:
        base_image = potential_image

    base_hist = base_image.histogram()
    base_image_stats = base_hist.get_statistics()
    base_image_L_mean = base_image_stats[L_mean]

    potential_image = sensor.snapshot()

    potential_hist = potential_image.histogram()
    potential_image_stats = potential_hist.get_statistics()
    potential_image_L_mean = potential_image_stats[L_mean]

    DeltaE = math.fabs(potential_image_L_mean - base_image_L_mean)
    decision[0] = decision[1]
    decision[1] = decision[2]
    decision[2] = DeltaE
    sumDecision = decision[0] + decision[1] + decision[2]
    if sumDecision > 2.42:
        saved = "YES"
    else:
        saved ="NO"

    FPS = clock.fps()
    start = False

    frames = 0
    frames_limit = 9
    Background = 0
    NonTarget = 0
    Target = 0
    predicted = "Unclear"


    if saved == "YES":
        while(frames <= frames_limit):
            for obj in tf.classify(net, img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
                predictions_list = list(zip(labels, obj.output()))
                Background = Background + predictions_list[0][1]
                NonTarget = NonTarget + predictions_list[1][1]
                Target = Target + predictions_list[2][1]

            frames = frames + 1

        Background_mean = Background/(frames_limit + 1)
        NonTarget_mean = NonTarget/(frames_limit + 1)
        Target_mean = Target/(frames_limit + 1)

        if (Background >= 6):
            predicted = "Background"
        if (NonTarget >=5):
            predicted = "Not Target"
        if (Target >= 7):
            predicted = "Target!"

        print("Predictions: Background: %.2f, Not target: %.2f, Target: %.2f. Predicted: %s" % (Background, NonTarget, Target, predicted))

Hi, the IDE has a timeout for polling the camera. However, this would only be an issue if you do something that disables interrupts. I don’t see anything like that in your code.

If you shrink the window size does it not crash anymore?

Also, snapshot doesn’t return a new image object. Just a pointer to the frame buffer. You need to alloc a second frame buffer if you want two images. So, the snapshot calls you are making are replacing each other.

Hi, thanks for your reply. Well I have tried a similar code and I still had same issues. The camera was being disconnected from the IDE very often. Most of the times I couldnt see the pictures that has been stored on the SD card. Some other times the pictures appears on the SD card after long time. Once I even got random folder and files stored on my SD card (Please see image):

The files on red were automatically generated.

Then I tried to reduce the window size to 120x100, and sometimes doesnt crash (but pictures sometimes are not saved) and some other times i get this other message:

OSError: Assertion failed.

Am I doing something wrong or missing something?

About the snapshot suggestion. I think for what i am trying to do is fine like this. (correct me if im wrong). What i am trying to do is… Take 1 picture, then take the statistics from histogram, then take 2nd picture, and compare with histogram statistics of fist image. if differences are higher than “X”, then save 2nd image. Otherwise do nothing.

Thanks again for your help and time

Code below:

import sensor, image, time
import pyb, math, os

L_mean = 0
A_mean = 8
B_mean = 16
image_number = 1300

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
#sensor.set_framesize(sensor.WQXGA2) #2592x1944  WQXGA2

sensor.set_framesize(sensor.QQVGA)  #160 120

sensor.set_windowing((120,100))   #1900 1600   1550 1475 
sensor.set_saturation(-3)
sensor.set_contrast(-3)
sensor.set_brightness(0)
sensor.skip_frames(time = 2000)

clock = time.clock()

base_image = sensor.snapshot()
start = True
decision = [0,0,0]

if not "temp" in os.listdir(): os.mkdir("temp") # Make a temp directory


while(True):

    clock.tick()

    if  start == False:
        base_image = potential_image

    # BASE IMAGE CIELAB CHARACTERISTICS
    base_hist = base_image.histogram()
    base_image_stats = base_hist.get_statistics()
    base_image_L_mean = base_image_stats[L_mean]
    potential_image = sensor.snapshot()

    # POTENTIAL IMAGE CIELAB CHARACTERISTICS
    potential_hist = potential_image.histogram()
    potential_image_stats = potential_hist.get_statistics()
    potential_image_L_mean = potential_image_stats[L_mean]

    DeltaE = math.fabs(potential_image_L_mean - base_image_L_mean)
    decision[0] = decision[1]
    decision[1] = decision[2]
    decision[2] = DeltaE
    sumDecision = decision[0] + decision[1] + decision[2]
    if sumDecision > 2:
        image_number = image_number + 1
        saved = "YES"
        potential_image.save("/Dubia/Dubia_%d.jpg" %image_number)

    else:
        saved ="NO"

    print(decision)
    FPS = clock.fps()
    print("FPS: %.2f B L: %i P L: %i, DeltaE: %.2f Sum: %.2f %s" % (FPS, base_image_L_mean, potential_image_L_mean,DeltaE, sumDecision, saved))
    start = False

Hi, we’ve noticed an issue with saving files to the disk causes the IDE to have stability issues. This is because interrupts are disabled. We’re working on a fix for this.