logo

请问如何实现人像追踪(最好可以这样实现),或者人脸追踪,想实现功能:小车追着人跑

2022-11-12 01:05
7
ghdk
请在这里粘贴代码(这个是追小球的小车的案例之一)
from pyb import Pin, Timer
inverse_left=False  #change it to True to inverse left wheel  False正转
inverse_right=False #change it to True to inverse right wheel

ain1 =  Pin('P0', Pin.OUT_PP)
ain2 =  Pin('P1', Pin.OUT_PP)
bin1 =  Pin('P2', Pin.OUT_PP)
bin2 =  Pin('P3', Pin.OUT_PP)
ain1.low() #低电平有效
ain2.low()
bin1.low()
bin2.low()

pwma = Pin('P7')
pwmb = Pin('P8')
tim = Timer(4, freq=1000) #定时器4
ch1 = tim.channel(1, Timer.PWM, pin=pwma)
ch2 = tim.channel(2, Timer.PWM, pin=pwmb)
ch1.pulse_width_percent(0) #通道的脉宽百分比,该数值介于1-100间
ch2.pulse_width_percent(0)

def run(left_speed, right_speed):
    if inverse_left==True: #如果反转
        left_speed=(-left_speed)
    if inverse_right==True:
        right_speed=(-right_speed)

    if left_speed < 0:
        ain1.low()
        ain2.high()
    else:
        ain1.high()
        ain2.low()
    ch1.pulse_width_percent(int(abs(left_speed))) #abs绝对值

    if right_speed < 0:
        bin1.low()
        bin2.high()
    else:
        bin1.high()
        bin2.low()
    ch2.pulse_width_percent(int(abs(right_speed)))

请在这里粘贴代码(这个是人像检测的案例)

# TensorFlow Lite 人检测例程

#

# Google的“人检测模型”会检测到是否有人。

#

# 在此示例中,我们将探测器窗口滑到图像上方,并获取激活列表。

# 请注意,使用带有滑动窗口的CNN计算极为复杂,因此对于详尽搜索,不要期望CNN是实时的。

import sensor, image, time, os, tf

sensor.reset() # 复位并初始化传感器。

sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)

#设置图像色彩格式,有RGB565色彩图和GRAYSCALE灰度图两种

sensor.set_framesize(sensor.QVGA) # 将图像大小设置为QVGA (320x240)

sensor.set_windowing((240, 240)) # 设置240x240窗口。

sensor.skip_frames(time=2000) # 等待一段时间,让相机设置生效。

# 加载内置的人检测神经网络模型(该网络位于OpenMV Cam的固件中)。

net = tf.load('person_detection')

labels = ['unsure', 'person', 'no_person']

clock = time.clock()

while(True):

clock.tick()

img = sensor.snapshot()

# net.classify()将在图像的roi上运行网络(如果没有指定roi,则在整个图像上运行)

# 将为每个位置生成一个分类得分输出向量。

# 在每个比例下,检测窗口都以x_overlap(0-1)和y_overlap(0-1)为指导在ROI中移动。

# 如果将重叠设置为0.5,那么每个检测窗口将与前一个窗口重叠50%。

# 请注意,重叠越多,计算工作量就越大。

# 最后,对于在网络沿x/y方向滑动后的多尺度匹配,检测窗口将由scale_mul(0-1)缩小到min_scale(0-1)。

# 下降到min_scale(0-1)。例如,如果scale_mul为0.5,则检测窗口将缩小50%。

# 请注意,如果x_overlap和y_overlap较小,则在较小的比例下可以搜索更多区域...

# 默认设置只是进行一次检测...更改它们以搜索图像...

for obj in net.classify(img, min_scale=1.0, scale_mul=0.5, x_overlap=0.0, y_overlap=0.0):

print("**********\nDetections at [x=%d,y=%d,w=%d,h=%d]" % obj.rect())

for i in range(len(obj.output())):

print("%s = %f" % (labels[i], obj.output()[i]))

img.draw_rectangle(obj.rect())

img.draw_string(obj.x()+3, obj.y()-1, labels[obj.output().index(max(obj.output()))], mono_space = False)

print(clock.fps(), "fps")

```
请在这里粘贴代码(这个是人脸追踪的案例)
# 人脸追踪例程
#
# 这个例子展示了使用OpenMV Cam的关键点功能来跟踪一个被Haar Cascade
# 检测到的人脸。 该脚本的第一部分使用前面Haar Cascade在图像中找到一张
# 脸。 之后,脚本使用关键点功能自动学习你的脸并跟踪它。 关键点可以
# 用来自动追踪任何东西。
import sensor, time, image

# Reset sensor
sensor.reset()
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((320, 240))
sensor.set_pixformat(sensor.GRAYSCALE) #设置相机模块的像素格式

# 跳过几帧,让感光元件稳定下来,使设置生效
sensor.skip_frames(time = 2000)

# 加载Haar算子
# 默认情况下,这将使用所有阶段,较低的阶段更快但不太准确。
face_cascade = image.HaarCascade("frontalface", stages=25) #Haar Cascade是一系列用来确定一个对象是否存在于图像中的对比检查。进行25个阶段的检查
print(face_cascade)

# First set of keypoints
# 第一组关键点
kpts1 = None

# Find a face!
# 找一张脸!
while (kpts1 == None):
    img = sensor.snapshot()
    img.draw_string(0, 0, "Looking for a face...")
    # Find faces
    objects = img.find_features(face_cascade, threshold=0.5, scale=1.25) #这个方法搜索与Haar Cascade匹配的所有区域的图像,并返回一个关于这些特征的边界框矩形元组(x,y,w,h)的列表。若未发现任何特征,则返回一个空白列表。threshold 是浮点数(0.0-1.0),其中较小的值在提高检测速率同时增加误报率;scale 是一个必须大于1.0的浮点数。较高的比例因子运行更快,但其图像匹配相应较差。理想值介于1.35-1.5之间;第4个参数是ROI
    if objects:
        # 在每个方向上将ROI扩大31个像素
        face = (objects[0][0]-31, objects[0][1]-31,objects[0][2]+31*2, objects[0][3]+31*2)
        # 使用检测面大小作为ROI提取关键点
        kpts1 = img.find_keypoints(threshold=10, scale_factor=1.1, max_keypoints=100, roi=face)
        # 在第一个人脸周围画一个矩形
        img.draw_rectangle(objects[0])

# Draw keypoints
print(kpts1)
img.draw_keypoints(kpts1, size=24)
img = sensor.snapshot()
time.sleep_ms(2000)

# FPS clock
clock = time.clock() #开始追踪运行时间

while (True):
    clock.tick()
    img = sensor.snapshot()
    # 从整个帧中提取关键点
    kpts2 = img.find_keypoints(threshold=10, scale_factor=1.1, max_keypoints=100, normalized=True) #第一个参数可设置ROI

    if (kpts2):
        # 将第一组关键点与第二组关键点匹配
        c=image.match_descriptor(kpts1, kpts2, threshold=85)
        match = c[6] # C[6] contains the number of matches. C[6]包含匹配的数量。
        if (match>5):
            img.draw_rectangle(c[2:6])
            img.draw_cross(c[0], c[1], size=10)
            print(kpts2, "matched:%d dt:%d"%(match, c[7]))

    # Draw FPS
    img.draw_string(0, 0, "FPS:%.2f"%(clock.fps())) #停止追踪运行时间,并返回当前FPS(每秒帧数)。
7
ghdk

在线等!!!万分感谢!!

2022-11-14 15:04
I
网红小智智 星瞳科技

我推荐用目标点检测FOMO来做。https://singtown.com/learn/50918/

然后参考这个修改:https://singtown.com/learn/50041/