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:
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)