Python 类型错误:can';t将序列乘以类型为'的非整数;模块';
初级水平 在这段代码中,Python 类型错误:can';t将序列乘以类型为'的非整数;模块';,python,opencv,computer-vision,sequence,Python,Opencv,Computer Vision,Sequence,初级水平 在这段代码中,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 fram
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回溯(最近一次调用上次)
in()32 33
cv2.imshow('帧',帧)--->34距离=速度*时间35如果
距离==起始距离或距离==中间距离或距离==
总距离:36类型错误:不能将序列乘以的非整数
类型“模块”
问题是您正在乘以
时间
并且它是时间
模块的名称,您应该使用其中一种时间方法并使用它进行乘法。可能是开始时间,但我不知道这个变量的确切用途
您还需要将速度
强制转换为int,以便在乘法中使用它。我想你想得到一个int
来自文档
然后在你的循环中:
out.write(frame)
让我知道这是否有效因为
时间
是一个包而不是变量,您需要创建一个变量来保存当前时间:
current_time = time.time()
distance = speed * current_time
或者,您可以通过以下方式获取当前时间:
distance = speed * time.time()
别误会我。我喜欢Python,但这就是为什么Perl和PHP等语言使用诸如
$
或@
之类的符号来表示变量的一个例子。在这些语言中,一个名为time
的变量包含一个值,它将被写入$time
,而具有相同名称的函数(或模块)将被写入time
。因此,很难将两者混淆。请在speed*time
time
变量未定义且time
是您通过import time
导入的模块时,显示您正在获取的错误。@Chritopher Bottonms()32 33 cv2.imshow('frame',frame)中的TypeError回溯(最近一次调用)-->34 distance=speed*time 35如果distance==start\u distance或distance==mid\u distance或distance==total\u distance:36 TypeError:无法将序列乘以'module'类型的非int,我已经替换了time.time(),并且我也将速度强制转换为int,但现在它只拍摄视频。它没有捕获图像,会产生什么错误?需要的输出是什么,您有哪个输出?它只是播放视频,没有提到错误,也没有捕获图像
distance = speed * time.time()