IndexError:列表索引超出范围 - 如果我的运动传感器无法检测到运动,为什么我的运动传感器会崩溃或检测到太多?
我一直在使用Python和Pygame构建一个移动传感器,它基本上拍摄了您的相机镜头的图片,将每个像素的RGB值与最后拍摄的图片进行比较,并显示在Pygame屏幕中更改的像素。由于某种原因,如果它检测到过多的移动,例如您覆盖相机,或者它检测到太麻烦,例如您停止移动一段时间会崩溃。
代码:
import pygame
from pygame.locals import *
import pygame.camera
import pygame.image
import numpy as np
import cv2
pygame.init()
pygame.camera.init()
pygame.display.set_caption("Game")
cameras = pygame.camera.list_cameras()
print("Using camera %s ..." % cameras[0])
cam1 = pygame.camera.Camera(cameras[0])
cam1.start()
img1 = cam1.get_image()
flags = DOUBLEBUF
screen = pygame.display.set_mode((500,500), flags, 4)
frame = 0
def take_screen_shot():
global frame
save_file = str(frame)+'.png'#save file
pygame.image.save(img1, save_file)
def image_proccesing():
global frame
global counter
if frame >= 1:
img1 = cv2.imread(str(frame - 1)+'.png', 0)
img2 = cv2.imread(str(frame)+'.png', 0)
sub = cv2.subtract(img1, img2)
coords = np.argwhere(sub > 100)#checks if the pixels have a difference bigger than 100
coords_list = coords.tolist()#converts the array into a list
x = list(list(zip(*coords_list))[0])#grabs the first value, in this case x coordenates
y = list(list(zip(*coords_list))[1])#grabs the first value, in this case y coordenates
for i in range(len(x)):
pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(y[i], x[i], 1, 1))
x.clear()
y.clear()
clock = pygame.time.Clock()
while 1:
screen.fill((0, 0, 0))
for event in pygame.event.get():
pygame.event.set_allowed(KEYDOWN)
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
pygame.camera.quit()
img1 = cam1.get_image().convert()
take_screen_shot()
image_proccesing()
clock.tick(15)
pygame.display.update()
frame += 1
错误:
x = list(list(zip(*coords_list))[0])
IndexError: list index out of range
I have been building a movement sensor using python and pygame, it basically takes a picture of your camera footage, compares the RGB value of every pixel to the last picture taken and displays the pixels that have changed in the pygame screen. For some reason if it detects too much movement eg you cover the camera, or if it detects too litte eg you stop moving for a while it will crash.
code:
import pygame
from pygame.locals import *
import pygame.camera
import pygame.image
import numpy as np
import cv2
pygame.init()
pygame.camera.init()
pygame.display.set_caption("Game")
cameras = pygame.camera.list_cameras()
print("Using camera %s ..." % cameras[0])
cam1 = pygame.camera.Camera(cameras[0])
cam1.start()
img1 = cam1.get_image()
flags = DOUBLEBUF
screen = pygame.display.set_mode((500,500), flags, 4)
frame = 0
def take_screen_shot():
global frame
save_file = str(frame)+'.png'#save file
pygame.image.save(img1, save_file)
def image_proccesing():
global frame
global counter
if frame >= 1:
img1 = cv2.imread(str(frame - 1)+'.png', 0)
img2 = cv2.imread(str(frame)+'.png', 0)
sub = cv2.subtract(img1, img2)
coords = np.argwhere(sub > 100)#checks if the pixels have a difference bigger than 100
coords_list = coords.tolist()#converts the array into a list
x = list(list(zip(*coords_list))[0])#grabs the first value, in this case x coordenates
y = list(list(zip(*coords_list))[1])#grabs the first value, in this case y coordenates
for i in range(len(x)):
pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(y[i], x[i], 1, 1))
x.clear()
y.clear()
clock = pygame.time.Clock()
while 1:
screen.fill((0, 0, 0))
for event in pygame.event.get():
pygame.event.set_allowed(KEYDOWN)
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
pygame.camera.quit()
img1 = cam1.get_image().convert()
take_screen_shot()
image_proccesing()
clock.tick(15)
pygame.display.update()
frame += 1
error:
x = list(list(zip(*coords_list))[0])
IndexError: list index out of range
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了错误,我必须检查文件是否具有任何值。我通过更改
image_proccesing
函数来做到这一点。image_proccesing:
I have found out the error, I had to check if the files had any values. I did this by changing the
image_proccesing
function.image_proccesing: