OpenCV矩形填充

时间:2018-09-26 02:59:49

标签: python image opencv video global

enter image description here我正在尝试填充矩形,但是即使更改代码(将厚度更改为-10)也没有效果。我觉得全球范围与此有关。

我已附上以下代码。

import cv2
import os
import numpy as np
from .utils import download_file

initialize = True
net = None
dest_dir = os.path.expanduser('~') + os.path.sep + '.cvlib' + os.path.sep + 'object_detection' + os.path.sep + 'yolo' + os.path.sep + 'yolov3'
classes = None
COLORS = np.random.uniform(0, 255, size=(80, 3))




def draw_bbox(img, bbox, labels, confidence, colors=None, write_conf=False):

    global COLORS
    global classes

    if classes is None:
        classes = populate_class_labels()

    for i, label in enumerate(labels):

        if colors is None:
            color = COLORS[classes.index(label)]            
        else:
            color = colors[classes.index(label)]

        if write_conf:
            label += ' ' + str(format(confidence[i] * 100, '.2f')) + '%'

        cv2.rectangle(img, (bbox[i][0],bbox[i][1]), (bbox[i][2],bbox[i][3]), color,-1)
        cv2.putText(img, label, (bbox[i][0],bbox[i][1]-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

    return img

def detect_common_objects(image):

    Height, Width = image.shape[:2]
    scale = 0.00392

    global classes
    global dest_dir

    config_file_name = 'yolov3.cfg'
    config_file_abs_path = dest_dir + os.path.sep + config_file_name

    weights_file_name = 'yolov3.weights'
    weights_file_abs_path = dest_dir + os.path.sep + weights_file_name    

    url = 'https://github.com/arunponnusamy/object-detection-opencv/raw/master/yolov3.cfg'

    if not os.path.exists(config_file_abs_path):
        download_file(url=url, file_name=config_file_name, dest_dir=dest_dir)

    url = 'https://pjreddie.com/media/files/yolov3.weights'

    if not os.path.exists(weights_file_abs_path):
        download_file(url=url, file_name=weights_file_name, dest_dir=dest_dir)    

    global initialize
    global net

    if initialize:
        classes = populate_class_labels()
        net = cv2.dnn.readNet(weights_file_abs_path, config_file_abs_path)
        initialize = False

    blob = cv2.dnn.blobFromImage(image, scale, (416,416), (0,0,0), True, crop=False)

    net.setInput(blob)

    outs = net.forward(get_output_layers(net))

    class_ids = []
    confidences = []
    boxes = []
    conf_threshold = 0.5
    nms_threshold = 0.4

    for out in outs:
        for detection in out:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.5 and class_id=='person':
                center_x = int(detection[0] * Width)
                center_y = int(detection[1] * Height)
                w = int(detection[2] * Width)
                h = int(detection[3] * Height)
                x = center_x - w / 2
                y = center_y - h / 2
                class_ids.append(class_id)
                confidences.append(float(confidence))
                boxes.append([x, y, w, h])


    indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)

    bbox = []
    label = []
    conf = []

    for i in indices:
        i = i[0]
        box = boxes[i]
        x = box[0]
        y = box[1]
        w = box[2]
        h = box[3]
        if str(classes[class_ids[i]])=='person':
            bbox.append([round(x), round(y), round(x+w), round(y+h)])
            label.append(str(classes[class_ids[i]]))
            conf.append(confidences[i])

    return bbox, label, conf

上面的整个代码。它是一个使用Yolo和opencv的对象检测程序。我还在最后一行中添加了几行以仅启用人员类,但似乎可以检测到所有类。我也尝试过修改矩形的厚度,但是更改值没有任何效果。

3 个答案:

答案 0 :(得分:0)

您只需将-10更改为-1。更改后,您的代码将如下所示:

<form action="result.php" method="post" id="form"> 
<input required type="number" min="1" max="5" name="11">
<input required type="number" min="1" max="5" name="12">
<input required type="number" min="1" max="5" name="13">
<input required type="number" min="1" max="5" name="14">
<input required type="number" min="1" max="5" name="15">
<button class="button3" name="click" >Submit</button>

答案 1 :(得分:0)

我确实犯了一个愚蠢的错误。我正在更改我的Github文件夹中的object / detection.py文件。但是,当我看到这一切时,一切都是有意义的。

File "/Users/dukeglacia/anaconda3/lib/python3.6/site-packages/cvlib/object_detection.py"

我实际上是在更改错误的文件(尽管原本完全相同)。

答案 2 :(得分:0)

我有一个矩形边界并用于填充这些矩形。输入“-1”

cv2.rectangle(frame, (x, y), (w, h), (255, 255, 255), -1)