is creating a Region of interest possible?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
just_trying
Posts: 2
Joined: Sun Aug 30, 2020 3:36 pm

is creating a Region of interest possible?

Post by just_trying » Sun Aug 30, 2020 4:16 pm

Hello I've been using the Sipeed M1 dock board to create a very simple lamp.
My idea is that the camera detects people and the more people present in a region of interest, the more LEDs will be turned on.
The project already works like it should, but I want to constrain what the camera sees in a certain area and ignore the rest.
Is it possible?
example:
Image

my codes are below, thanks for the help:

Code: Select all

import sensor,image,lcd,time,os,utime
import KPU as kpu
from modules import ws2812
lcd.init(freq=15000000)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_vflip(0)
sensor.set_hmirror(0)
sensor.run(1)
clock = time.clock()
utime.sleep_ms(25)
lcd.clear(lcd.BLACK)
ledNum = 20
ledPin = 19
ledDetect = ws2812(ledPin,ledNum)
classes = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
utime.sleep_ms(25)
task = kpu.load("/sd/20class.kmodel")
info = kpu.netinfo(task)
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987,5.3658, 5.155437, 6.92275, 6.718375, 9.01025)
a = kpu.init_yolo2(task, 0.80, 0.85, 5, anchor)
timeCount = 0
timeReset = 500


def detectionRun(modelName,colorFill, ColorFillB, r, g, b):
    for i in code:
        a=img.draw_rectangle(i.rect(),colorFill,ColorFillB,1)
        a = lcd.display(img)
        a = img.draw_string(i.x(),i.y(), classes[i.classid()], color=(255,0,0), scale=2)
        lcd.draw_string(i.x(), i.y(), classes[i.classid()], lcd.RED)
        detect = i.x() #holds value
        ledConstrain = constrain(i.x(),0,240)
        print('constrain')
        print(ledConstrain)
        print(detect)
        print('...')
        ledpush = map(detect ,0,240,0,20)
        ledDetect.set_led(ledpush,(r,g,b))
        ledDetect.display()


def constrain(val, min_val, max_val):
    return min(max_val, max(min_val, val))

def blinkRun():
    ledDetectB.set_led(ledNumB,(250,0,0))
    ledDetectB.display()
    
def map(x, in_min, in_max, out_min, out_max):
    utime.sleep_ms(25)
    return int((x-in_min) * (out_max-out_min) / (in_max-in_min) + out_min)

while(True):
#    clock.tick()
    img = sensor.snapshot()
    code = kpu.run_yolo2(task, img)
    a=img.draw_rectangle(0,0,80,240,lcd.WHITE,lcd.WHITE,1)
    a=img.draw_rectangle(240,0,80,240,lcd.WHITE,lcd.WHITE,1)
    a=img.draw_rectangle(0,0,320,60,lcd.WHITE,lcd.WHITE,1)
    if code:
        for i in code:#(80,60,160,180)
            a=img.draw_rectangle(80,60,160,180,lcd.WHITE)
            a = lcd.display(img)
            subject = classes[i.classid()]
        if subject == "person":
            colorName = lcd.PINK
            detectionRun(subject,colorName, colorName, 0, 50, 250)
            timeCount += 1
            print("person")

        if  timeCount >= timeReset:
            for l in range(ledNum):
                #ledDetect.ledFader( r, g, b)
                ledDetect.set_led(l,(0,0,0))
            ledDetect.display()
            timeCount = 0
    else:
        a=img.draw_rectangle(80,60,160,180,lcd.WHITE)
        a = lcd.display(img)
        for l in range(ledNum):
            ledDetect.set_led(l,(0,0,0))
        ledDetect.display()
a = kpu.deinit(task)
please do not crucify me, I'm a very bad coder :)

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: is creating a Region of interest possible?

Post by OutoftheBOTS_ » Tue Sep 01, 2020 6:07 am

I haven't used this board but to cut out a square ROI then something like this

Code: Select all

img = sensor.snapshot()[y:y+h, x:x+w]

just_trying
Posts: 2
Joined: Sun Aug 30, 2020 3:36 pm

Re: is creating a Region of interest possible?

Post by just_trying » Wed Sep 02, 2020 7:03 am

Thanks a lot, I didn't know snapshot had this function,
I will give it a try :)

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: is creating a Region of interest possible?

Post by OutoftheBOTS_ » Wed Sep 02, 2020 8:43 am

it isn't that snapshot as that function, it is the fact that snap shot returns an array and this is how you can cut out of an array

Post Reply