Another Webcam Demo Program

Share your advanced PureBasic knowledge/code with the community.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

Code: Select all

IAMVideoProcAmp::Set
https://msdn.microsoft.com/en-us/librar ... s.85).aspx
Constants

VideoProcAmp_Brightness
Specifies the brightness, also called the black level. For NTSC, the value is expressed in IRE units * 100. For non-NTSC sources, the units are arbitrary, with zero representing blanking and 10,000 representing pure white. Values range from –10,000 to 10,000.
VideoProcAmp_Contrast
Specifies the contrast, expressed as gain factor * 100. Values range from zero to 10,000.
VideoProcAmp_Hue
Specifies the hue, in degrees * 100. Values range from -180,000 to 180,000 (-180 to +180 degrees).
VideoProcAmp_Saturation
Specifies the saturation. Values range from 0 to 10,000.
VideoProcAmp_Sharpness
Specifies the sharpness. Values range from 0 to 100.
VideoProcAmp_Gamma
Specifies the gamma, as gamma * 100. Values range from 1 to 500.
VideoProcAmp_ColorEnable
Specifies the color enable setting. The possible values are 0 (off) and 1 (on).
VideoProcAmp_WhiteBalance
Specifies the white balance, as a color temperature in degrees Kelvin. The range of values depends on the device.
VideoProcAmp_BacklightCompensation
Specifies the backlight compensation setting. Possible values are 0 (off) and 1 (on).
VideoProcAmp_Gain
Specifies the gain adjustment. Zero is normal. Positive values are brighter and negative values are darker. The range of values depends on the device.
__________________________________________________
URL tags added
18.09.2017
RSBasic
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 »

Could you test this new escapi.pbi with camera properties set/get functions ?

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

;/* 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)

Enumeration CAPTURE_PROPERTIES ;Name corrected
   #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
 
;/* Gets value (0..1) of a camera property (see CAPTURE_PROPERTIES, above) */
PrototypeC.f getCapturePropertyValueProc(deviceno, prop)
;/* Gets whether the property is set To automatic (see CAPTURE_PROPERTIES, above) */
PrototypeC getCapturePropertyAutoProc(deviceno, prop);
;/* Set camera property To a value (0..1) And whether it should be set To auto. */
PrototypeC setCapturePropertyProc(deviceno, prop, value.f, autoval);

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

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

;/* initCaptureWithOptions allows additional options To be given. Otherwise it's identical with initCapture
PrototypeC initCaptureWithOptionsProc(deviceno, *aParams.SimpleCapParams, aOptions)


Global countCaptureDevices.countCaptureDevicesProc
Global initCapture.initCaptureProc
Global deinitCapture.deinitCaptureProc
Global doCapture.doCaptureProc
Global isCaptureDone.isCaptureDoneProc
Global initCOM.initCOMProc
Global getCaptureDeviceName.getCaptureDeviceNameProc
Global getCapturePropertyValue.getCapturePropertyValueProc
Global getCapturePropertyAuto.getCapturePropertyAutoProc
Global setCaptureProperty.setCapturePropertyProc  
Global ESCAPIDLLVersion.ESCAPIDLLVersionProc
Global initCaptureWithOptions.initCaptureWithOptionsProc

Macro MyGetFunction(var, dll, function)
  var = GetFunction(dll, function)
  If var = 0
    Debug "Unsupported function : " + function
    error = #True
  EndIf
EndMacro

Procedure setupESCAPI()
   
  ; load library
  capdll = OpenLibrary(#PB_Any, "escapi.dll")
  If capdll = 0
    ProcedureReturn 0
  EndIf
  
  Define init
  
   ;/* Fetch function entry points */
  Define error = #False
  MyGetFunction(countCaptureDevices    , capdll, "countCaptureDevices")
  MyGetFunction(initCapture            , capdll, "initCapture")
  MyGetFunction(deinitCapture          , capdll, "deinitCapture")
  MyGetFunction(doCapture              , capdll, "doCapture")
  MyGetFunction(isCaptureDone          , capdll, "isCaptureDone")
  MyGetFunction(initCOM                , capdll, "initCOM")
  MyGetFunction(getCaptureDeviceName   , capdll, "getCaptureDeviceName")
  MyGetFunction(ESCAPIDLLVersion       , capdll, "ESCAPIDLLVersion")
  ;MyGetFunction(initCaptureWithOptions , capdll, "initCaptureWithOptions")
  MyGetFunction(getCapturePropertyValue, capdll, "getCapturePropertyValue")
  MyGetFunction(getCapturePropertyAuto , capdll, "getCapturePropertyAuto")
  MyGetFunction(setCaptureProperty     , capdll, "setCaptureProperty")
  
  If error
    Debug "Error while getting some dll functions."
  EndIf
   
   ;/* Verify DLL version */
   If ESCAPIDLLVersion() < $200
     ProcedureReturn 0
   EndIf
 
   ;/* Initialize COM.. */
   initCOM(); 
 
  ; returns number of devices found
  ProcedureReturn countCaptureDevices()
EndProcedure 
Last edited by djes on Wed Sep 13, 2017 8:22 am, edited 1 time in total.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

I added the following lines to the new escapi.pbi:

Code: Select all

#VideoProcAmp_Flags_Auto	= 1
#VideoProcAmp_Flags_Manual	= 2
The new code compiles and runs but I am not seeing any change to the camera image.

The following two lines of code both return zero:

Code: Select all

setCaptureProperty(#DEVICE,#CAPTURE_GAMMA,50,#VideoProcAmp_Flags_Manual)
Debug getCapturePropertyValue(#DEVICE,#CAPTURE_GAMMA)
I chose gamma because the default is 1 and the range is 1 to 500, so I should get a non-zero result regardless.
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'll look tomorrow but normally, the value is a float, from 0 to 1
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

VideoProcAmp_Gamma
Specifies the gamma, as gamma * 100. Values range from 1 to 500.
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:
VideoProcAmp_Gamma
Specifies the gamma, as gamma * 100. Values range from 1 to 500.
Normally, but escapi uses normalized ranges from 0..1 and converts to the needed range.
Same for auto, to be set as TRUE/FALSE, not 1/2.

I've slighly modified the PBI to return a float value from getCapturePropertyValueProc().
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

escapi uses normalized ranges from 0..1 and converts to the needed range.
Same for auto, to be set as TRUE/FALSE, not 1/2.
Right, but we're dealing with DirectShow here.

https://msdn.microsoft.com/en-us/librar ... s.85).aspx
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:
escapi uses normalized ranges from 0..1 and converts to the needed range.
Same for auto, to be set as TRUE/FALSE, not 1/2.
Right, but we're dealing with DirectShow here.

https://msdn.microsoft.com/en-us/librar ... s.85).aspx
:? You're using escapi.dll, right ? Escapi is using directshow, but converts your values (0..1) to needed directshow values. For example, if you're using setCaptureProperty(device, #CAPTURE_HUE, 1, #False); escapi asks for a hue of 180. See capture.cpp/lines 292, 296, 311, 315.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

Any progress?
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'm sorry but, progress about what ?
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

djes wrote:I'm sorry but, progress about what ?
PB control of webcam proc amp controls.
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 »

It works perfectly for me, just by using floating point values instead of integer.

I can change this behaviour in the dll if you want, but beware that some parameters could be difficult to adjust.
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

It works now -- I had to straighten out the values of the flags. Your help is very much appreciated.

Next will be to discover the ranges of these controls and to work on the pan, tilt, zoom etc.
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 »

Thank you ! The escapi calculus is based on previously quoted values from directshow, reduced on the 0..1 range. So you have to do the inverse operation to have the correct values.
For example with
VideoProcAmp_Contrast
Specifies the contrast, expressed as gain factor * 100. Values range from zero to 10,000.
the correct value is calculated with a cross product like this :

Code: Select all

contrastmax = 10000 : contrastmin = 0
mycontrast = 5000
Escapicontrast.f = mycontrast / (contrastmax - contrastmin) ; = should be 0.5
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: Another Webcam Demo Program

Post by chris319 »

Not all webcams support all of the controls. For example, gamma and sharpness are not supported on the Logitech BRIO. Better to set them to unity (0.5) on the camera and apply one's own controls.

I have added the following:

Code: Select all

#VideoProcAmp_Flags_Auto = #True
#VideoProcAmp_Flags_Manual = #False
Post Reply