使puttext标签始终显示

2024-10-02 16:25:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的代码来检测对象并预测它。你知道吗

for i in range(len(detections)):
    face_i = detections[i]
    x,y,w,h  = face_i
    xw1 = max(int(x - 0.4 * w), 0)
    yw1 = max(int(y - 0.4 * h), 0)
    xw2 = min(int(x + w + 0.7 * w), img_w - 1)
    yw2 = min(int(y + h + 0.4 * h), img_h - 1)
roi = frame[yw1:yw2 + 1, xw1:xw2 + 1, :]
roi = cv2.resize(roi, (299, 299), interpolation=cv2.INTER_CUBIC)

numpy_frame = np.asarray(roi)
numpy_frame = cv2.normalize(numpy_frame.astype('float'), None, -0.5, .5, cv2.NORM_MINMAX)
numpy_final = np.expand_dims(numpy_frame, axis=0)

start_time = timeit.default_timer()

 #Do prediction for every five seconds
if (constance.x % 5 == 0):

    predictions = sess.run(detection_graph, {'Mul:0': numpy_final})

    animal_score= predictions[0][1]
    human_Score = predictions[0][0]
    if (male_score > female_Score):
        human_string = "Animal"
    else:
        human_string = "Human"

    cv2.putText(image_np, str (human_string), (x, y - 10), font, 1, (0, 0, 255), 2, cv2.LINE_AA)
cv2.rectangle(image_np, (xw1, yw1), (xw2, yw2), (0, 222, 0), 1)

return image_np

因为预测要花很多时间,我有时会跳过预测。因为我每五秒钟才放一次文本,所以标签值在闪烁。如何使标签始终出现?你知道吗


Tags: imagenumpyforstringnpcv2frameint