@nobu6787 (nobunobuta)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

fourccとは

opencvのタイムラプス生成が上手くいきません。
尚、python実践データ分析100本ノックのノック88を参照しています。

<コード>
import cv2

print("タイムラプス生成を開始します")

cap = cv2.VideoCapture("mov/mov01.avi")
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
hogParams = {'winStride': (8,8), 'padding': (32,32), 'scale': 1.05, 'hitThreshold':0, 'finalThreshold':5}

movie_name = "timelapse.avi"
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
video = cv2.VideoWriter(movie_name,fourcc, 30, (width,height))

num = 0
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
if (num%10==0):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
human, r = hog.detectMultiScale(gray, **hogParams)
if(len(human)>0):
for(x, y, w, h) in human:
cv2.rectangle(frame, (x,y),(x + w, y + h), (255,255,255), 3)

        video.write(frame)
else:
    break
num = num + 1

video.release()
cap.release()
cv2.destroyAllWindows()
print("タイムラプス生成を終了しました")

<エラー文>
error Traceback (most recent call last)
 in ()
16 movie_name = "timelapse.avi"
17 fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
---> 18 video = cv2.VideoWriter(movie_name,fourcc, 30, (width,height))
20 num = 0
21 while(cap.isOpened()):

error: OpenCV(4.5.5) :  -1  : error: (-5:Bad argument) in function 'VideoWriter'

Overload resolution failed:

  • Can't parse 'frameSize'. Sequence item with index 0 has a wrong type
  • VideoWriter() missing required argument 'frameSize' (pos 5)
  • VideoWriter() missing required argument 'params' (pos 5)
  • VideoWriter() missing required argument 'frameSize' (pos 5)

エラーがvideo = cv2.VideoWriter(movie_name,fourcc, 30, (width,height))を指していること、
コード中の'fourcc'に青波線があり、カーソルを合わせると"fourcc":unknown wordとなることから
この部分が良くないのはおおよそ検討がつくのですが、なかなか解決しません。
エラー文の意味も合わせてご教授いただけると幸いです。
どうぞよろしくお願いします。

0 likes

3Answer

次のように、intへのキャスト処理を加えてやると良さそうに思えます.

-width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
-height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
+width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
+height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
0Like

出版社のサイトに置かれているサンプルコードを参照にされると良いかも知れません.

今回の No88 も掲載されたコードと、出版社サイトに上げられているコードに差分がありました.

0Like

Comments

  1. @nobu6787

    Questioner

    ありがとうございます。現状ご指示頂いたのを参考に試行錯誤しています。
    ありがとうございました。

使用してるカメラのコーデックが合ってないのではないでしょうか?
以下の記事が参考になると思います。
https://qiita.com/Gyutan/items/e279e8680b90ca891250

import cv2

cap = cv2.VideoCapture(0)   #カメラ番号を指定

#戻り値はDuble型なのでInt型に変換する
fourcc_int = int(cap.get(cv2.CAP_PROP_FOURCC))
cap.release()

print("fourcc_int",fourcc_int)
#>>>fourcc_int 842094158

#Four codec characterに変換する方法1
fourcc = list((fourcc_int.to_bytes(4, 'little').decode('utf-8')))
print(fourcc)

(記事より引用)

こちらを実行してみて、この出力のforccを該当のコードに代入してみてはいかがでしょうか。
fourcc = cv2.VideoWriter_fourcc("ここに代入")

0Like

Comments

  1. @nobu6787

    Questioner

    ありがとうございます。現状ご指示頂いたのを参考に試行錯誤しています。
    ありがとうございました。

Your answer might help someone💌