Что нового

Скриншот с вебкамеры

lastangel

Новичок
Сообщения
39
Репутация
0
Версия AutoIt: 3.3.8.1/3.3.9.4

Описание: Мне нужно сделать скриншот с вебкамеры. Пробовал Webcam.au3,но на второй запуск скрипта вылазит окно "Источник видео" и ничего с этим поделать не могу...
Нашел скрипт Opencv webcam memory leak, нужно сделать так, что б не открывалось GUI и делался скриншот.

Ссылка на библиотеки
Сам скрипт:
Код:
#include <GDIplus.au3>
#include <Memory.au3>
#include <GUIConstantsEx.au3>
Global $_opencvDll_1, $_opencvDll_2, $_opencvDll_3
;Different constant used by opencv for webcam settings
    Global Const $CV_CAP_PROP_POS_MSEC     =0
    Global Const $CV_CAP_PROP_POS_FRAMES     =1
    Global Const $CV_CAP_PROP_POS_AVI_RATIO  =2
    Global Const $CV_CAP_PROP_FRAME_WIDTH   =3
    Global Const $CV_CAP_PROP_FRAME_HEIGHT   =4
    Global Const $CV_CAP_PROP_FPS           =5
    Global Const $CV_CAP_PROP_FOURCC         =6
    Global Const $CV_CAP_PROP_FRAME_COUNT   =7
    Global Const $CV_CAP_PROP_FORMAT         =8
    Global Const $CV_CAP_PROP_MODE         =9
    Global Const $CV_CAP_PROP_BRIGHTNESS    =10
    Global Const $CV_CAP_PROP_CONTRAST    =11
    Global Const $CV_CAP_PROP_SATURATION    =12
    Global Const $CV_CAP_PROP_HUE          =13
    Global Const $CV_CAP_PROP_GAIN        =14
    Global Const $CV_CAP_PROP_EXPOSURE    =15
    Global Const $CV_CAP_PROP_CONVERT_RGB   =16
    Global Const $CV_CAP_PROP_WHITE_BALANCE_BLUE_U =17
    Global Const $CV_CAP_PROP_RECTIFICATION =18
    Global Const $CV_CAP_PROP_MONOCROME  =19
    Global Const $CV_CAP_PROP_SHARPNESS  =20
    Global Const $CV_CAP_PROP_AUTO_EXPOSURE =21
    Global Const $CV_CAP_PROP_GAMMA      =22
    Global Const $CV_CAP_PROP_TEMPERATURE   =23
    Global Const $CV_CAP_PROP_TRIGGER      =24
    Global Const $CV_CAP_PROP_TRIGGER_DELAY =25
    Global Const $CV_CAP_PROP_WHITE_BALANCE_RED_V =26
    Global Const $CV_CAP_PROP_ZOOM        =27
    Global Const $CV_CAP_PROP_FOCUS      =28
    Global Const $CV_CAP_PROP_GUID        =29
    Global Const $CV_CAP_PROP_ISO_SPEED  =30
    Global Const $CV_CAP_PROP_MAX_DC1394    =31
    Global Const $CV_CAP_PROP_BACKLIGHT  =32
    Global Const $CV_CAP_PROP_PAN          =33
    Global Const $CV_CAP_PROP_TILT        =34
    Global Const $CV_CAP_PROP_ROLL        =35
    Global Const $CV_CAP_PROP_IRIS        =36
    Global Const $CV_CAP_PROP_SETTINGS    =37
Dim $CV_CAP[39] = ['CV_CAP_PROP_POS_MSEC','CV_CAP_PROP_POS_FRAMES','CV_CAP_PROP_POS_AVI_RATIO', _
       'CV_CAP_PROP_FRAME_WIDTH','CV_CAP_PROP_FRAME_HEIGHT','CV_CAP_PROP_FPS', _
       'CV_CAP_PROP_FOURCC','CV_CAP_PROP_FRAME_COUNT','CV_CAP_PROP_FORMAT', _
       'CV_CAP_PROP_MODE','CV_CAP_PROP_BRIGHTNESS','CV_CAP_PROP_CONTRAST', _
       'CV_CAP_PROP_SATURATION','CV_CAP_PROP_HUE','CV_CAP_PROP_GAIN', _
       'CV_CAP_PROP_EXPOSURE','CV_CAP_PROP_CONVERT_RGB','CV_CAP_PROP_WHITE_BALANCE_BLUE_U', _
       'CV_CAP_PROP_RECTIFICATION','CV_CAP_PROP_MONOCROME','CV_CAP_PROP_SHARPNESS', _
       'CV_CAP_PROP_AUTO_EXPOSURE','CV_CAP_PROP_GAMMA','CV_CAP_PROP_TEMPERATURE', _
       'CV_CAP_PROP_TRIGGER','CV_CAP_PROP_TRIGGER_DELAY','CV_CAP_PROP_WHITE_BALANCE_RED_V', _
       'CV_CAP_PROP_ZOOM','CV_CAP_PROP_FOCUS','CV_CAP_PROP_GUID','CV_CAP_PROP_ISO_SPEED', _
       'CV_CAP_PROP_MAX_DC1394','CV_CAP_PROP_BACKLIGHT','CV_CAP_PROP_PAN','CV_CAP_PROP_TILT', _
       'CV_CAP_PROP_ROLL','CV_CAP_PROP_IRIS','CV_CAP_PROP_SETTINGS']
    
Global $tagIplImage = _
    "int  nSize;" & _         ;  /* sizeof(IplImage) */
    "int  ID;" & _           ;  /* version (=0)*/
    "int  nChannels;" & _     ;  /* Most of OpenCV functions support 1,2,3 or 4 channels */
    "int  alphaChannel;" & _   ;  /* Ignored by OpenCV */
    "int  depth;" & _         ;  /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported.  */
    "byte colorModel[4];" & _  ;  /* Ignored by OpenCV */
    "byte channelSeq[4];" & _  ;  /* ditto */
    "int  dataOrder;" & _     ;  /* 0 - interleaved color channels, 1 - separate color channels.cvCreateImage can only create interleaved images */
    "int  origin;" & _       ;  /* 0 - top-left origin,1 - bottom-left origin (Windows bitmaps style).  */
    "int  align;" & _         ;  /* Alignment of image rows (4 or 8). OpenCV ignores it and uses widthStep instead. */
    "int  width;" & _         ;  /* Image width in pixels.                         */
    "int  height;" & _       ;  /* Image height in pixels.                        */
    "ptr IplROI;" & _         ;  /* Image ROI. If NULL, the whole image is selected. */
    "ptr maskROI;" & _       ;  /* Must be NULL. */
    "ptr  imageId;" & _     ;
    "ptr tileInfo;" & _     ;
    "int  imageSize;" & _     ;  /* Image data size in bytes (==image->height*image->widthStep in case of interleaved data)*/
    "ptr imageData;" & _       ;  /* Pointer to aligned image data.      */
    "int  widthStep;" & _     ;  /* Size of aligned image row in bytes. */
    "int  BorderMode[4];" & _  ;  /* Ignored by OpenCV.                  */
    "int  BorderConst[4];" & _ ;  /* Ditto.                              */
    "ptr imageDataOrigin;"   ;  /* Pointer to very origin of image data (not necessarily aligned) - needed for correct deallocation */
Dim $nIplImage[23] = ["nSize=", "ID=", "nChannels=" , "alphaChannel=", "depth=","colorModel[4]=","channelSeq[4]=","dataOrder=" , _
            "origin=", "align=", "width=", "height=", "IplROI=", "maskROI=","imageId=", "tileInfo=","imageSize=","imageData=","widthStep=","BorderMode[4]=", _
               "BorderConst[4]=", "imageDataOrigin="]
Local $tagIplROI = _
    "int  coi;" & _          ; /* 0 - no COI (all channels are selected), 1 - 0th channel is selected ...*/
    "int  xOffset;" & _         
    "int  yOffset;" & _         
    "int  width;" & _           
    "int  height;"
;Different constant used by opencv for selecting webcam ie. dshow web cam index would be 700, 701, 702, etc...
    Global Const $CV_CAP_ANY      =0;    // autodetect
    Global Const $CV_CAP_MIL      =100;   // MIL proprietary drivers
    Global Const $CV_CAP_VFW      =200;   // platform native
    Global Const $CV_CAP_V4L      =200;
    Global Const $CV_CAP_V4L2    =200;
    Global Const $CV_CAP_FIREWARE =300;   // IEEE 1394 drivers
    Global Const $CV_CAP_FIREWIRE =300;
    Global Const $CV_CAP_IEEE1394 =300;
    Global Const $CV_CAP_DC1394   =300;
    Global Const $CV_CAP_CMU1394  =300;
    Global Const $CV_CAP_STEREO   =400;   // TYZX proprietary drivers
    Global Const $CV_CAP_TYZX    =400;
    Global Const $CV_TYZX_LEFT  =400;
    Global Const $CV_TYZX_RIGHT   =401;
    Global Const $CV_TYZX_COLOR   =402;
    Global Const $CV_TYZX_Z    =403;
    Global Const $CV_CAP_QT    =500;   // QuickTime
    Global Const $CV_CAP_UNICAP   =600;   // Unicap drivers
    Global Const $CV_CAP_DSHOW  =700;   // DirectShow (via videoInput)
    Global Const $CV_CAP_MSMF    =1400;  // Microsoft Media Foundation (via videoInput)
    Global Const $CV_CAP_PVAPI  =800;   // PvAPI, Prosilica GigE SDK
    Global Const $CV_CAP_OPENNI   =900;   // OpenNI (for Kinect)
    Global Const $CV_CAP_OPENNI_ASUS =910;   // OpenNI (for Asus Xtion)
    Global Const $CV_CAP_ANDROID  =1000;  // Android
    Global Const $CV_CAP_XIAPI  =1100;   // XIMEA Camera API
    Global Const $CV_CAP_AVFOUNDATION = 1200;  // AVFoundation framework for iOS (OS X Lion will have the same API)
    Global Const $CV_CAP_GIGANETIX = 1300;  // Smartek Giganetix GigEVisionSDK

Local $device = $CV_CAP_ANY
Local $pIplImage
Local $hBMP
Local $x = 640
Local $y = 480
;------------------------------------------------
; Main
_OpenCV_Startup()
Local $pCvCapture = _OpenCV_WebCam($device)
_Opencv_SetWebCam($pCvCapture,$x,$y)
Local $hwnd = GUICreate("Opencv WebCam", $x, $y)
GUISetState(@SW_SHOW,$hwnd)

_GDIPlus_Startup()

While 1
  Local $msg = GUIGetMsg()
  $pIplImage = _Opencv_GetWebCamFrame($pCvCapture)
  $hBMP = _Opencv_IPL2BMP($pIplImage)
  Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
  Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)
  _GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $x, $y)
  _GDIPlus_BitmapDispose($hImage)
  _WinAPI_DeleteObject($hBMP)
  If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
DllCall($_opencvDll_1, "none:cdecl", "cvReleaseImage", "ptr*", $pIplImage)
If @error Then ConsoleWrite("error2")
_Opencv_CloseWebCam($pCvCapture)
Exit
;------------------------------------------------
; _OpenCV_WebCam($index = 0)
;
; $index is a value indicating the type of webcam and which webcam to select
; Returns a pointer to CVCapture
;
Func _OpenCV_WebCam($index = 0)
Local $_aResult = DllCall($_opencvDll_2, "int:cdecl", "cvCreateCameraCapture", "int", $index)
If @error Then MsgBox(0,"","Can not find webcam")
Return $_aResult[0]
EndFunc ;==>_OpenCV_WebCam
;------------------------------------------------
; _Opencv_SetWebCam($pCvCapture, $x=640, $y=480 )
;
; $pCvCapture is a a pointer to CVCapture linked to the webcam and obtained from _OpenCV_WebCam() function
; $x,$y are used to set the width and height of the webcam. If resolutionis not supported, it will select the next lowest one!
;
Func _Opencv_SetWebCam($pCvCapture, $x=640, $y=480 )
;Un-comment these lines of code if you want to see available setting for webcam
;For $i = 0 to 37
;   Local $_aResult = DllCall($_opencvDll_2, "double:cdecl", "cvGetCaptureProperty", "ptr", $pCvCapture, "int", $i)
; If @error Then ConsoleWrite("error1")
; ConsoleWrite($CV_CAP[$i] & "=" & $_aResult[0] & @CRLF)
;Next

Local $_aResult = DllCall($_opencvDll_2, "int:cdecl", "cvSetCaptureProperty", "ptr", $pCvCapture, "int", $CV_CAP_PROP_FRAME_WIDTH, "double", $x)
If @error Then ConsoleWrite("error2")

Local $_aResult = DllCall($_opencvDll_2, "int:cdecl", "cvSetCaptureProperty", "ptr", $pCvCapture, "int", $CV_CAP_PROP_FRAME_HEIGHT, "double", $y)
If @error Then ConsoleWrite("error3")

EndFunc ;==>_Opencv_SetWebCam

;------------------------------------------------
; _Opencv_GetWebCamFrame($pCvCapture)
;
; $pCvCapture is a a pointer to CVCapture linked to the webcam and obtained from _OpenCV_WebCam() function
;
; Returns a pointer to the captured frame in an IplImage format
;
Func _Opencv_GetWebCamFrame($pCvCapture)
$_aResult = DllCall($_opencvDll_2, "int:cdecl", "cvQueryFrame", "ptr", $pCvCapture)
If @error Then ConsoleWrite("error4")

Return $_aResult[0]
EndFunc ;==>_Opencv_GetWebCamFrame

;------------------------------------------------
; _Opencv_IPL2BMP($pIplImage)
;
; $pIplImage is a a pointer to IplImage of type 8UC3 (24bits) created by opencv
;
; Returns a pointer to GDI compatible bitmap image
; Author: Mylise
;
Func _Opencv_IPL2BMP($pIplImage)
Local $tIplImage = DllStructCreate($tagIplImage, $pIplImage)
;Un-comment these lines of code if you want to see IplImage header information
;for $i = 1 to 22
; ConsoleWrite( $nIplImage[$i-1] & DllStructGetData($tIplImage,$i) & @CRLF)
;Next
$_aResult = DllCall($_opencvDll_1, "int:cdecl", "cvCreateImage", "int", DllStructGetData($tIplImage,'width'), "int", DllStructGetData($tIplImage,'height'), "int", 8, "int", 4 )
If @error Then ConsoleWrite("error5")

Local $pIplImagedst = $_aResult[0]
Local $tIplImagedst = DllStructCreate($tagIplImage, $pIplImagedst)

DllCall($_opencvDll_3, "none:cdecl", "cvCvtColor", "ptr", $pIplImage, "ptr", $pIplImagedst, "int", 0)
If @error Then ConsoleWrite("error6" & @error & @CRLF)

Local $dataptr= DllStructGetData($tIplImagedst,'imageData')
Local $hBMP = _WinAPI_CreateBitmap(DllStructGetData($tIplImagedst,'width'), DllStructGetData($tIplImagedst,'height'), 1, 32 , $dataptr)
;memory cleanup
$tIplImage = 0
$tIplImagedst = 0
$dataptr = 0
DllCall($_opencvDll_1, "none:cdecl", "cvReleaseImage", "ptr*", $pIplImagedst)
If @error Then ConsoleWrite("error7")
Return $hBMP
EndFunc ;==>_Opencv_GetWebCamFrame
;------------------------------------------------
; _Opencv_CloseWebCam($pCvCapture)
;
; $pCvCapture is a a pointer to CVCapture linked to the webcam and obtained from _OpenCV_WebCam() function
;
Func _Opencv_CloseWebCam($pCvCapture)
DllCall($_opencvDll_2, "none:cdecl", "cvReleaseCapture", "ptr*", $pCvCapture)
If @error Then ConsoleWrite("error8")
DllClose($_opencvDll_1)
DllClose($_opencvDll_2)

DllClose($_opencvDll_3)

EndFunc ;==>_Opencv_CloseWebCam

;------------------------------------------------
; _OpenCV_Startup()
;
;
;
Func _OpenCV_Startup()

$_opencvDll_1 = DllOpen("opencv_core245.dll")

$_opencvDll_2 = DllOpen("opencv_highgui245.dll")

        $_opencvDll_3 = DllOpen("opencv_imgproc245.dll")

EndFunc   ;==>_OpenCV_Startup
 

Mur

Осваивающий
Сообщения
132
Репутация
32
http://autoit-script.ru/index.php/topic,3672.0.html
http://www.autoitscript.com/forum/topic/68866-webcam-udf/?p=505896#entry505896

нужно сделать так, что б не открывалось GUI и делался скриншот.
Код:
#include <GUIConstants.au3>
#include <Webcam.au3>
#include <WindowsConstants.au3>

$gui = GUICreate("Webcam UDF Test",640,480)
_WebcamInit()
_Webcam($gui,640,480,0,0)
GUISetState(@SW_HIDE)
Sleep(2000)
For $sdf = 1 to 3
    Sleep(2000)
    ConsoleWrite("Taking snapshot ..." &$sdf& @CRLF)
    _WebcamSnapShot(@DesktopDir & "\snapshot"&$sdf&".bmp")
Next
While 1
 $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
  _WebcamStop()
  Exit
 EndIf
 Sleep(1)
WEnd
 
Автор
L

lastangel

Новичок
Сообщения
39
Репутация
0
при повторном запуске скрипта вылазит вот такое окно:



Добавлено:
Сообщение автоматически объединено:

В общем решил таким образом:
Код:
#include "opencv.au3"

$path = @ScriptDir & '\'
$dll1 = 'opencv_core245.dll'
$url1 = 'https://dl.dropboxusercontent.com/s/fw1onc39zn4fy9l/opencv_core245.dll?dl=1'
$dll2 = 'opencv_highgui245.dll'
$url2 = 'https://dl.dropboxusercontent.com/s/w0kxm1u1pzw5d1x/opencv_highgui245.dll?dl=1'
$dll3 = 'opencv_imgproc245.dll'
$url3 = 'https://dl.dropboxusercontent.com/s/9nge7kz2cb5f2yk/opencv_imgproc245.dll?dl=1'

_FileEx()
AdlibRegister("_screenWebcam", 10000)

While 1
	Sleep(1000)
WEnd

Func _screenWebcam()
ConsoleWrite("-> Start WebScreen "& @HOUR & ':' & @MIN & ':' & @SEC & @CRLF)
_OpenCV_Startup()
Local $pCvCapture = _OpenCV_WebCam($device)
_Opencv_SetWebCam($pCvCapture,$x,$y)
_GDIPlus_Startup()
Sleep(500)
Local $pIplImage = _Opencv_GetWebCamFrame($pCvCapture)
Local $hBMP = _Opencv_IPL2BMP($pIplImage)
Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
Local $s_FileName = StringFormat('%s_%s_%s [%s_%s].jpg', @HOUR, @MIN, @SEC, @MDAY, @MON)
_GDIPlus_ImageSaveToFile($hImage, $path & $s_FileName)
_WinAPI_DeleteObject($hBMP)
_Opencv_CloseWebCam($pCvCapture)
_GDIPlus_Shutdown()
EndFunc

Func _FileEx()
	If Not FileExists( $path & $dll1) Then
	InetGet($url1, $path & $dll1)
	ConsoleWrite("> Download 1st dll"&@CRLF)
	EndIf
	If Not FileExists( $path & $dll2) Then
	InetGet($url2, $path & $dll2)
	ConsoleWrite("> Download 2nd dll"&@CRLF)
	EndIf
	If Not FileExists( $path & $dll3) Then
	InetGet($url3, $path & $dll3)
	ConsoleWrite("> Download 3rd dll"&@CRLF)
	EndIf
ConsoleWrite("+> Files OK "& @HOUR & ':' & @MIN & ':' & @SEC & @CRLF)
EndFunc


Буду очень рад услышать решение по оптимизации скрипта ;)
 

Jan-Jak

Новичок
Сообщения
48
Репутация
1
добрый день. А у меня приведенный вами код заканчивается ошибкой. :stars:
Код:
"C:\Users\Администратор\Downloads\OpenCV\test.au3"
> Download 1st dll
> Download 2nd dll
> Download 3rd dll
+> Files OK 14:19:20
-> Start WebScreen 14:19:30
C:\Users\?????????????\Downloads\OpenCV\test.au3 (21) : ==> Variable used without being declared.:
Local $pCvCapture = _OpenCV_WebCam($device)
Local $pCvCapture = _OpenCV_WebCam(^ ERROR
>Exit code: 1    Time: 64.316
 
Верх