 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
fures
Joined: 31 Jan 2008 Posts: 29
|
Posted: Mon Apr 07, 2008 11:39 pm Post subject: Webcam capture (WDM) with motion detection in AHK |
|
|
Capture code originated from here:
http://www.autohotkey.com/forum/viewtopic.php?t=17042&highlight=webcam
and here:
http://www.autohotkey.com/forum/viewtopic.php?t=29166&highlight=webcam
Here's a small and fast WDM/WfW webcam capture solution with motion detection in AHK - based on ImageSearch.
I was surprized how fast ImageSearch can be...
| Code: |
;#NoTrayIcon
#SingleInstance FORCE
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Persistent
CoordMode Pixel, Screen ; Interprets the coordinates below as relative to the screen rather than the active window.
;defaults
WDT := 320 ;width
HGT := 240 ;height
FPS := 15 ;frame per second
fileJPG = vidcap.jpg ;capture file name on the desktop - jpg
fileBMP = vidcap.bmp ;capture file name on the desktop - bmp
SENS := 80 ;motion detect sensitivity% (0%-100%)
MotionCheckTime := 500 ;msec - time period to check for motion
hModule := DllCall("LoadLibrary", "str", "avicap32.dll")
Gui, +lastfound
mgH := WinExist()
Gui, Add, GroupBox, x4 y4 w492 h100, Available Video Drivers
Gui, Add, ListView, x8 y20 w400 h80 gSelectDriver vCapDriversLV, Index|Name
Gui, Add, Picture, x434 y16 w32 h32 Icon204, %A_WinDir%\system32\shell32.dll
Gui, Add, Button, x412 y50 w80 h24 gRefreshDrivers, Refresh
Gui, Add, Button, x412 y76 w80 h24 gSelectDriver vSelectDriverB, Select
; --- Video preview section of Gui
Gui, Add, GroupBox, x4 y108 w492 h262, Video
Gui, Add, CheckBox, x10 y120 w100 h24 vPreviewToggleState gPreviewToggle, Preview video
Gui, Add, Text, x10 y160, width:
Gui, Add, Edit, x70 y160 w30 vWDT gdoWDT, %WDT%
Gui, Add, Text, x10 y190, height:
Gui, Add, Edit, x70 y190 w30 vHGT gdoGHT, %HGT%
Gui, Add, Text, x10 y220, fps:
Gui, Add, Edit, x70 y220 w30 vFPS gdoFPS, %FPS%
Gui, Add, Button, x110 y190 h24 gPreviewToggle, Change
Gui, Add, Button, x400 y160 h24 gCopyToClipBoard, Copy
Gui, Add, Text, x240 y193, Desktop\
Gui, Add, Edit, x290 y190 w100 vfileJPG gdoJPG, %fileJPG%
Gui, Add, Button, x400 y190 h24 gSenToFile2, Send to &JPG
Gui, Add, Text, x240 y223, Desktop\
Gui, Add, Edit, x290 y220 w100 vfileBMP gdoBMP, %fileBMP%
Gui, Add, Button, x400 y220 h24 gSenToFile, Send to &BMP
Gui, Add, Text, x10 y250, Sensitivity `%
Gui, Add, Edit, x90 y250 w30 vSENS gdoSENS, %SENS%
Gui, Add, Text, x10 y280, Motion check:
Gui, Add, Edit, x90 y280 w30 vMotionCheckTime gdoMOT, %MotionCheckTime%
Gui, Add, Text, x130 y280, msec
Gui, Add, Button, Disabled x130 y250 h24 gMotionToggle, Motion ON
Gui, Font, cGray s14 bold, Arial
Gui, Add, Text, x10 y320 w300,
GoSub, RefreshDrivers
Gui, Show, x200 w500 h400, Video For Windows for AutoHotkey - VFW4AHK
Return
doMOT:
ControlGetText,MotionCheckTime,Edit7,A
If MotionToggleState
SetTimer, CompareImages, % MotionCheckTime
Return
doSENS:
ControlGetText,SENS,Edit6,A
Return
doWDT:
ControlGetText,WDT,Edit1,A
Return
doGHT:
ControlGetText,HGT,Edit2,A
Return
doJPG:
ControlGetText,fileJPG,Edit4,A
Return
doBMP:
ControlGetText,fileBMP,Edit5,A
Return
doFPS:
ControlGetText,FPS,Edit3,A
Return
PreviewToggle:
ControlGet,PreviewToggleState,Checked,,Button5,A
If PreviewToggleState
{
Control,Enable,,Button10,A
Gui, 2:Destroy
Gui, 2:Add, Text, x0 y0 w%WDT% h%HGT% vVidPlaceholder
GuiControl, +0x7, VidPlaceholder ; frame
Gui 2:+LastFound
hwndParent := WinExist()
Gui, 2:Show, x750 w%WDT% h%HGT%, Viewer
GoSub ConnectToDriver
}
Else
{
Control,Disable,,Button10,A
Gui, 2:Destroy
GoSub DisconnectDriver
}
Return
MotionToggle:
If MotionToggleState
{
MotionToggleState =
ControlSetText,Button10,Motion ON,ahk_id %mgH%
SetTimer, CompareImages, Off
Sleep % 2*MotionCheckTime
ControlSetText,Static10,,ahk_id %mgH%
}
Else
{
MotionToggleState = 1
ControlSetText,Button10,Motion OFF,ahk_id %mgH%
SetTimer, CompareImages, % MotionCheckTime
}
Return
ConnectToDriver:
; --- Connect and preview - hwnd, x, y, w, h
capHwnd := Cap_CreateCaptureWindow(hwndParent, 0, 0, WDT, HGT)
WM_USER = 0x0400
WM_CAP_START := WM_USER
WM_CAP_GRAB_FRAME_NOSTOP := WM_USER + 61
WM_CAP_FILE_SAVEDIB := WM_CAP_START + 25
WM_CAP := 0x400
WM_CAP_DRIVER_CONNECT := WM_CAP + 10
WM_CAP_DRIVER_DISCONNECT := WM_CAP + 11
WM_CAP_EDIT_COPY := WM_CAP + 30
WM_CAP_SET_PREVIEW := WM_CAP + 50
WM_CAP_SET_PREVIEWRATE := WM_CAP + 52
WM_CAP_SET_SCALE := WM_CAP + 53
; Connect to driver
if SelectedDriver =
{
if foundDriver
SelectedDriver = 0
else
{
MsgBox, 16, Error!, You didn't select a video driver`, and there seems to be no driver present.
Return
}
}
SendMessage, WM_CAP_DRIVER_CONNECT, %SelectedDriver%, 0, , ahk_id %capHwnd%
; Set the preview scale
SendMessage, WM_CAP_SET_SCALE, 1, 0, , ahk_id %capHwnd%
; Set the preview rate in milliseconds
MSC := round((1/FPS)*1000)
SendMessage, WM_CAP_SET_PREVIEWRATE, MSC, 0, , ahk_id %capHwnd%
; Start previewing the image from the camera
SendMessage, WM_CAP_SET_PREVIEW, 1, 0, , ahk_id %capHwnd%
Return
CompareImages:
imagefile = %A_Desktop%\comp.BMP
SendMessage, WM_CAP_FILE_SAVEDIB, 0, &imagefile, , ahk_id %capHwnd%
Sensitivity := (100-SENS)/100*255
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *%Sensitivity% *w%WDT% *h%HGT% %imagefile%
if ErrorLevel = 2
ControlSetText,Static10,Search error,ahk_id %mgH%
else if ErrorLevel = 1
ControlSetText,Static10,MOTION!!!,ahk_id %mgH%
else
ControlSetText,Static10,STILL,ahk_id %mgH%
Return
CopyToClipBoard:
SendMessage, WM_CAP_EDIT_COPY, 0, 0, , ahk_id %capHwnd%
Return
SenToFile2:
SendMessage, WM_CAP_EDIT_COPY, 0, 0, , ahk_id %capHwnd%
RunWait, C:\Program Files\IrfanView\i_view32.exe /clippaste /convert=%A_Desktop%\%fileJPG% ;copies from clipboard to file
Return
SenToFile:
imagefile = %A_Desktop%\%fileBMP%
SendMessage, WM_CAP_FILE_SAVEDIB, 0, &imagefile, , ahk_id %capHwnd%
return
DisconnectDriver:
SendMessage, WM_CAP_DRIVER_DISCONNECT, 1, 0, , ahk_id %capHwnd%
Return
RefreshDrivers:
foundDriver = 0
LV_Delete()
Loop
{
thisInfo := Cap_GetDriverDescription(A_Index-1)
If thisInfo
{
foundDriver = 1
LV_Add("", A_Index-1, thisInfo)
}
Else
Break
}
If !foundDriver
{
LV_Delete()
LV_Add("", "", "Could not get video drivers")
GuiControl, Disable, CapDriversLV
GuiControl, Disable, SelectDriverB
}
Return
SelectDriver:
FocusedRowNumber := LV_GetNext(0, "F") ; Find the focused row.
if not FocusedRowNumber ; No row is focused.
return
LV_GetText(SelectedDriver, FocusedRowNumber, 1)
Return
Cap_CreateCaptureWindow(hWndParent, x, y, w, h)
{
WS_CHILD := 0x40000000
WS_VISIBLE := 0x10000000
lpszWindowName := "test"
lwndC := DLLCall("avicap32.dll\capCreateCaptureWindowA"
, "Str", lpszWindowName
, "UInt", WS_VISIBLE | WS_CHILD ; dwStyle
, "Int", x
, "Int", y
, "Int", w
, "Int", h
, "UInt", hWndParent
, "Int", 0)
Return lwndC
}
Cap_GetDriverDescription(wDriver)
{
VarSetCapacity(lpszName, 100)
VarSetCapacity(lpszVer, 100)
res := DLLCall("avicap32.dll\capGetDriverDescriptionA"
, "Short", wDriver
, "Str", lpszName
, "Int", 100
, "Str", lpszVer
, "Int", 100)
If res
capInfo := lpszName ; " | " lpszVer
Return capInfo
}
GuiClose:
GoSub, DisconnectDriver
DllCall("FreeLibrary", "str", hModule)
ExitApp
Return
|
Anybody has any idea how to capture video from the gui in AHK?
cheers,
fures[/url]
Last edited by fures on Tue Apr 08, 2008 4:08 pm; edited 2 times in total |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Tue Apr 08, 2008 3:12 am Post subject: |
|
|
| I see nice preview, but when I save a snapshot, my changed resolution is not honored, it is always 320x240 pixel. When I move one snapshot is saved in bmp format, but then nothing more. Could you give a little more detailed description? |
|
| Back to top |
|
 |
System Monitor
Joined: 09 Mar 2007 Posts: 392 Location: Unknown
|
Posted: Tue Apr 08, 2008 4:53 am Post subject: |
|
|
Not even getting a preview. Here is the properties window for my camera.
 _________________
 |
|
| Back to top |
|
 |
fures
Joined: 31 Jan 2008 Posts: 29
|
Posted: Tue Apr 08, 2008 9:10 am Post subject: |
|
|
Okay, a little more info:
1. Start the script
2. In the top listview are the available WfW capture sources
(SysteMonitor - you might not have a WFW driver for your camera)
3. You select the source you whish. (either doubleclick on the row or select the row and click the select button)
4. Click on the Preview checkbox to open a new gui with the preview of your above selected camera.
5. The preview has 3 parameters: width, height, fps (frames/sec). These parameters can be changed on the fly by clicking the Change button. This action would re-create the preview gui with the changed parameters.
6. There are 3 buttons on the right: Copy, Send to JPG, Send to BMP. Clicking these would capture the current frame and drop it either on the clipboard (copy), or into a file (.jpg or .bmp) on the desktop. The captured images have the size of your camera driver setup - not the size of the preview gui.
The motion part:
7. Adjust sensitivity % from 0-100 (around 80 seems OK for me)
8. Adjust Motion check period (in msec) - 500 checks motion 2x per sec
9. When you click Motion ON button - the program starts motion detection:
Motion detection saves a reference image (on the desktop named comp.BMP) every time period you've given above (in 8.) and then compares the actual camera image to the reference image with the given sensitivity (in 7.).
In case of a motion, at the bottom of the main Gui you see a large text: MOTION!!!
In case of no motion, the text changes to STILL.
It does not save captured images or video at this stage - for it is experimental at this moment.
Hope it helps.
fures |
|
| Back to top |
|
 |
twhyman
Joined: 07 Dec 2005 Posts: 218
|
Posted: Tue Apr 08, 2008 9:18 am Post subject: |
|
|
WOW, this is amazing what you did
I got it working on my laptop and and it seems to work great.
Very nice demo, i am sure i will use it some day
Thanks
Twhyman |
|
| Back to top |
|
 |
daonlyfreez
Joined: 16 Mar 2005 Posts: 745 Location: Berlin
|
Posted: Tue Apr 08, 2008 10:37 am Post subject: |
|
|
Very nice!
Good to see the code is being used and developed further.  _________________ (sorry, homesite offline atm) |
|
| Back to top |
|
 |
fures
Joined: 31 Jan 2008 Posts: 29
|
Posted: Tue Apr 08, 2008 10:50 am Post subject: my questions |
|
|
The questions I have:
1. Any ideas about saving a video (using codecs) from this preview gui?
2. How to enlist directX capture devices rather than WDM/WfW?
keep talking...
fures |
|
| Back to top |
|
 |
fures
Joined: 31 Jan 2008 Posts: 29
|
Posted: Tue Apr 08, 2008 11:13 am Post subject: |
|
|
| daonlyfreez wrote: | Very nice!
Good to see the code is being used and developed further.  |
Yes, I've tried to give an origin reference in the first post, but as much as I know, the base code can be thanked to daonlyfreez. Thanks for it.
http://www.autohotkey.com/forum/viewtopic.php?t=17042&highlight=webcam |
|
| Back to top |
|
 |
n-l-i-daonlyfreez Guest
|
Posted: Tue Apr 08, 2008 11:20 am Post subject: |
|
|
1:
| source wrote: | | Code: | ; http://ej.bantz.com/video/
; http://ej.bantz.com/video/detail/
; http://www.dotnet4all.com/dotnet-code/2004/12/video-capture.html
|
|
2:
For DirectX/DirectVideo support, you'll need either a COM implementation:
IVideoWindow Interface
or, probably more difficult, DirectShow:
Microsoft Windows Media and DirectShow
Video programming sample source code (might help)
HTH
(PS: I have no webcam to try this with at the moment, and the coding skills are lacking too ) |
|
| Back to top |
|
 |
fures
Joined: 31 Jan 2008 Posts: 29
|
Posted: Tue Apr 08, 2008 4:04 pm Post subject: |
|
|
Thanks for this, very extensive material though, it will take time to go through it, and I have not much clue about VB/C++ programming...
I've got these calls from one of the scripts, but again, I have no clue which ones to call in which order to make a video capture.
| Code: |
Public Const WM_CAP_START = WM_USER
Public Const WM_CAP_GET_CAPSTREAMPTR = WM_CAP_START + 1
Public Const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2
Public Const WM_CAP_SET_CALLBACK_STATUS = WM_CAP_START + 3
Public Const WM_CAP_SET_CALLBACK_YIELD = WM_CAP_START + 4
Public Const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5
Public Const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6
Public Const WM_CAP_SET_CALLBACK_WAVESTREAM = WM_CAP_START + 7
Public Const WM_CAP_GET_USER_DATA = WM_CAP_START + 8
Public Const WM_CAP_SET_USER_DATA = WM_CAP_START + 9
Public Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
Public Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
Public Const WM_CAP_DRIVER_GET_NAME = WM_CAP_START + 12
Public Const WM_CAP_DRIVER_GET_VERSION = WM_CAP_START + 13
Public Const WM_CAP_DRIVER_GET_CAPS = WM_CAP_START + 14
Public Const WM_CAP_FILE_SET_CAPTURE_FILE = WM_CAP_START + 20
Public Const WM_CAP_FILE_GET_CAPTURE_FILE = WM_CAP_START + 21
Public Const WM_CAP_FILE_ALLOCATE = WM_CAP_START + 22
Public Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23
Public Const WM_CAP_FILE_SET_INFOCHUNK = WM_CAP_START + 24
Public Const WM_CAP_FILE_SAVEDIB = WM_CAP_START + 25
Public Const WM_CAP_EDIT_COPY = WM_CAP_START + 30
Public Const WM_CAP_SET_AUDIOFORMAT = WM_CAP_START + 35
Public Const WM_CAP_GET_AUDIOFORMAT = WM_CAP_START + 36
Public Const WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41
Public Const WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42
Public Const WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43
Public Const WM_CAP_GET_VIDEOFORMAT = WM_CAP_START + 44
Public Const WM_CAP_SET_VIDEOFORMAT = WM_CAP_START + 45
Public Const WM_CAP_DLG_VIDEOCOMPRESSION = WM_CAP_START + 46
Public Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
Public Const WM_CAP_SET_OVERLAY = WM_CAP_START + 51
Public Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
Public Const WM_CAP_SET_SCALE = WM_CAP_START + 53
Public Const WM_CAP_GET_STATUS = WM_CAP_START + 54
Public Const WM_CAP_SET_SCROLL = WM_CAP_START + 55
Public Const WM_CAP_GRAB_FRAME = WM_CAP_START + 60
Public Const WM_CAP_GRAB_FRAME_NOSTOP = WM_CAP_START + 61
Public Const WM_CAP_SEQUENCE = WM_CAP_START + 62
Public Const WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63
Public Const WM_CAP_SET_SEQUENCE_SETUP = WM_CAP_START + 64
Public Const WM_CAP_GET_SEQUENCE_SETUP = WM_CAP_START + 65
Public Const WM_CAP_SET_MCI_DEVICE = WM_CAP_START + 66
Public Const WM_CAP_GET_MCI_DEVICE = WM_CAP_START + 67
Public Const WM_CAP_STOP = WM_CAP_START + 68
Public Const WM_CAP_ABORT = WM_CAP_START + 69
Public Const WM_CAP_SINGLE_FRAME_OPEN = WM_CAP_START + 70
Public Const WM_CAP_SINGLE_FRAME_CLOSE = WM_CAP_START + 71
Public Const WM_CAP_SINGLE_FRAME = WM_CAP_START + 72
Public Const WM_CAP_PAL_OPEN = WM_CAP_START + 80
Public Const WM_CAP_PAL_SAVE = WM_CAP_START + 81
Public Const WM_CAP_PAL_PASTE = WM_CAP_START + 82
Public Const WM_CAP_PAL_AUTOCREATE = WM_CAP_START + 83
Public Const WM_CAP_PAL_MANUALCREATE = WM_CAP_START + 84
'// Following added post VFW 1.1
Public Const WM_CAP_SET_CALLBACK_CAPCONTROL = WM_CAP_START + 85
'// Defines end of the message range
Public Const WM_CAP_END = WM_CAP_SET_CALLBACK_CAPCONTROL
|
Anybody with relevant experience?
Meditate on it, I promise I will.  |
|
| Back to top |
|
 |
Rhys
Joined: 17 Apr 2007 Posts: 722 Location: Florida
|
Posted: Tue Apr 08, 2008 6:53 pm Post subject: |
|
|
Hmm, this is very interesting to me - There have been some people messing with my cars lately and I've been wanting to check and see the best way to set up video / photo surveillance cameras. Would this work with more than one camera at once? Now I need to find some cheap webcams that can see in the dark and have high enough resolution to positively ID people...  _________________ [Join IRC!]
 |
|
| Back to top |
|
 |
fures
Joined: 31 Jan 2008 Posts: 29
|
Posted: Fri Apr 11, 2008 5:54 pm Post subject: |
|
|
Here's a new version:
Now it can capture video:
Uncompressed AVI as default
but compression can be hand-selected (unable to save settings though
I suggest Microsoft Windows Media Video 9 codec that has the best quality/size ratio.
I gave up trying to solve saving the codec-setup.
I actually could not figure out how Microsoft saves codec data for a certain capture window. There seems to be no Struct for it.
| Code: |
;#NoTrayIcon
#SingleInstance FORCE
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Persistent
CoordMode Pixel, Screen ; Interprets the coordinates below as relative to the screen rather than the active window.
;defaults
WDT := 320 ;width
HGT := 240 ;height
FPS := 15 ;frame per second
fileJPG = vidcap.jpg ;capture file name on the desktop - jpg
fileBMP = vidcap.bmp ;capture file name on the desktop - bmp
SENS := 80 ;motion detect sensitivity% (0%-100%)
MotionCheckTime := 500 ;msec - time period to check for motion
IniFile = %A_ScriptDir%\WebCam.ini
hModule := DllCall("LoadLibrary", "str", "avicap32.dll")
Gui, +lastfound
mainHwnd := WinExist()
Gui, Add, GroupBox, x4 y4 w492 h100 , Available Video Drivers
Gui, Add, ListView, x8 y20 w400 h80 gSelectDriver vCapDriversLV AltSubmit, Index|Name
Gui, Add, Picture, x434 y16 w32 h32 Icon204, %A_WinDir%\system32\shell32.dll
Gui, Add, Button, x412 y50 w80 h24 gRefreshDrivers, Refresh
Gui, Add, Button, x412 y76 w80 h24 gSelectDriver vSelectDriverB, Select
Gui, Add, GroupBox, x6 y110 w100 h160 , Preview
Gui, Add, CheckBox, x16 y130 w80 h30 vPreviewToggleState gPreviewToggle, Preview on/off
Gui, Add, Text, x16 y170 w50 h20 , width:
Gui, Add, Edit, x66 y170 w30 h20 vWDT gdoWDT, %WDT%
Gui, Add, Text, x16 y190 w50 h20 , height:
Gui, Add, Edit, x66 y190 w30 h20 vHGT gdoGHT, %HGT%
Gui, Add, Text, x16 y210 w50 h20 , fps:
Gui, Add, Edit, x66 y210 w30 h20 vFPS gdoFPS, %FPS%
Gui, Add, Button, x16 y240 w80 h20 gPreviewToggle, Change
Gui, Add, Button, x286 y130 w200 h20 gCopyToClipBoard, Snapshot to Clipboard
Gui, Add, Text, x286 y150 w60 h20 , Desktop\
Gui, Add, Edit, x336 y150 w70 h20 vfileJPG gdoJPG, %fileJPG%
Gui, Add, Button, x406 y150 w80 h20 gSenToFile2, Send to &JPG
Gui, Add, Text, x286 y170 w60 h20 , Desktop\
Gui, Add, Edit, x336 y170 w70 h20 vfileBMP gdoBMP, %fileBMP%
Gui, Add, Button, x406 y170 w80 h20 gSenToFile, Send to &BMP
Gui, Add, Text, x146 y130 w60 h20 , Sensitivity `%
Gui, Add, Edit, x206 y130 w30 h20 vSENS gdoSENS, %SENS%
Gui, Add, Text, x146 y160 w50 h30 , Motion check:
Gui, Add, Edit, x206 y160 w30 h20 vMotionCheckTime gdoMOT, %MotionCheckTime%
Gui, Add, Text, x206 y180 w30 h20 , msec
Gui, Add, Button, x146 y200 w90 h30 Disabled gMotionToggle, Motion ON
Gui, Add, Button, x286 y290 w200 h30 gSenToAvi, Capture to &AVI
Gui, Add, Button, x376 y230 w110 h20 gSetupVideoFormat, Video Format
Gui, Add, Button, x376 y250 w110 h20 gSetupVideoCompression, Video Compression
Gui, Add, Button, x286 y230 w90 h20 gSetupVideoSource, Video Source
Gui, Add, Button, x286 y250 w90 h20 gSetupVideoDisplay, Video Display
Gui, Add, CheckBox, x286 y270 w200 h20 vSetupDefaultToggleState gSetupDefaultToggle, Save current video setup as Default
Gui, Font, cGray s14 bold, Arial
Gui, Add, Text, x146 y240 w90 h30 ,
Gui, Font
Gui, Add, GroupBox, x136 y110 w110 h170 , Motion Detection
Gui, Add, GroupBox, x276 y110 w220 h90 , Snapshot
Gui, Add, GroupBox, x276 y210 w220 h120 , Video Capture
; Generated using SmartGUI Creator 4.0
Gui, Show, x100 y125 h402 w502, Video For Windows for AutoHotkey - VFW4AHK
GoSub, RefreshDrivers
Return
doMOT:
ControlGetText,MotionCheckTime,Edit7,A
Return
doSENS:
ControlGetText,SENS,Edit6,A
Return
doWDT:
ControlGetText,WDT,Edit1,A
Return
doGHT:
ControlGetText,HGT,Edit2,A
Return
doJPG:
ControlGetText,fileJPG,Edit4,A
Return
doBMP:
ControlGetText,fileBMP,Edit5,A
Return
doFPS:
ControlGetText,FPS,Edit3,A
Return
PreviewToggle:
ControlGet,PreviewToggleState,Checked,,Button5,A
If PreviewToggleState
{
Control,Enable,,Button10,A
Gui, 2:Destroy
Gui, 2:Add, Text, x0 y0 w%WDT% h%HGT% vVidPlaceholder
GuiControl, +0x7, VidPlaceholder ; frame
Gui 2:+LastFound
previewHwnd := WinExist()
Gui, 2:Show, x650 w%WDT% h%HGT%, Viewer
GoSub ConnectToDriver
GoSub SetSequenceSetup
;MsgBox, previewHwnd = %previewHwnd% lwndC = %lwndC%
}
Else
{
Control,Disable,,Button10,A
Gui, 2:Destroy
GoSub DisconnectDriver
}
Return
SetupDefaultToggle:
ControlGet,SetupDefaultToggleState,Checked,,Button16,A
If SetupDefaultToggleState
{
GoSub GetSequenceSetup
MsgBox, DOES NOT WORK. Video setup is saved as Default
}
Else
{
Rect1 =
MsgBox, Default video setup is deleted
}
IniWrite, %Rect1%, %IniFile%, Settings, VideoSetup
Return
MotionToggle:
If MotionToggleState
{
MotionToggleState =
ControlSetText,Button10,Motion ON,ahk_id %mainHwnd%
ControlSetText,Static10,,ahk_id %mainHwnd%
Sleep % 2*MotionCheckTime
ControlSetText,Static10,,ahk_id %mainHwnd%
}
Else
{
MotionToggleState = 1
ControlSetText,Button10,Motion OFF,ahk_id %mainHwnd%
GoSub, CompareImages
}
Return
CompareImages:
SetTimer, CompareImages, Off
imagefile = %A_Desktop%\comp.BMP
SendMessage, WM_CAP_FILE_SAVEDIB, 0, &imagefile, , ahk_id %capHwnd%
Sensitivity := (100-SENS)/100*255
ErrorLevel =
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *%Sensitivity% *w%WDT% *h%HGT% %imagefile%
IfEqual, ErrorLevel, 2
ControlSetText,Static10,ERROR,ahk_id %mainHwnd%
else IfEqual, ErrorLevel, 1
ControlSetText,Static10,MOTION!!!,ahk_id %mainHwnd%
else
ControlSetText,Static10,STILL,ahk_id %mainHwnd%
If MotionToggleState
SetTimer, CompareImages, % MotionCheckTime
Return
ConnectToDriver:
; --- Connect and preview - hwnd, x, y, w, h
capHwnd := Cap_CreateCaptureWindow(previewHwnd, 0, 0, WDT, HGT)
WM_USER := 0x0400
WM_CAP_START := WM_USER
WM_CAP_GRAB_FRAME_NOSTOP := WM_USER + 61
WM_CAP_FILE_SAVEDIB := WM_CAP_START + 25
WM_CAP := 0x400
WM_CAP_DRIVER_CONNECT := WM_CAP + 10
WM_CAP_DRIVER_DISCONNECT := WM_CAP + 11
WM_CAP_EDIT_COPY := WM_CAP + 30
WM_CAP_SET_PREVIEW := WM_CAP + 50
WM_CAP_SET_PREVIEWRATE := WM_CAP + 52
WM_CAP_SET_SCALE := WM_CAP + 53
WM_CAP_SEQUENCE := WM_CAP_START + 62
WM_CAP_FILE_SET_CAPTURE_FILE := WM_CAP_START + 20
WM_CAP_DLG_VIDEOFORMAT := WM_CAP_START + 41
WM_CAP_DLG_VIDEOSOURCE := WM_CAP_START + 42
WM_CAP_DLG_VIDEODISPLAY := WM_CAP_START + 43
WM_CAP_DLG_VIDEOCOMPRESSION := WM_CAP_START + 46
WM_CAP_SET_SEQUENCE_SETUP := WM_CAP_START + 64
WM_CAP_GET_SEQUENCE_SETUP := WM_CAP_START + 65
WM_CAP_SET_AUDIOFORMAT := WM_CAP_START + 35
WM_CAP_GET_AUDIOFORMAT := WM_CAP_START + 36
WM_CAP_GET_VIDEOFORMAT := WM_CAP_START + 44
WM_CAP_SET_VIDEOFORMAT := WM_CAP_START + 45
WM_CAP_SET_OVERLAY := WM_CAP_START + 51
;DRV_USER := &H4000
DRV_USER := 0x4000
ICM_RESERVED := DRV_USER + 0x1000
ICM_CONFIGURE := (ICM_RESERVED + 10)
ICM_GETSTATE := (ICM_RESERVED + 0)
ICM_SETSTATE := (ICM_RESERVED + 1)
; Connect to driver
if SelectedDriver =
{
if foundDriver
SelectedDriver = 0
else
{
MsgBox, 16, Error!, You didn't select a video driver`, and there seems to be no driver present.
Return
}
}
SendMessage, WM_CAP_DRIVER_CONNECT, %SelectedDriver%, 0, , ahk_id %capHwnd%
; Set the preview scale
SendMessage, WM_CAP_SET_SCALE, 1, 0, , ahk_id %capHwnd%
; Set the preview rate in milliseconds
MSC := round((1/FPS)*1000)
SendMessage, WM_CAP_SET_PREVIEWRATE, MSC, 0, , ahk_id %capHwnd%
; Start previewing the image from the camera
SendMessage, WM_CAP_SET_PREVIEW, 1, 0, , ahk_id %capHwnd%
SendMessage, WM_CAP_SET_OVERLAY, 1, 0, , ahk_id %capHwnd%
Return
CopyToClipBoard:
SendMessage, WM_CAP_EDIT_COPY, 0, 0, , ahk_id %capHwnd%
Return
SenToFile2:
SendMessage, WM_CAP_EDIT_COPY, 0, 0, , ahk_id %capHwnd%
RunWait, C:\Program Files\IrfanView\i_view32.exe /clippaste /convert=%A_Desktop%\%fileJPG% ;copies from clipboard to file
Return
SenToFile:
imagefile = %A_Desktop%\%fileBMP%
SendMessage, WM_CAP_FILE_SAVEDIB, 0, &imagefile, , ahk_id %capHwnd%
return
SenToAvi:
avifile = %A_Desktop%\CAPT.avi
SendMessage, WM_CAP_FILE_SET_CAPTURE_FILE, 0, &avifile, , ahk_id %capHwnd%
ToolTip, Press Esc or Click to Stop Avi Capture
SendMessage, WM_CAP_SEQUENCE, 0, 0, , ahk_id %capHwnd%
ToolTip
return
GetSequenceSetup:
SendMessage,ICM_GETSTATE,0,0,,ahk_id %capHwnd%
SSize := ErrorLevel
;MsgBox, SSize = %SSize%
VarSetCapacity(Rect, SSize, 0) ; A RECT is a struct with 24*4
SendMessage,ICM_GETSTATE,&Rect,SSize,,ahk_id %capHwnd%
;SSize := 96
;VarSetCapacity(Rect, SSize, 0) ; A RECT is a struct with 24*4
;SendMessage,WM_CAP_GET_SEQUENCE_SETUP,SSize,&Rect,,ahk_id %capHwnd%
;does not work Res:=DllCall("avicap32.dll\capCaptureGetSetupA"
; , "UInt", previewHwnd
; , "UInt", &Rect
; , "Int", 96) ; WinExist() returns an HWND.
;VarSetCapacity(Rect,-1) ; Reset the length
MyStructData =
Loop, % round(SSize/4)
{
Offset := 4*(A_Index-1)
Rect%A_Index% := NumGet(Rect,Offset)
MyStructData := MyStructData . A_Tab . Rect%A_Index%
}
;MsgBox, MyStructData = %MyStructData%
Rect1 := SubStr(MyStructData,2)
Return
SetSequenceSetup:
VarSetCapacity(Rect, 96) ; A RECT is a struct consisting of four 32-bit integers (i.e. 4*4=16).
IfNotExist, %IniFile%
Return
IniRead, Rect1, %IniFile%, Settings, VideoSetup
If not Rect1
Return
Loop, Parse, Rect1, %A_Tab%
{
Offset := 4*(A_Index-1)
NumPut(%A_LoopField%,Rect,Offset)
}
SendMessage,WM_CAP_SET_SEQUENCE_SETUP,96,&Rect,,ahk_id %capHwnd%
; DllCall("avicap32.dll\capCaptureSetSetupA"
; , "UInt", previewHwnd
; , "UInt", &Rect
; , "Int", 96) ; WinExist() returns an HWND.
MsgBox, DOES NOT WORK. Default video setup is loaded.
Return
SetupVideoSource:
SendMessage, WM_CAP_DLG_VIDEOSOURCE, 0, 0, , ahk_id %capHwnd%
Return
SetupVideoDisplay:
SendMessage, WM_CAP_DLG_VIDEODISPLAY, 0, 0, , ahk_id %capHwnd%
Return
SetupVideoFormat:
SendMessage, WM_CAP_DLG_VIDEOFORMAT, 0, 0, , ahk_id %capHwnd%
Return
SetupVideoCompression:
SendMessage, WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0, , ahk_id %capHwnd%
Return
DisconnectDriver:
SendMessage, WM_CAP_DRIVER_DISCONNECT, 1, 0, , ahk_id %capHwnd%
Return
RefreshDrivers:
foundDriver = 0
LV_Delete()
Loop
{
thisInfo := Cap_GetDriverDescription(A_Index-1)
If thisInfo
{
foundDriver = 1
LV_Add("", A_Index-1, thisInfo)
}
Else
Break
}
If !foundDriver
{
LV_Delete()
LV_Add("", "", "Could not get video drivers")
GuiControl, Disable, CapDriversLV
GuiControl, Disable, SelectDriverB
}
Return
SelectDriver:
FocusedRowNumber := LV_GetNext(0, "F") ; Find the focused row.
if not FocusedRowNumber ; No row is focused.
return
LV_GetText(SelectedDriver, FocusedRowNumber, 1)
Return
Cap_CreateCaptureWindow(previewHwnd, x, y, w, h)
{
Global lwndC
WS_CHILD := 0x40000000
WS_VISIBLE := 0x10000000
lpszWindowName := "test"
lwndC := DLLCall("avicap32.dll\capCreateCaptureWindowA"
, "Str", lpszWindowName
, "UInt", WS_VISIBLE | WS_CHILD ; dwStyle
, "Int", x
, "Int", y
, "Int", w
, "Int", h
, "UInt", previewHwnd
, "Int", 0)
Return lwndC
}
Cap_GetDriverDescription(wDriver)
{
VarSetCapacity(lpszName, 100)
VarSetCapacity(lpszVer, 100)
res := DLLCall("avicap32.dll\capGetDriverDescriptionA"
, "Short", wDriver
, "Str", lpszName
, "Int", 100
, "Str", lpszVer
, "Int", 100)
If res
capInfo := lpszName ; " | " lpszVer
Return capInfo
}
GuiClose:
GoSub, DisconnectDriver
DllCall("FreeLibrary", "str", hModule)
ExitApp
Return
/*
WM_CAP_SET_SEQUENCE_SETUP
The WM_CAP_SET_SEQUENCE_SETUP message sets the configuration parameters used with streaming capture. You can send this message explicitly or by using the capCaptureSetSetup macro.
WM_CAP_SET_SEQUENCE_SETUP
wParam = (WPARAM) (wSize);
lParam = (LPARAM) (LPVOID) (LPCAPTUREPARMS) (psCapParms);
Parameters
wSize
Size, in bytes, of the structure referenced by s.
psCapParms
Pointer to a CAPTUREPARMS structure.
Return Values
Returns TRUE if successful or FALSE otherwise.
CAPTUREPARMS structure:
typedef struct {
DWORD dwRequestMicroSecPerFrame;
BOOL fMakeUserHitOKToCapture;
UINT wPercentDropForError;
BOOL fYield;
DWORD dwIndexSize;
UINT wChunkGranularity;
BOOL fUsingDOSMemory;
UINT wNumVideoRequested;
BOOL fCaptureAudio;
UINT wNumAudioRequested;
UINT vKeyAbort;
BOOL fAbortLeftMouse;
BOOL fAbortRightMouse;
BOOL fLimitEnabled;
UINT wTimeLimit;
BOOL fMCIControl;
BOOL fStepMCIDevice;
DWORD dwMCIStartTime;
DWORD dwMCIStopTime;
BOOL fStepCaptureAt2x;
UINT wStepCaptureAverageFrames;
DWORD dwAudioBufferSize;
BOOL fDisableWriteCache;
UINT AVStreamMaster;
} CAPTUREPARMS;
*/
;#Include ws4ahk.ahk
|
So if anybody knows in what struct compression-params are stored, please drop a post here. Thx.
Possible future plans:
Make it DirectX ather than VfW
Take control over codec setup
Include a more professional motion detection (probably not in AHK code)
Cheers.
fures |
|
| Back to top |
|
 |
Clash
Joined: 27 Jun 2006 Posts: 182
|
Posted: Sun Jun 22, 2008 9:51 pm Post subject: |
|
|
Hi, i'm nosing at your code now. Unless i'm wrong, pictures are periodically taken and saved to a file, the CompareImages label compares the latest and last image for differences.
But i don't understand why the preview needs to be on for motion detection to work.
Edit: i think i see what you're doing now. you're checking the entire screen to see if the saved image is there. isn't it possible to compare 2 saved images? _________________
 |
|
| Back to top |
|
 |
TheLost Guest
|
Posted: Wed Jul 09, 2008 7:16 am Post subject: You get false positives when the window is hid |
|
|
between:
Gui, 2:Show, x650 w%WDT% h%HGT%, Viewer
and
GoSub ConnectToDriver
add
Gui, 2:+AlwaysOnTop
so that the viewer doesn't disappear behind stuff
can still be drug off the edge of the screen, though
Really cool program, have been looking for something like this for awhile
Hope to get to mess with it more tomorrow |
|
| Back to top |
|
 |
StreetRider_on_The_Road Guest
|
Posted: Sat Jul 12, 2008 1:03 am Post subject: Question |
|
|
Maybe this would be to simple.
What about having it self click on the capture to avi for say 5 or 10 seconds and then stop. That would be good for creating the capture.
If you can make the message pop up that there is motion you should be able to make it record during that time.
I have not checked into the code, but it seems pretty straight forward.
Just saying... |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|