Another Webcam Demo Program

Share your advanced PureBasic knowledge/code with the community.
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: Another Webcam Demo Program

Post by AAT »

There ara much tutorials. Forexample http://docs.opencv.org/2.4/doc/tutorial ... stall.html
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

I've seen the OpenCV documentation; it's too much for me. If you already know how to compile it, I would appreciate your either doing so, or explain how to make a dll out of cap_dshow.cpp using Visual Studio.

This appears in the file cap_dshow.cpp

//The video types we support
//in order of preference

Here is my modified array. Note that it puts MJPG at the top of the list i.e. first preference.

Code: Select all

    //The video types we support
    //in order of preference

    mediaSubtypes[0]    = MEDIASUBTYPE_MJPG;
    mediaSubtypes[1]     = MEDIASUBTYPE_RGB24;
    mediaSubtypes[2]     = MEDIASUBTYPE_RGB32;
    mediaSubtypes[3]     = MEDIASUBTYPE_RGB555;
    mediaSubtypes[4]     = MEDIASUBTYPE_RGB565;
    mediaSubtypes[5]     = MEDIASUBTYPE_YUY2;
    mediaSubtypes[6]     = MEDIASUBTYPE_YVYU;
    mediaSubtypes[7]     = MEDIASUBTYPE_YUYV;
    mediaSubtypes[8]     = MEDIASUBTYPE_IYUV;
    mediaSubtypes[9]     = MEDIASUBTYPE_UYVY;
    mediaSubtypes[10]     = MEDIASUBTYPE_YV12;
    mediaSubtypes[11]    = MEDIASUBTYPE_YVU9;
    mediaSubtypes[12]    = MEDIASUBTYPE_Y411;
    mediaSubtypes[13]    = MEDIASUBTYPE_Y41P;
    mediaSubtypes[14]    = MEDIASUBTYPE_Y211;
    mediaSubtypes[15]    = MEDIASUBTYPE_AYUV;

    //non standard
    mediaSubtypes[16]    = MEDIASUBTYPE_Y800;
    mediaSubtypes[17]    = MEDIASUBTYPE_Y8;
    mediaSubtypes[18]    = MEDIASUBTYPE_GREY;
    mediaSubtypes[19]    = MEDIASUBTYPE_I420;
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Another Webcam Demo Program

Post by djes »

There's a nice video capabilities detection code there: https://chromium.googlesource.com/chrom ... _mf_win.cc

It's able to choose the best format depending on size and frame rate.

Basically, the latest escapi code lacks the possibility to enumerate device capabilities, and let the user choose the better.

I've compiled it and got the unused initCaptureWithOptions() function to receive raw data. But it's not faster with my camera.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

I've compiled it and got the unused initCaptureWithOptions() function to receive raw data. But it's not faster with my camera.
Escapi, OpenCV or the chromium code?

Jari says:
escapi requests a format it can easily convert to the target format, and if it doesn't get one, the camera just fails. It's entirely possible that the camera driver does something horrible just to satisfy the request, and some other (still acceptable) format would work faster. However, detecting such would be non-trivial, and might easily break.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Another Webcam Demo Program

Post by djes »

chris319 wrote:
I've compiled it and got the unused initCaptureWithOptions() function to receive raw data. But it's not faster with my camera.
Escapi, OpenCV or the chromium code?

Jari says:
escapi requests a format it can easily convert to the target format, and if it doesn't get one, the camera just fails. It's entirely possible that the camera driver does something horrible just to satisfy the request, and some other (still acceptable) format would work faster. However, detecting such would be non-trivial, and might easily break.
Escapi.
Jari said all. However, it would be nice to get camera capabilities and so, choose the desired "mediatype", even it's not handled/converted by escapi. However, I doubt we'll be able to decode 1280x768x60fps mjpeg "on the fly" :wink:
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

It would be nice to compile OpenCV, too, but I get hopelessly lost when I try to.
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Another Webcam Demo Program

Post by djes »

Why don't you ask to the opencv expert, jhpjhp?
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

I followed these directions:

http://dogfeatherdesign.com/opencv-3-0- ... ake-and-c/

I installed cmake and gitbash exactly as instructed in the directions and "cloned" opencv to C:\. Cmake threw some error messages and that's as far as I got.
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: Another Webcam Demo Program

Post by AAT »

You try to rebuild OpenCV 3.3, you don't needed in it, try to rebuild older version from openCV 2.4 branch.

Some years ago I built the openCV 2.4.8 libraries for use with CodeBlocks 13.12 MinGW IDE. CMAke v.2.8
I can't build the newer versions openCV for CodeBlocks due to compilation errors (not cmake errors).

I have no MS Visual Studio, i don't use it. I can't help you.

P.S. Open software, if it did't work "from the box", require much time and skills to get workable software configuration and good results.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

I tried 2.4, still CMake errors.

I give up.
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: Another Webcam Demo Program

Post by AAT »

I have no problem with OpenCV 2.4.10, CMake 2.8.12, GNU GCC from CodeBlocks 13.12
http://images.vfl.ru/ii/1504334771/905f ... 453134.png
http://images.vfl.ru/ii/1504334772/1737 ... 453135.png

What error message did you receive?
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

AAT: You said you don't have Visual Studio. What operating system are you using?

I'm using Visual Studio 2015 on Windows 10. You seem to be having better luck with GCC.
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: Another Webcam Demo Program

Post by AAT »

Windows XP on my working computer and on my old notebook.
Visual Studio - express for Win XP - mean too much changes in the OS.
Visual Studio community require more recent OS.
I have a Tablet PC with Windows 10, but it's used for web surfing basically.
chris319 wrote:... You seem to be having better luck with GCC.
Not certainly in that way: many errors appear when compiling the latest versions of OpenCV. There is no problem with CMake.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Controlling Webcam ProcAmp

Post by chris319 »

Feeling ambitious and having no success compiling OpenCV, I see that escapi has some facility for controlling a webcam's proc amp. I'm having trouble getting the OOP interface to the IAMVideoProcAmp interface in DirectShow. Any help is appreciated.

https://www.google.com/search?q=iamvide ... 8&oe=utf-8

All code posted here works, but need help getting the IAMVideoProcAmp interface to work. I don't think I'm setting things up properly at lines 8, 14 and 125 of escapi.pbi, below.

Code: Select all

;WORKS WITH ESCAPI
;UPDATED 8/28/2017
;PRESS CTL-Q TO QUIT
;INCORPORATES PROC AMP

ExamineDesktops()

gamma.f = 1.34 ;WEBCAM OUTPUT IS ALREADY GAMMA CORRECTED
#PEDESTAL = 16
gain.f = (235-16)/235
gain * 0.97

factor.f = 255/Pow(255,gamma)

;SET WEBCAM TO DEFAULTS AND SHARPNESS TO 192

;MAKE LUTS
Dim gammaTable.a(256)
For cnt = 0 To 255
gammaTable.a(cnt)=(Pow(cnt,gamma) * factor * gain) + #PEDESTAL
If gammaTable(cnt) > 254: gammaTable(cnt) = 254
EndIf
Next

Dim x3table.l(1921)
For cnt = 0 To 1920
x3table(cnt) = cnt * 3
Next

LoadFont(1,"Arial",24)

IncludeFile "escapi.pbi"
Global WIDTH = 640;DesktopWidth(0)
Global HEIGHT = 360;DesktopHeight(0)
Global WIDTHM1 = WIDTH - 1
Global HEIGHTM1 = HEIGHT - 1
Global pixCount = (WIDTH * HEIGHT) - 2

Global Dim pixcolor.l(WIDTH, HEIGHT): Global Dim unsmoothedY.d(WIDTH, HEIGHT)
Global Dim Cr.d(WIDTH, HEIGHT): Global Dim Y.d(WIDTH, HEIGHT): Global Dim Cb.d(WIDTH, HEIGHT)

Global imHeight, imWidth, xCoord, yCoord,Rd,Gd,Bd

#DEVICE = 0    
If setupESCAPI() = #Null
      MessageRequester("Error", "Unable to initialize ESCAPI.")
    End
EndIf

    bufSize = WIDTH * HEIGHT * 4
    scp.SimpleCapParams
    scp\mWidth = WIDTH
    scp\mHeight = HEIGHT
    scp\mTargetBuf = AllocateMemory(bufSize)
    *buf = scp\mTargetBuf

    If initCapture(#DEVICE, @scp)
     
image = CreateImage(1, WIDTH, HEIGHT, 24)
OpenWindow(1, 0, 0, WIDTH, HEIGHT,"",#PB_Window_BorderLess)
AddKeyboardShortcut(1, #PB_Shortcut_Control|#PB_Shortcut_Q, 113);CTL Q TO QUIT
ImageGadget(0, 0, 0, WIDTH, HEIGHT, ImageID(1))
Quit = #False

;procamp\Set(#CAPTURE_BRIGHTNESS, 128, #VideoProcAmp_Flags_Manual)

StartDrawing(ImageOutput(1))
*writeBuffer = DrawingBuffer()
pitch = DrawingBufferPitch()
StopDrawing()

StartDrawing(WindowOutput(1))
;StartDrawing(ImageOutput(1))
DrawingFont(FontID(1))

Repeat
If WindowEvent() = #PB_Event_Menu And EventMenu() = 113
  Quit = #True
EndIf
     
doCapture(#DEVICE)

Repeat: Until isCaptureDone(#DEVICE) <> #False

;PIXEL-BY-PIXEL READING AND WRITING
hm1 = *writebuffer + (HEIGHTM1 * pitch)
*bufoff = *buf

;Goto skip
For y = 0 To HEIGHTM1
For x = 0 To WIDTHM1
x3 = hm1 + x3table(x)

p1.l = PeekL(*bufoff)

PokeA(x3,gammaTable(p1 & 255))
PokeA(x3+1,gammaTable(p1 >> 8 & 255))
PokeA(x3+2,gammaTable(p1 >> 16))

*bufoff + 4
Next
hm1 - pitch
Next

skip:
SetGadgetState(0, ImageID(1))

now.f = ElapsedMilliseconds()
fps.f = now.f-then.f

If fps > 0:fps$ = StrF((1/fps)*1000,2) ;:EndIf
If (1/fps)*1000 < 10: fps$ = "0" + fps$: EndIf
EndIf

DrawText(100, 200, fps$+" fps",#White)
then.f = ElapsedMilliseconds()
;StopDrawing()

Until Quit = #True
StopDrawing()
deinitCapture(#DEVICE)
FreeImage(1)
FreeMemory(scp\mTargetBuf)
CloseWindow(1)

Else
      Debug "Init capture failed."
EndIf

    End
Here is the modified version of escapi.pbi.

Code: Select all

;/* Extremely Simple Capture API */
Structure SimpleCapParams
  *mTargetBuf ; Must be at least mWidth * mHeight * SizeOf(int) of size!
  mWidth.l
  mHeight.l
EndStructure

Structure procAmpSet
property.l
lvalue.l
Flags.l
EndStructure

Structure udtSelf
  *VTable
  text1.s
EndStructure

#VideoProcAmp_Flags_Auto	= 1
#VideoProcAmp_Flags_Manual	= 2

;C style Interface

;     typedef struct IAMVideoProcAmpVtbl

;Structure IAMVideoProcAmpVtbl

Interface procAmp
;         HRESULT ( STDMETHODCALLTYPE *QueryInterface )( 
;             IAMVideoProcAmp * This,
;             /* [in] */ REFIID riid,
;             /* [iid_is][out] */ 
;             __RPC__deref_out  void **ppvObject);
;         
;ULONG (STDMETHODCALLTYPE *AddRef)(IAMVideoProcAmp * This)
         
;         ULONG ( STDMETHODCALLTYPE *Release )( 
;             IAMVideoProcAmp * This);
;         
;         HRESULT ( STDMETHODCALLTYPE *GetRange )( 
;             IAMVideoProcAmp * This,
;             /* [in] */ long Property,
;             /* [out] */ 
;             __out  long *pMin,
;             /* [out] */ 
;             __out  long *pMax,
;             /* [out] */ 
;             __out  long *pSteppingDelta,
;             /* [out] */ 
;             __out  long *pDefault,
;             /* [out] */ 
;             __out  long *pCapsFlags);
;         

;         HRESULT ( STDMETHODCALLTYPE *Set )( 
;             IAMVideoProcAmp * This,
Set(property.l,lvalue.l,Flags.l)
             ;property.l ;/* [in] */ 
;             lValue.l /* [in] */ long 
;             /* [in] */ long Flags);
;         
;         HRESULT ( STDMETHODCALLTYPE *Get )( 
;             IAMVideoProcAmp * This,
;             /* [in] */ long Property,
;             /* [out] */ 
;             __out  long *lValue,
;             /* [out] */ 
;             __out  long *Flags);
EndInterface

;*object.procAmp = #Null ;createObject
;*object\Set(0,0,0)

;/* Return the number of capture devices found */
PrototypeC countCaptureDevicesProc()

; /* initCapture tries To open the video capture device.
;  * Returns 0 on failure, 1 on success.
;  * Note: Capture parameter values must Not change While capture device
;  *       is in use (i.e. between initCapture And deinitCapture).
;  *       Do *Not* free the target buffer, or change its pointer!
;  */
PrototypeC initCaptureProc(deviceno, *aParams.SimpleCapParams)

;/* deinitCapture closes the video capture device. */
PrototypeC deinitCaptureProc(deviceno)

;/* doCapture requests video frame To be captured. */
PrototypeC doCaptureProc(deviceno)

;/* isCaptureDone returns 1 when the requested frame has been captured.*/
PrototypeC isCaptureDoneProc(deviceno)

;/* Get the user-friendly name of a capture device. */
PrototypeC getCaptureDeviceNameProc(deviceno, *namebuffer, bufferlength)

;/* Returns the ESCAPI DLL version. 0x200 For 2.0 */
PrototypeC ESCAPIDLLVersionProc()

; marked as "internal" in the example
PrototypeC initCOMProc()

;chris319
Enumeration CAPTURE_PROPETIES
	#CAPTURE_BRIGHTNESS = 0
	#CAPTURE_CONTRAST
	#CAPTURE_HUE
	#CAPTURE_SATURATION
	#CAPTURE_SHARPNESS
	#CAPTURE_GAMMA
	#CAPTURE_COLORENABLE
	#CAPTURE_WHITEBALANCE
	#CAPTURE_BACKLIGHTCOMPENSATION
	#CAPTURE_GAIN
	#CAPTURE_PAN
	#CAPTURE_TILT
	#CAPTURE_ROLL
	#CAPTURE_ZOOM
	#CAPTURE_EXPOSURE
	#CAPTURE_IRIS
	#CAPTURE_FOCUS
	#CAPTURE_PROP_MAX
EndEnumeration

Procedure CreateObject()
  Protected *v.procAmpSet
  *v = AllocateMemory(SizeOf(procAmpSet))
;  *v\VTable = ?Procedures
  ProcedureReturn *v
EndProcedure

procAmp.IAMVideoProcAmp = CreateObject()

;see file strmif.h lines 9954,10110

Global countCaptureDevices.countCaptureDevicesProc
Global initCapture.initCaptureProc
Global deinitCapture.deinitCaptureProc
Global doCapture.doCaptureProc
Global isCaptureDone.isCaptureDoneProc
Global getCaptureDeviceName.getCaptureDeviceNameProc
Global ESCAPIDLLVersion.ESCAPIDLLVersionProc

Procedure setupESCAPI()
  
  ; load library
  capdll = OpenLibrary(#PB_Any, "escapi.dll")
  If capdll = 0
    ProcedureReturn 0
  EndIf
  
  ;/* Fetch function entry points */
  countCaptureDevices = GetFunction(capdll, "countCaptureDevices")
  initCapture = GetFunction(capdll, "initCapture")
  deinitCapture = GetFunction(capdll, "deinitCapture")
  doCapture = GetFunction(capdll, "doCapture")
  isCaptureDone = GetFunction(capdll, "isCaptureDone")
  initCOM.initCOMProc = GetFunction(capdll, "initCOM")
  getCaptureDeviceName = GetFunction(capdll, "getCaptureDeviceName")
  ESCAPIDLLVersion = GetFunction(capdll, "ESCAPIDLLVersion")
  
  If countCaptureDevices = 0 Or initCapture = 0 Or deinitCapture = 0 Or doCapture = 0 Or isCaptureDone = 0 Or initCOM = 0 Or getCaptureDeviceName = 0 Or ESCAPIDLLVersion = 0
    ProcedureReturn 0
  EndIf
  
  ;/* Verify DLL version */
  If ESCAPIDLLVersion() < $200
    ProcedureReturn 0
  EndIf
  
  ;/* Initialize COM.. */
  initCOM();
  
  ; returns number of devices found
  ProcedureReturn countCaptureDevices()
EndProcedure
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Another Webcam Demo Program

Post by djes »

I had success compiling both, but opencv 3.3 is not compatible with JHPJHP's great pb implementation, and compiled 2.4 is not recognised by his pbi . It seems that I have the _ name mangling and other problems that I don't know now how to resolve. :cry:
Post Reply