2017-11-30 5 views
2

私はOpenCV Pythonの初心者であり、StackOverflowでも初心者ですので間違いはありません。私は画像分類タスクにPython 3.6でOpenCV 3.2を使用しています。私の仕事は、フレーム内のオブジェクトを分類し、そのフレーム上にクラスラベルを付けることです。最終的な出力ビデオが書き戻されると、ビデオに貼り付けられたテキストがオーバーラップします。下は私のコードです。OpenCv 3.2 Pythonビデオライティングの問題:フレーム上のテキストが重複する

cap = cv2.VideoCapture(r"D:\python\tank_video\All_vehicle.mp4") 
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) 
currentFrame = 0 

# videowriter object 
codc = int(cap.get(cv2.CAP_PROP_FOURCC)) 
fps = int(cap.get(cv2.CAP_PROP_FPS)) 
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 

fourcc = cv2.VideoWriter_fourcc(*'XVID') 
out = cv2.VideoWriter('classify_300_200_3.avi', fourcc, 15, (300,200)) 

          "CLASSIFICATION CODE" 

predictions = [classes_names[i] for i in clf.predict(test_features)] 
arr = np.array([]) 

j = 0 
for prediction in predictions: 
    pt = (0, 3 * image.shape[0] // 4) 
    cv2.putText(image, prediction, pt ,cv2.FONT_HERSHEY_SIMPLEX, 2, [0, 255, 
    0], 2) 
    np.append(arr,cv2.imwrite(str(j)+'.jpg',image)) 

out.write(image) 
currentFrame += 1 
cap.release()   
out.release() 
cv2.destroyAllWindows() 

出力画像が添付されています。 this is the screenshot of video output. 1つのクラスラベルを正確に表示したい。前のフレームのラベルを表示しないで、前のフレームのラベルに現在のフレームのラベルを上書きしないでください。誰も私がそれを解決するのを助けることができる?

答えて

1

あなたが唯一のクラスラベルを表示したい場合は、次のように変更する必要があります。

for prediction in predictions: 
    pt = (0, 3 * image.shape[0] // 4) 
    cv2.putText(image, prediction, pt ,cv2.FONT_HERSHEY_SIMPLEX, 2, [0, 255, 
    0], 2) 
    np.append(arr,cv2.imwrite(str(j)+'.jpg',image)) 

コードのその部分が画像に検出されたすべてのクラスを書き込みますので、。たとえば、次のようにすることができます。

# check if at least one class detected 
if len(predictions)>0: 
    # choose to display only the first class 
    prediction = predictions[0] 

    pt = (0, 3 * image.shape[0] // 4) 
    cv2.putText(image, prediction, pt ,cv2.FONT_HERSHEY_SIMPLEX, 2, [0, 255, 
    0], 2) 
    np.append(arr,cv2.imwrite(str(j)+'.jpg',image)) 
+0

これは重複するテキストの問題を解決します。ありがとう –

関連する問題