【问题标题】:OpenCV DNN Face detectorOpenCV DNN 人脸检测器
【发布时间】:2019-04-20 23:00:44
【问题描述】:

def 检测视频(图像):

gray=image


blob = cv2.dnn.blobFromImage(gray, 1.0, (300, 300), [104, 117, 123], False, False)
net.setInput(blob)
detections = net.forward()
bboxes = []
gray=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
frameWidth=image.shape[1]
frameHeight=image.shape[0]
for i in range(detections.shape[2]):
    confidence = detections[0, 0, i, 2]
    if confidence > 0.7:
        x1 = int(detections[0, 0, i, 3] * frameWidth)
        y1 = int(detections[0, 0, i, 4] * frameHeight)
        x2 = int(detections[0, 0, i, 5] * frameWidth)
        y2 = int(detections[0, 0, i, 6] * frameHeight)
        cv2.rectangle(image,(x1,y1),(x2,y2),(255,255,0),3)
        try:
            image1 = gray[y1:(y2), x1:(x2)]

            img = cv2.resize(image1, (48,48), interpolation = cv2.INTER_CUBIC) / 255.

            prediction=model1.predict_proba(img.reshape(1,48,48,1))

            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(image,str(emotions[prediction[0].argmax()]),(x1,y1+10), font, 1,(255,255,255),2,cv2.LINE_AA)

            result=prediction
            if result is not None:
                if result[0][6] < 0.6:
                    result[0][6] = result[0][6] - 0.12
                    result[0][:3] += 0.01
                    result[0][4:5] += 0.04
    # write the different emotions and have a bar to indicate probabilities for each class
                for index, emot in enumerate(emotion):
                    cv2.putText(image, emot, (10, index * 20 + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
                    cv2.rectangle(image, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4), (255, 0, 0), -1)
                emt=[prediction[0][0],prediction[0][1],prediction[0][2],prediction[0][3],prediction[0][4],prediction[0][5],prediction[0][6]]
                indx=np.arange(len(emotion))
                plt.bar(indx,emt,color='blue')

                plt.xticks(indx,emotion)
                plt.savefig("ab.png")
                cv2.imshow("graph",cv2.imread("ab.png"))
                plt.clf()
                #cv2.waitKey(5)
                #plt.show()
                #return indx,emt


        except:
            #print("----->Problem during resize .Probably Cant detect any face")
            continue
return image

我已经制作了自己的模型并在 KDEF 数据集上进行了训练。现在,当我将视频作为输入时,它会检测到视频中的人脸,但会生成两个边界框。谁能帮助我代码中的错误是什么.它运行成功,但只是创建了两个边界框。神经网络接受的输入是 48*48。

【问题讨论】:

    标签: python opencv computer-vision blob


    【解决方案1】:

    首先选择置信度最高的检测,然后将其绘制在图像上。

    detection_index = 0
    max_confidence = 0
    
    for i in range(detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if max_confidence < confidence:
            max_confidence = confidence
            detection_index = i
    
    i = detection_index
    
    x1 = int(detections[0, 0, i, 3] * frameWidth)
    y1 = int(detections[0, 0, i, 4] * frameHeight)
    x2 = int(detections[0, 0, i, 5] * frameWidth)
    y2 = int(detections[0, 0, i, 6] * frameHeight)
    cv2.rectangle(image, (x1, y1), (x2, y2), (255, 255, 0), 3)
    try:
        image1 = gray[y1:(y2), x1:(x2)]
    
    img = cv2.resize(image1, (48, 48), interpolation=cv2.INTER_CUBIC) / 255.
    
    prediction = model1.predict_proba(img.reshape(1, 48, 48, 1))
    
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(image, str(emotions[prediction[0].argmax()]), (x1, y1 + 10), font, 1, (255, 255, 255), 2, cv2.LINE_AA)
    
    result = prediction
    if result is not None:
        if result[0][6] < 0.6:
            result[0][6] = result[0][6] - 0.12
            result[0][:3] += 0.01
            result[0][4:5] += 0.04
            # write the different emotions and have a bar to indicate probabilities for each class
        for index, emot in enumerate(emotion):
            cv2.putText(image, emot, (10, index * 20 + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
            cv2.rectangle(image, (130, index * 20 + 10), (130 + int(result[0][index] * 100), (index + 1) * 20 + 4),
                          (255, 0, 0), -1)
        emt = [prediction[0][0], prediction[0][1], prediction[0][2], prediction[0][3], prediction[0][4],
               prediction[0][5], prediction[0][6]]
        indx = np.arange(len(emotion))
        plt.bar(indx, emt, color='blue')
    
        plt.xticks(indx, emotion)
        plt.savefig("ab.png")
        cv2.imshow("graph", cv2.imread("ab.png"))
        plt.clf()
        # cv2.waitKey(5)
        # plt.show()
        # return indx,emt
    
    
    except:
        # print("----->Problem during resize .Probably Cant detect any face")
        continue
    return image
    

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 2020-05-14
      • 2020-12-15
      • 2012-02-04
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2012-04-15
      相关资源
      最近更新 更多