Webcam motion detector thingy
Posted: Thu Apr 16, 2009 5:18 pm
Since I'm planning to make Terminators to take over the world I started messing around with how to analyze frames from my webcam (the AI needs to be able to see). With this piece of code (which is just for fun) I'm able to detect motion and to demonstrate this I visualize it with green boxes.
Feel free to tweak and improve, it would be cool if anyone help me make it detect moving objects, like a hand for example and then make it so you can use your hand to move the cursor. Or detect faces, etc.
Edit:
If it is slow then run without debugger, wow what a speed
Feel free to tweak and improve, it would be cool if anyone help me make it detect moving objects, like a hand for example and then make it so you can use your hand to move the cursor. Or detect faces, etc.

Edit:
If it is slow then run without debugger, wow what a speed

Code: Select all
EnableExplicit
;{ CAP constants
#WM_CAP_START = #WM_USER
#WM_CAP_DRIVER_CONNECT = #WM_CAP_START + 10
#WM_CAP_DRIVER_DISCONNECT = #WM_CAP_START + 11
#WM_CAP_DRIVER_GET_CAPS = #WM_CAP_START + 14
#WM_CAP_EDIT_COPY = #WM_CAP_START + 30
#WM_CAP_SET_PREVIEW = #WM_CAP_START + 50
#WM_CAP_SET_PREVIEWRATE = #WM_CAP_START + 52
#WM_CAP_STOP = #WM_CAP_START + 68
#WM_CAP_SET_SCALE = #WM_CAP_START + 53
#WM_CAP_START = #WM_USER
#WM_CAP_DLG_VIDEOSOURCE = #WM_CAP_START + 42
;}
#Main=0
#camWidth = 640
#camHeight = 480
#w16 = 40
#h16 = 30
Structure avg
r.l
g.l
b.l
EndStructure
Global hWndC
Define Ccam_lib1, *capAddress
Procedure rotate90(*MemoryTarget,W,H,size)
Protected X,Y,Target, Origin, *MemoryOrigin = AllocateMemory(size)
CopyMemory(*MemoryTarget,*MemoryOrigin,size)
For Y = 0 To H - 1
For X = 0 To W - 1
Origin = (Y * W + X) << 2
Target = ((H - Y - 1) + (X * H)) << 2
PokeL(*MemoryTarget + Target, PeekL(*MemoryOrigin + Origin))
Next
Next
FreeMemory(*MemoryOrigin)
EndProcedure
Procedure greyScale(*Memory,MemorySize)
Protected Counter, Color
For Counter = 0 To MemorySize - 1 Step 4
Color = PeekL(*Memory + Counter)
Color = (Red(Color) + Green(Color) + Blue(Color)) / 3
PokeL(*Memory + Counter, RGB(Color, Color, Color))
Next
EndProcedure
Procedure captureImage(dummy)
Protected image, bmp.BITMAP, imageID, imageSize, *bits, pos
Protected Dim prev.avg(#w16,#h16)
Protected x,y,yy,xx, avg.avg, color, diff
Repeat
SendMessage_(hWndC,#WM_CAP_EDIT_COPY,0,0)
image = GetClipboardImage(#PB_Any,32)
imageID = ImageID(image) ;maybe flip it first
GetObject_(imageID,SizeOf(BITMAP),@bmp.BITMAP)
imageSize = bmp\bmWidthBytes * bmp\bmHeight
*bits = bmp\bmBits
StartDrawing(WindowOutput(0))
DrawImage(imageID,0,0)
DrawingMode(#PB_2DDrawing_Outlined)
For x=0 To #w16-1
For y=0 To #h16-1
avg\r = 0: avg\g=0: avg\b=0
For xx=x*16 To x*16+16-1
For yy=y*16 To y*16+16-1
pos = (yy * bmp\bmWidth + xx) << 2
color = PeekL(*bits+pos)
avg\r + Red(color)
avg\g + Green(color)
avg\b + Blue(color)
;Plot(xx,479-yy,color)
Next
Next
avg\r = avg\r / 256
avg\g = avg\g / 256
avg\b = avg\b / 256
;Box(x*16,y*16,16,16,RGB(avg\r,avg\g,avg\b))
diff = 0
diff + Abs(prev(x,y)\r - avg\r)
diff + Abs(prev(x,y)\g - avg\g)
diff + Abs(prev(x,y)\b - avg\b)
If diff > 20
Box(x*16,464-y*16,16,16,#Green)
;Circle(x*16+8,464-y*16+8,8,#Green) or circles if you want
EndIf
prev(x,y)\r = avg\r
prev(x,y)\g = avg\g
prev(x,y)\b = avg\b
Next
Next
;DrawImage(imageID,0,0)
StopDrawing()
FreeImage(image)
;Delay(2000)
ForEver
EndProcedure
If OpenWindow(#Main,0,0,640,480,"Webcam Motion Detector by JLC",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
Ccam_lib1 = OpenLibrary(#PB_Any, "avicap32.dll")
If Ccam_lib1
*capAddress = GetFunction(Ccam_lib1, "capCreateCaptureWindowA")
hWndC = CallFunctionFast(*capAddress, "My Capture Window", #WS_CHILD | #WS_VISIBLE, 0,0, 1 , 1, WindowID(#Main),0)
SendMessage_(hWndC, #WM_CAP_DRIVER_CONNECT, 0, 0)
SendMessage_(hWndC, #WM_CAP_SET_OVERLAY, #True, 0)
SendMessage_(hWndC, #WM_CAP_SET_PREVIEW, #True, 0)
SendMessage_(hWndC, #WM_CAP_SET_PREVIEWRATE, 1, 0)
EndIf
EndIf
CreateThread(@captureImage(),0)
Repeat
Delay(10)
Until WaitWindowEvent() = #PB_Event_CloseWindow
SendMessage_(hWndC,#WM_CAP_STOP,0,0)
SendMessage_(hWndC,#WM_CAP_DRIVER_DISCONNECT,0,0)
DestroyWindow_(hWndC)
CloseLibrary(Ccam_lib1)