Детектор лиц OpenCV DNN

определение_видео (изображение):

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.


person KRK    schedule 18.11.2018    source источник


Ответы (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
person Tahirhan    schedule 18.11.2018