Help with live webcam code - OpenCV_PB?

Just starting out? Need help? Post your questions and find answers here.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Help with live webcam code - OpenCV_PB?

Post 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
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with live webcam code - OpenCV_PB?

Post 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
Last edited by JHPJHP on Thu Oct 24, 2013 7:11 pm, edited 4 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with live webcam code - OpenCV_PB?

Post by JHPJHP »

Updates
- cvSaveImage crashed when changing the name from within the function
- added exit after saving 10 frames
Last edited by JHPJHP on Thu Oct 24, 2013 4:28 pm, edited 4 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Help with live webcam code - OpenCV_PB?

Post 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?
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with live webcam code - OpenCV_PB?

Post 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
Last edited by JHPJHP on Thu Oct 24, 2013 4:28 pm, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with live webcam code - OpenCV_PB?

Post 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)
Last edited by JHPJHP on Thu Oct 24, 2013 7:12 pm, edited 2 times in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with live webcam code - OpenCV_PB?

Post 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)
Last edited by JHPJHP on Fri Oct 25, 2013 1:06 am, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Help with live webcam code - OpenCV_PB?

Post by Fred »

Works great here
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Help with live webcam code - OpenCV_PB?

Post by Mythros »

Tried it with my carmine AND kinect. it doesnt work with either. it just closes after a few seconds.
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with live webcam code - OpenCV_PB?

Post 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

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with live webcam code - OpenCV_PB?

Post 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!

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Help with live webcam code - OpenCV_PB?

Post 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. :(
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Help with live webcam code - OpenCV_PB?

Post 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,
Last edited by JHPJHP on Fri Oct 25, 2013 12:52 am, edited 1 time in total.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: Help with live webcam code - OpenCV_PB?

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