Page 1 of 1

Help with live webcam code - OpenCV_PB?

Posted: Wed Oct 23, 2013 9:23 pm
by Mythros
Hi all. I'm working on a live web streaming program, and it was going great until I ran into a stack overflow in the OpenCV PB module function call, called "cvQueryFrame()"...

It crashes at:
*Image.IplImage = cvQueryFrame(*capture)
I just want this to work with my Carmine 1.09 webcam. I need to turn it into a DLL so it can work with more than just Purebasic.

I need the live image to be applied to a texture.

PLEASE NOTE:

This requires the following OpenCV libraries:
  • highgui100.dll
    cxcore100.dll
    libguide40.dll
Download:

http://sourceforge.net/projects/opencvl ... v-win/1.0/

Download the file: "OpenCV_1.0.exe" & go to the following directory to find the above library files:

"OpenCV\bin"

and put all 3 of the above DLL files into the same directory as where the below source is, then open the source file you saved from below, and compile it.

NOTE: Uses PB 5.11 & higher!

Webcam.pb:

Code: Select all

; Live webcam with OpenCV
; AAT, 2011
;------------------------

InitSprite()
InitKeyboard()

OpenWindow(0, 0, 0, 1, 1, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 1, 0, 0, 0)
HideWindow(0, 1)

#CV_WINDOW_AUTOSIZE = 1

 Structure IplImage
   nSize.i
   id.i
   nChannels.i
   alphaChannel.i
   depth.i 
   colorModel.b[4]
   channelSeq.b[4]
   dataOrder.i
   origin.i
   align.i
   width.i
   height.i
   *roi
   *maskROI
   *imageId
   *tileInfo
   imageSize.i
   *imageData
   widthStep.i
   BorderMode.i[4]
   BorderConst.i[4]
   *imageDataOrigin
 EndStructure

;-- highgui --
  Prototype.l ProtocvNamedWindow(name.s, flags.l = #CV_WINDOW_AUTOSIZE)
  Prototype.l ProtocvDestroyWindow(name.s)
  Prototype.l ProtocvShowImage(name.s, image.l)
  Prototype.l ProtocvWaitKey(delay.l = 0)
  
  ;-- video in highgui --
  Prototype.l ProtocvCreateCameraCapture(Index.i)
  Prototype.l ProtocvGrabFrame(CapturePtr.l );
  Prototype.l ProtocvQueryFrame(CapturePtr.l);  
  Prototype.l ProtocvReleaseCapture(*CvCapture)
;------------

If OpenLibrary(1, "highgui100.dll")
  pcvNamedWindow.ProtocvNamedWindow = GetFunction(1, "cvNamedWindow")
  pcvDestroyWindow.ProtocvDestroyWindow = GetFunction(1, "cvDestroyWindow")
  PcvShowImage.ProtocvShowImage = GetFunction(1, "cvShowImage")
  PcvWaitKey.ProtocvWaitKey = GetFunction(1, "cvWaitKey")  
  cvCreateCameraCapture.ProtocvCreateCameraCapture = GetFunction(1,"cvCreateCameraCapture")
  cvQueryFrame.ProtocvQueryFrame = GetFunction(1,"cvQueryFrame")
  cvReleaseCapture.ProtocvReleaseCapture = GetFunction(1,"cvReleaseCapture")
EndIf

win.i = pcvNamedWindow("Live Camera", #CV_WINDOW_AUTOSIZE)

*capture = cvCreateCameraCapture(0)

ExamineKeyboard()

While Not KeyboardPushed(#PB_Key_Escape)
  *Image.IplImage = cvQueryFrame(*capture)
  If *Image
    ret.i = pcvShowImage("Live Camera", *Image) 
    If ret = 0                                    ; Window is closed
      Break
    EndIf  
  EndIf
Wend   
cvReleaseCapture(@*capture)
pcvDestroyWindow("Live Camera")
End
Thank you all SO kindly, and have a GREAT day! =)

Mythros

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 3:52 am
by JHPJHP
Original Source: http://www.purebasic.fr/english/viewtop ... eraCapture

----------------------------------------------------------

Working
- PureBasic 5.20 32bit
- Windows 7 64bit
- Sony Visual Communication Camera

Updates
- got past the Stack Overflow, but was still getting a black screen
-- code & Structure changes
- added cvSaveImage for further testing
- downloaded the lastest version (v2.4.6): http://opencv.org/
-- only included the x86 version of the OpenCV DLL's in the link
--- opencv_core246.dll
--- opencv_highgui246.dll
- downloaded other required DLL's (included in the link)
-- msvcp110.dll
-- msvcr110.dll

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 5:54 am
by JHPJHP
Updates
- cvSaveImage crashed when changing the name from within the function
- added exit after saving 10 frames

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 6:16 am
by jesperbrannmark
Carmine 1.09 is not a normal webcam - it is Primesense 3d sensing device (like kinect). Does that really open in native opencv - dont you need to use openni and nite middleware?

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 6:33 am
by JHPJHP
Nice catch jesperbrannmark - this may help (as a start) if the script doesn't work OOTB: http://docs.opencv.org/doc/user_guide/ug_highgui.html

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 9:26 am
by JHPJHP
Updates
- included the x64 DLL's with working example (same example as x86)
- moved the cvSaveImage function below cvShowImage otherwise it caused a crash
- x86 DLL's are from the vc11 folder located in the OpenVC download package
- x64 DLL's are from the vc10 folder (vc11 version did not work with PureBasic 64bit)

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 7:10 pm
by JHPJHP
Updates
- added Esc key [ Exit ] via OpenCV function: cvWaitKey
- added Mouse Event Callback
-- you will probably want to disable image saving (cvSaveImage)
-- you will need to remove the 10 frame limit (to quick)

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 7:17 pm
by Fred
Works great here

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 7:32 pm
by Mythros
Tried it with my carmine AND kinect. it doesnt work with either. it just closes after a few seconds.

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 7:35 pm
by JHPJHP
Thanks Fred.

-----------------------

Hi Mythros,

Did you get a chance to look at the following - from previous post:
Nice catch jesperbrannmark - this may help (as a start) if the script doesn't work OOTB: http://docs.opencv.org/doc/user_guide/ug_highgui.html

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 7:53 pm
by JHPJHP
Hi Mythros,

Can you check the folder your running it from, particularly the Images folder being created; do you have any images in it?

The script is written to only stay open for a few seconds, then close after it has captured 10 images - a second or two at most.

Otherwise, hopefully the link I supplied will be helpful.

Cheers!

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 8:25 pm
by Mythros
Hi, JHP, I did check the folder, and it wasn't even created. I had to create it myself. I then checked it after running it, and to no avail, unfortunately. :(

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 8:37 pm
by JHPJHP
You may want to try increasing the number of attempts attaching to the camera:

Code: Select all

Repeat
  nCreate + 1
  *capture = cvCreateCameraCapture(0)
  Delay(1000)
Until nCreate = 10 Or *capture
Otherwise you will have to follow the instructions from the link - Sorry, really wish it would have worked OOTB.

Good Luck,

Re: Help with live webcam code - OpenCV_PB?

Posted: Thu Oct 24, 2013 10:04 pm
by Mythros
I don't understand any of this, nor' do I care to. I don't like C#, C++ OR VB.net.

I like to stick with what I am familiar with, which is the BASIC language family.