- Turret project (hardware and python questions)
- 09 Jul 2008 06:40:12 pm
- Last edited by Harq on 15 Jul 2008 08:37:03 pm; edited 13 times in total
I recently picked up python again (I barely did anything last time >.> ), and I have decided on a project I want to do to A. work on my Python skills and B. be really awesome =P
The project I have chosen is basically a paintball sentry gun (although I will just start with a laserpointer) I saw the sentry gun that a person made on youtube and now I really want to try to make one myself >.>
the project is going to be incredibly advanced for me, but thank
I am doing relatively well with one portion of the project, I have been doing extensive research on the topic and finding as many examples as I can in Python (most are in c and c++ and use libraries with no python binding, but I think I finally found a good one - Opencv has python bindings). The harder part for me, however, is going to be the hardware.
Does anybody have any good suggestions on
1. good motors that could rotate a paintball gun or something of similar mass up and down and around, but also easily interfaced with a computer (I will probably end up having a friend of mine get me some motors and wiring it, because I suck with hardware)
2. a good tutorial on controlling motors in python (unfortunately the computer I am using does not have a serial port, but I am sure there are some usb->serial converters somewhere, my mom may actually have one)
it is possible this project will die, but it is something that I really want to try =)
CURRENT CODE: must have webcam, PyGame, Python Imaging Library and VideoCapture (google them)
I also recommend Psyco since it seems to yield a noticeable speed difference (although it may be just a phychological effect >.> )
If you do have Psyco, uncomment the last 2 lines of import (the ones with psyco in them =P )
After a lot of work I finally retried the flood fill method for blob detection, and it turns out the method I used before sucked >.>
The method now is completely iterative and it keeps up with my frame rate of 24 fps (the cpu usage IS 30%, but this would be the only thing running at any time >.> )
If you have not tried it yet, TRY IT NOW!
When running this code, SIT OUT OF THE WAY UNTIL YOU SEE A VIDEO FEED (this is so it can get the background etc. if you move out of the way afterwards, it will redo the background after a couple seconds, I have the refresh set to 50 loops of the number of targets being the same, it does not have to be consecutive)
try throwing a small object into the scene, or running in and taking something out, or moving it slightly, just to see how sensitive it is =)
Code:
The project I have chosen is basically a paintball sentry gun (although I will just start with a laserpointer) I saw the sentry gun that a person made on youtube and now I really want to try to make one myself >.>
the project is going to be incredibly advanced for me, but thank
I am doing relatively well with one portion of the project, I have been doing extensive research on the topic and finding as many examples as I can in Python (most are in c and c++ and use libraries with no python binding, but I think I finally found a good one - Opencv has python bindings). The harder part for me, however, is going to be the hardware.
Does anybody have any good suggestions on
1. good motors that could rotate a paintball gun or something of similar mass up and down and around, but also easily interfaced with a computer (I will probably end up having a friend of mine get me some motors and wiring it, because I suck with hardware)
2. a good tutorial on controlling motors in python (unfortunately the computer I am using does not have a serial port, but I am sure there are some usb->serial converters somewhere, my mom may actually have one)
it is possible this project will die, but it is something that I really want to try =)
CURRENT CODE: must have webcam, PyGame, Python Imaging Library and VideoCapture (google them)
I also recommend Psyco since it seems to yield a noticeable speed difference (although it may be just a phychological effect >.> )
If you do have Psyco, uncomment the last 2 lines of import (the ones with psyco in them =P )
After a lot of work I finally retried the flood fill method for blob detection, and it turns out the method I used before sucked >.>
The method now is completely iterative and it keeps up with my frame rate of 24 fps (the cpu usage IS 30%, but this would be the only thing running at any time >.> )
If you have not tried it yet, TRY IT NOW!
When running this code, SIT OUT OF THE WAY UNTIL YOU SEE A VIDEO FEED (this is so it can get the background etc. if you move out of the way afterwards, it will redo the background after a couple seconds, I have the refresh set to 50 loops of the number of targets being the same, it does not have to be consecutive)
try throwing a small object into the scene, or running in and taking something out, or moving it slightly, just to see how sensitive it is =)
Code:
#http://www.codeproject.com/KB/audio-video/Motion_Detection.aspx
#http://www.pythonware.com/library/pil/handbook/image.htm#image-mode-attribute
#take stream from webcam
#get initial frame
#subtract initial frame from subsequent frames
#check for differences
#draw rectangles around differences, rate by size
#########################################################
import Image, ImageChops, ImageFilter
import pygame
from pygame.locals import *
from VideoCapture import Device
import sys
import psyco
psyco.full()
REFRESH = 50
fps = 24
cam = Device(devnum=0)
#Device.displayCapturePinProperties(cam)
Device.displayCaptureFilterProperties(cam)
def get_image():
return Device.getImage(cam, timestamp=0, boldfont=0, textpos='bl')
def flood((x,y), temparray,retlist):
"""takes in current x and y coordinated, and then iterates through
temp array starting at x and y, trying to come back around to the beginning
"""
temparray[x,y] = 0
retlist.append([x,y])
if temparray[x,y-1] == 255:
flood((x,y-1), temparray,retlist)
if temparray[x+1,y] == 255:
flood((x+1,y), temparray,retlist)
if temparray[x,y+1] == 255:
flood((x,y+1), temparray,retlist)
if temparray[x-1,y] == 255:
flood((x-1,y), temparray,retlist)
return retlist
def flood_fill(temparray):
blist = []
for yline in xrange(120):
for xline in [0,159]:
temparray[xline,yline] = 0
for yline in [0,119]:
for xline in xrange(160):
temparray[xline,yline] = 0
for yline in xrange(1,119):
for xline in xrange(1,159):
if temparray[xline,yline] == 255:
retlist = []
tlist = flood((xline,yline),temparray,retlist)
xlist = []
ylist = []
for a in xrange(len(tlist)):
xlist.append(tlist[a][0])
ylist.append(tlist[a][1])
blist.append([min(xlist)*4,max(xlist)*4,min(ylist)*4,max(ylist)*4])
return blist
pygame.init()
res = (640,480)
pygame.init()
screen = pygame.display.set_mode((800,480))
pygame.display.set_caption('Webcam')
def get_image():
return Device.getImage(cam, timestamp=0, boldfont=0, textpos='bl')
lowpix = 35
backgroundframe = get_image()
backgroundframe = backgroundframe.resize((160,120))
backgroundframe = backgroundframe.convert("L",dither=Image.NONE)
averagex = 370
averagey = 240
blobs = 0
lblobs = 0
conblobs = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
keyinput = pygame.key.get_pressed()
if keyinput[K_1]: fps += 1
if keyinput[K_2]: fps -= 1
if keyinput[K_3]: lowpix += 1
if keyinput[K_4]: lowpix -= 1
if keyinput[K_5]:
backgroundframe = get_image()
backgroundframe = backgroundframe.resize((160,120))
backgroundframe = backgroundframe.convert("L",dither=Image.NONE)
print fps
dispimg = get_image()
compimg = dispimg.resize((160,120)).convert("L",dither=Image.NONE)
difframe = ImageChops.difference(backgroundframe, compimg)
difframe = difframe.point(lambda p: (p>lowpix) * 255.0)
difframe = difframe.filter(ImageFilter.ModeFilter)
difframe = difframe.filter(ImageFilter.MaxFilter)
difframe = difframe.filter(ImageFilter.MaxFilter)
difframe = difframe.filter(ImageFilter.MaxFilter)
pgimg2 = difframe.convert("RGB")
if difframe.histogram()[255] < 10000:
repdifframe = difframe
tarray = repdifframe.load()
bloblist = flood_fill(tarray)
blobs = len(bloblist)
else:
backgroundframe = get_image()
backgroundframe = backgroundframe.resize((160,120))
backgroundframe = backgroundframe.convert("L",dither=Image.NONE)
if blobs:
print [lblobs,blobs,conblobs]
if lblobs == blobs:
conblobs += 1
if conblobs > REFRESH:
backgroundframe = get_image()
backgroundframe = backgroundframe.resize((160,120))
backgroundframe = backgroundframe.convert("L",dither=Image.NONE)
conblobs = 0
if blobs == 0:
conblobs = 0
lblobs = blobs
pgimg = pygame.image.frombuffer(dispimg.tostring(), res, "RGB")
pgimg2 = pygame.image.frombuffer(pgimg2.tostring(), (160,120), "RGB")
pgimg = pgimg.convert()
pgimg2 = pgimg2.convert()
screen.blit(pgimg, (0,0))
screen.blit(pgimg2, (640,0))
if len(bloblist)>0:
for tlist in bloblist:
averagex = (tlist[0]+tlist[1])/2
averagey = (tlist[2]+tlist[3])/2
pygame.draw.circle(screen, (255, 0, 0), (averagex, averagey), 10, 5)
pygame.draw.line(screen, (255,0,0), (averagex,averagey-16), (averagex,averagey+16), 3)
pygame.draw.line(screen, (255,0,0), (averagex-16,averagey), (averagex+16,averagey), 3)
pygame.draw.rect(screen, (255,0,0),(tlist[0],tlist[2],tlist[1]-tlist[0],tlist[3]-tlist[2]), 3)
pygame.display.flip()
pygame.time.delay(int(1000 * 1.0/fps))