-1

I would like to use Raspberry Pi with OpenCV + yolo to do smart monitering system.

The question is: Instead of showing a box on the screen, could it return Ture or False when detecting "person"?

I learned that it could successful show on screen a box on screen when detecting something. I use a code from a chinese book, I am afraid that it might viloate the rule to paste the whole code here, so I breif describe the code it use.

the original code use the following 3 yolo file:

 yolo4-tiny.cfg
 coco.names
 yolo4-tine.weight

And

import cv2, numpy and time

the code resize the image to a smaller frame and then put the image into yolo

def XXX(image, model):
    classes, confs, boxes = model.detect(image, 0.6, 0.3)
    return classes, confs, boxes

the next step is show the box in the image.

def YYY(image, classes, confs, boxes, names, colors):
    new_image = image.copy()
    for (classid, conf, box) in zip(classes, confs, boxes):
        x, y, w , h = box 
        label = '{}: {:.2f}'.format(names[int(classid)], float(conf))
        color = colors[int(classid)]
        cv2.rectangle(new_image, (x, y), (x + w, y + h), color, 2)
        cv2.putText(new_image, label, (x, y - 10), 
            cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2
        )
    return new_image

My question is how to return true when detecting "person". Thank you for respone this question.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

1 Answers1

-1

In your case, (I haven't used Yolo, but I've used OpenCV face detection and MTCNN), you can use one of the returned variables from the detect() function. It should be safe to assume that if the model creates a bounding box in the detection function, then a person exists.

def XXX(image, model):
    classes, confs, boxes = model.detect(image, 0.6, 0.3)
    return classes, confs, boxes

x, y, z = XXX(image, model)
if z is not None:  # if not an iterable
# if z.any():  # <-- this might be any(z) instead of z.any()
    # do something
Djinn
  • 663
  • 5
  • 12
  • 2
    "Python counts non-None variables as True by default" is wrong. See truthy and falsy values. – MSH Jun 22 '22 at 05:24
  • and you still need to test if any of the detections is classified as "person". there are more classes than just the one. – Christoph Rackwitz Jun 22 '22 at 08:17
  • `[] is not None` => True, so that's really not doing what you hoped it would – Christoph Rackwitz Jun 22 '22 at 08:19
  • Considering OP is using boxes directly as if it had already detected a person (look at their code) it working the same as if it found a person. That's not the same as if the nod found a person. OP's code is under the assumption that a found box will immediately get used. Therefore, if box contains values, it found something, according to OP's code. – Djinn Jun 22 '22 at 16:05
  • `[] is not None`, you're right I mistyped that one. – Djinn Jun 22 '22 at 16:09