logo

为什么对人脸追踪的程序只是加入了打印人脸坐标,每次循环的结果都是一样的没有变化?

2019-07-05 15:52
3
ztvu
# Face Tracking Example
#
# This example shows off using the keypoints feature of your OpenMV Cam to track
# a face after it has been detected by a Haar Cascade. The first part of this
# script finds a face in the image using the frontalface Haar Cascade.
# After which the script uses the keypoints feature to automatically learn your
# face and track it. Keypoints can be used to automatically track anything.
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)

# Skip a few frames to allow the sensor settle down
sensor.skip_frames(time = 2000)

# Load Haar Cascade
# By default this will use all stages, lower satges is faster but less accurate.
face_cascade = image.HaarCascade("frontalface", stages=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)
    if objects:
        # Expand the ROI by 31 pixels in every direction
        face = (objects[0][0]-31, objects[0][1]-31,objects[0][2]+31*2, objects[0][3]+31*2)
        # Extract keypoints using the detect face size as the ROI
        kpts1 = img.find_keypoints(threshold=10, scale_factor=1.1, max_keypoints=100, roi=face)
        # Draw a rectangle around the first face
        img.draw_rectangle(objects[0])
       
# Draw keypoints
print(kpts1)
img.draw_keypoints(kpts1, size=24)
img = sensor.snapshot()
time.sleep(2000)

# FPS clock
clock = time.clock()

while (True):
    clock.tick()
    img = sensor.snapshot()
    # Extract keypoints from the whole frame
    kpts2 = img.find_keypoints(threshold=10, scale_factor=1.1, max_keypoints=100, normalized=True)

    if (kpts2):
        # Match the first set of keypoints with the second one
        c=image.match_descriptor(kpts1, kpts2, threshold=85)
        match = c[6] # C[6] contains the number of matches.
        if (match>5):
            img.draw_rectangle(c[2:6])
            img.draw_cross(c[0], c[1], size=10)
            t=img.draw_rectangle(c[2:6])
            print(kpts2, "matched:%d dt:%d"%(match, c[7]))
            for face in objects:
                print(face)

    # Draw FPS
    img.draw_string(0, 0, "FPS:%.2f"%(clock.fps()))
3
ztvu

只是加了这一句

> for face in objects:

> print(face)

3
ztvu

串口显示结果

{"size":54, "threshold":10, "normalized":1} matched:18 dt:0
(46, 8, 182, 182)
{"size":50, "threshold":10, "normalized":1} matched:17 dt:0
(46, 8, 182, 182)
{"size":53, "threshold":10, "normalized":1} matched:18 dt:0
(46, 8, 182, 182)
{"size":52, "threshold":10, "normalized":1} matched:17 dt:0
(46, 8, 182, 182)
{"size":56, "threshold":10, "normalized":1} matched:18 dt:0
(46, 8, 182, 182)
{"size":62, "threshold":10, "normalized":1} matched:18 dt:0
(46, 8, 182, 182)
{"size":55, "threshold":10, "normalized":1} matched:15 dt:0
(46, 8, 182, 182)
{"size":70, "threshold":10, "normalized":1} matched:10 dt:0
(46, 8, 182, 182)
{"size":85, "threshold":10, "normalized":1} matched:11 dt:0
(46, 8, 182, 182)
{"size":61, "threshold":10, "normalized":1} matched:9 dt:0
(46, 8, 182, 182)
{"size":58, "threshold":10, "normalized":1} matched:8 dt:0
(46, 8, 182, 182)
{"size":49, "threshold":10, "normalized":1} matched:7 dt:0
(46, 8, 182, 182)
{"size":51, "threshold":10, "normalized":1} matched:8 dt:0
(46, 8, 182, 182)
{"size":30, "threshold":10, "normalized":1} matched:7 dt:0
(46, 8, 182, 182)
{"size":44, "threshold":10, "normalized":1} matched:6 dt:0
(46, 8, 182, 182)
{"size":45, "threshold":10, "normalized":1} matched:8 dt:0
(46, 8, 182, 182)
{"size":45, "threshold":10, "normalized":1} matched:9 dt:0
(46, 8, 182, 182)
{"size":37, "threshold":10, "normalized":1} matched:6 dt:15
(46, 8, 182, 182)
{"size":41, "threshold":10, "normalized":1} matched:6 dt:15
(46, 8, 182, 182)
{"size":45, "threshold":10, "normalized":1} matched:6 dt:270
(46, 8, 182, 182)
{"size":38, "threshold":10, "normalized":1} matched:8 dt:15
(46, 8, 182, 182)
{"size":46, "threshold":10, "normalized":1} matched:8 dt:15
(46, 8, 182, 182)
{"size":43, "threshold":10, "normalized":1} matched:7 dt:0
(46, 8, 182, 182)
{"size":40, "threshold":10, "normalized":1} matched:7 dt:15
(46, 8, 182, 182)
{"size":49, "threshold":10, "normalized":1} matched:9 dt:30
(46, 8, 182, 182)
{"size":38, "threshold":10, "normalized":1} matched:7 dt:0
(46, 8, 182, 182)
{"size":63, "threshold":10, "normalized":1} matched:8 dt:0
(46, 8, 182, 182)
{"size":53, "threshold":10, "normalized":1} matched:8 dt:0
(46, 8, 182, 182)
{"size":49, "threshold":10, "normalized":1} matched:9 dt:0
(46, 8, 182, 182)
{"size":43, "threshold":10, "normalized":1} matched:6 dt:0
(46, 8, 182, 182)
{"size":47, "threshold":10, "normalized":1} matched:7 dt:0
(46, 8, 182, 182)
{"size":42, "threshold":10, "normalized":1} matched:6 dt:0
(46, 8, 182, 182)
{"size":41, "threshold":10, "normalized":1} matched:7 dt:0
(46, 8, 182, 182)
{"size":34, "threshold":10, "normalized":1} matched:7 dt:0
(46, 8, 182, 182)
{"size":21, "threshold":10, "normalized":1} matched:8 dt:0
(46, 8, 182, 182)
{"size":29, "threshold":10, "normalized":1} matched:6 dt:0
(46, 8, 182, 182)
{"size":24, "threshold":10, "normalized":1} matched:9 dt:0
(46, 8, 182, 182)
{"size":29, "threshold":10, "normalized":1} matched:6 dt:0
(46, 8, 182, 182)
{"size":23, "threshold":10, "normalized":1} matched:7 dt:0
(46, 8, 182, 182)
{"size":29, "threshold":10, "normalized":1} matched:8 dt:0
(46, 8, 182, 182)
{"size":29, "threshold":10, "normalized":1} matched:6 dt:0
(46, 8, 182, 182)

Traceback (most recent call last):
  File "<stdin>", line 56, in <module>
Exception: IDE interrupt
MicroPython v1.9.4-4553-gb4eccdfe3 on 2019-05-02; OPENMV4 with STM32H743
I
网红小智智 星瞳科技

代码垃圾。逻辑完全不对。

你的代码有两个while死循环,第二个死循环永远不会运行。

I
网红小智智 星瞳科技

我建议你从头开始看教程,把每一句代码的作用都搞懂。不要单纯的复制粘贴。