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 »

djes, what version of the 2.4 library did you compiled? 2.4.13.3?
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 »

AAT wrote:djes, what version of the 2.4 library did you compiled? 2.4.13.3?
Yes, the latest 2.4
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, OpenCV 2.4.13.3, CMake 2.8.12.1, MS Visual Studio 2010 Express

I compiled the OpenCV 2.4.13.3 library just now and tested it with last JHPJHP pack.
The example cv_webcam_saveimage.pb works fine.

I will edit cap_dshow.cpp tomorrow and load all dll-s if this is necessary for someone.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

djes wrote: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:
That's why I've given up on OpenCV.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

AAT wrote:I will edit cap_dshow.cpp tomorrow and load all dll-s if this is necessary for someone.
Does this do moving video or just still images? Would you be willing to compile it with the reordered mediaSubtypes?

http://www.purebasic.fr/english/viewtop ... 15#p511157

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;
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: Another Webcam Demo Program

Post by AAT »

chris319, you can download Opencv2.4.13.3 VC10 Dll-s with reordered mediaSubtypes (MEDIASUBTYPE_MJPG - first): http://rgho.st/84FfzcJrp (30 days!)

I don't think that there are any troubles with "...name mangling and other problems...": opencv 2.4 PB prototypes basically the same from 2013-2014, from JHPJHP project beginning.
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 »

AAT wrote:chris319, you can download Opencv2.4.13.3 VC10 Dll-s with reordered mediaSubtypes (MEDIASUBTYPE_MJPG - first): http://rgho.st/84FfzcJrp (30 days!)

I don't think that there are any troubles with "...name mangling and other problems...": opencv 2.4 PB prototypes basically the same from 2013-2014, from JHPJHP project beginning.
I didn't said that the prototypes were bad, I said that the DLL and LIB that I get from compilation had _ (underscore) name mangling problem and that *I* didn't know how to resolve that by now (I haven't time to do it). As I just installed VC for this special purpose, having only compiled the native opencv package (and not all the recommended stuff), no doubt it lacks some configuration.
Anyway, it's nice to have done the job, I hope chris319 can do something with your modified opencv. For my own, I'll try again this compilation when I'll get time, hoping that I don't have to use a XP virtual machine for that !
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

Hi AAT -

Thank you very much for compiling OpenCV for me. That was very kind of you to do.

My program, which displays the webcam output and the frame rate, successfully compiles and runs. Unfortunately, on a 1280 x 720 display we are still at about 10 fps. On a 640 x 360 display we get up to 30 fps. I have to conclude that it is the webcam and driver giving us this frame rate.

Later today a Logitech Brio will arrive and we'll see if the frame rate improves.

Thanks again for compiling OpenCV. I genuinely appreciate it.
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: Another Webcam Demo Program

Post by AAT »

djes wrote:... As I just installed VC for this special purpose, having only compiled the native opencv package...
Me too. I've never used Visual Studio before.
What version of Windows, Visual Studio and CMake did you use?

chris319, I'm using small old program amcap.exe (MS DirectShow Sample) to test webcam performafnce: resolution, codecs, fps.
My webcam have max fps with MJPG codec too.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

Hi AAT -

For testing webcam performance I like to use the program posted in this thread. It measures the actual frame rate using escapi's isCaptureDone() function. It seems there is a lot of jitter in the frame rate, that is, it is not a constant 30 fps (in 640 x 360 resolution) but fluctuates around 30 - 31 fps. The frame rate is also dependent on the amount of light on the subject.

To record a webcam I prefer OBS, Open Broadcaster Software which records .mp4 video using H.264 and allows the use of external audio sources. https://obsproject.com/
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 »

AAT wrote:
djes wrote:... As I just installed VC for this special purpose, having only compiled the native opencv package...
Me too. I've never used Visual Studio before.
What version of Windows, Visual Studio and CMake did you use?.
Windows 10, OpenCV 2.4.13.3, CMake 2.8.12.1, MS Visual Studio 2010 Express (I've tried at first with 2017 community). I've tried a lot of things, still has these "_" in the .lib files (and polink error). Maybe I have something installed on my PC changing the default compiling options (I've removed MinGW). Or maybe I don't do what is needed. Do you follow some specific rules to compile ?
AAT
Enthusiast
Enthusiast
Posts: 256
Joined: Sun Jun 15, 2008 3:13 am
Location: Russia

Re: Another Webcam Demo Program

Post by AAT »

I did not do anything special:
- CMake: -> Configure -> Generate
- MS Visual C++ 2010 Express: -> Open Solution (opencv.sln) -> Build Solution
That's all.

In CMake try:
File -> Delete Cache
Configure -> Visual Studio 10 -> Use default native compilers
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

I finally got 30 fps out of my webcam! Here is what it took:

New webcam (Logitech BRIO -- has a USB 3.0 output)

For 30 fps you MUST use a USB 3.0 connection, so I also had to get a USB 3.0 interface card and cable.

This is using escapi, not OpenCV.

That's it.

Thank you again for your help, AAT.

Here is the updated program. The fps counter is more stable.

Code: Select all

;WORKS WITH ESCAPI
;UPDATED 9/7/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 = DesktopWidth(0)
Global HEIGHT = 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

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)

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

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

now.f = ElapsedMilliseconds()
fps.f = now.f - then.f
SetGadgetState(0, ImageID(1))

frameCounter + 1
If frameCounter > 100: avgFPS = 30: frameCounter = 1: EndIf
avgFPS + fps

fps = (avgFPS / frameCounter)

If fps > 0:fps$ = Str((1/fps)*1000)
If (1/fps)*1000 < 10: fps$ = "0" + fps$: EndIf
EndIf

DrawText(100, 200, fps$+" fps",#White)

then.f = ElapsedMilliseconds()

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

Until Quit = #True

StopDrawing()
deinitCapture(#DEVICE)
FreeImage(1)
FreeMemory(scp\mTargetBuf)
CloseWindow(1)

Else
      Debug "Init capture failed."
EndIf

    End
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> Happy to read that ! I'm not that surprised about the USB3 vs USB2, because of bandwidth... Anyway !

AAT> Understood the leading underscore compiling problem. In fact, as I'm on a x64 system, the win32 compiler decorates the x32 compiled functions. I'm trying to compile in native x64 mode to see the difference :)

Edit: compiled flawlessly with visual studio community 2017 and cmake 3.9.1 in 64 bits. Works like a charm with JHPJHP's implementation... Thank you both for all your efforts, it could be useful for me a day.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Setting ProcAmp Params

Post by chris319 »

Thank you too, djes. I didn't mean to leave you out.

I'm still trying to control some webcam parameters in the escapi version. I'm having trouble converting the OO CPP to pb. Here is the rewritten escapi.pbi. See lines 124 - 129. I'm trying to control the webcam parameters using Set(). Any help is appreciated.

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 IAMVideoProcAmpVtbl
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

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

;/* 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.IAMVideoProcAmpVtbl
  *v = AllocateMemory(SizeOf(IAMVideoProcAmpVtbl))

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

;*v\VTable = ?Set

;*v\IAMVideoProcAmpVtbl = ?Set

  ProcedureReturn *v
EndProcedure

*object.IAMVideoProcAmpVtbl = createObject()
;*object\IAMVideoProcAmpVtbl = ?Set
;*v\IAMVideoProcAmpVtbl = ?Set

;*object\Set(0,0,0)

;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
Post Reply