初級レベルTypeError例外:非整数型の「モジュール」によってシーケンスを乗算することができない
このコードspeed
では、キーボード入力され、distance
は、起動している中間及び終了値と時間が変化するので、私は書かれていましたdistance = speed * time
の式を使用するコードが動作していません。
なぜこのエラーが発生するのですか?解決方法は何か?
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0) #input object
# frames iterator
i = 0;
# To capture number of frames
no_of_frames = 0
# coordinates to print time on x and y location
x = 30
y = 30
# take speed as input from keyboard
speed = input()
#total distance
start_distance = 0
total_distance = 75
mid_distance = int(total_distance/2)
# video start time
start_time = time.time()
# start capturing frames
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
cv2.imshow('frame',frame)
distance = speed * time
# capture frame
#if t == start_distance or t == mid_distance or t == total_distance:
if distance == start_distance or distance == mid_distance or distance == total_distance:
# calculate time
hours, rem = divmod(time.time() - start_time, 3600)
minutes, seconds = divmod(rem, 60)
# set elapsed time
elapsed_time = "{:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds)
# print elapsed time on frame
cv2.putText(frame,str(elapsed_time), (x,y), cv2.FONT_HERSHEY_PLAIN, 1, 255)
# capture frame
cv2.imwrite('output_frame_'+str(i)+'.jpg',frame)
no_of_frames = no_of_frames + 1
#print(t)
i=i+1
if no_of_frames >=3:
break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
#out.release()
cv2.destroyAllWindows()
私はこのエラーを取得する:
TypeError Traceback (most recent call last) in() 32 33 cv2.imshow('frame',frame) ---> 34 distance = speed * time 35 if distance == start_distance or distance == mid_distance or distance == total_distance: 36 TypeError: can't multiply sequence by non-int of type 'module'
エラーを表示してください –
'speed * time'では' time'変数が定義されておらず、 'time'は' import time'でインポートしたモジュールなのでエラーです。で –
Pavan
@Chritopher BottonmsはTypeErrorトレースバック(最新のコール最後)() 32 33 cv2.imshow( 'フレーム'、フレーム) ---> 34距離=速度*時間 距離==開始距離または距離==中央距離または距離== total_distance: TypeError: 'module'タイプの非整数でシーケンスを掛けることはできません –
Sober