logo logo
Home arrow Forums...
Tuesday, 07 October 2008
 
 
English English  Español Español  
Tag Cloud
Architectures Artificial Associations Books Brain Conscious Consciousness Forums Howto Machine Machine Consciousness Machines Microsoft Neuroscience Projects Publications Research Researchers Reviews Robot Robotics Robots Services Studio
Conscious-Robots.com Forum  


Raúl
Moderator

Moderator
Posts: 388
graph
Karma: 6  
Simulated WebCam exception in QueryFrame - 2007/09/18 12:31 I found a bug in the Robotics Studio SimulatedWebcam service.

The problem is that after a few seconds (some times it takes longer) I get an InvalidOperationException in the QueryFrameHandler method in file SimulatedWebcam.cs (The lines causing the exception are these:

Code:

 // size not specified if(size.Width == 0)   size _state.Image.Size;



see the rest of the code below)

The runtime is complaining because Object (_state.Image) is currently in use elsewhere.

I think the size assignment should be moved within the next try-catch. Also the method QueryFrameHandler should declared as Exclusive, because the image is being locked in the method.

Microsoft are making these changes for the next release.

Code:

 [ServiceHandler(ServiceHandlerBehavior.Concurrent)] public IEnumerator<ITaskQueryFrameHandler(webcam.QueryFrame query) { if (_state.Image == null) { query.ResponsePort.Post(new webcam.QueryFrameResponse()); yield break; } Size size = new Size((int)query.Body.Size.X, (int)query.Body.Size.Y); if (query.Body.Format == Guid.Empty) { // raw image requested; BitmapData raw null;  // size not specified if(size.Width == 0)   size _state.Image.Size; try { raw _state.Image.LockBits(new Rectangle(Point.Empty, size), ImageLockMode.ReadOnlyPixelFormat.Format24bppRgb); int byteSize raw.Height raw.Stride; webcam.QueryFrameResponse response = new webcam.QueryFrameResponse(); response.TimeStamp _state.LastFrameUpdate; response.Frame = new byte[byteSize]; response.Size = new Size(raw.Widthraw.Height); response.Format Guid.Empty; System.Runtime.InteropServices.Marshal.Copy(raw.Scan0response.Frame0byteSize); query.ResponsePort.Post(response); } catch (Exception ex) { query.ResponsePort.Post(Fault.FromException(ex)); } finally { if (raw != null) { _state.Image.UnlockBits(raw); } } } else { ImageFormat format = new ImageFormat(query.Body.Format); using (MemoryStream stream = new MemoryStream()) {  if (size == _state.Image.Size || size.Width == || size.Height == || size.Width >= _state.Image.Width || size.Height >= _state.Image.Height) { size _state.Image.Size; _state.Image.Save(streamformat); } else { using (Bitmap temp = new Bitmap( _state.Imagesize)) { temp.Save(streamformat); } } webcam.QueryFrameResponse response = new webcam.QueryFrameResponse(); response.TimeStamp _state.LastFrameUpdate; response.Frame = new byte[(int)stream.Length]; response.Size size; response.Format format.Guid; stream.Position 0; stream.Read(response.Frame0response.Frame.Length); query.ResponsePort.Post(response); } } yield break; }



Post edited by: Raúl, at: 2007/09/18 12:45
Raúl Arrabales Moreno. conscious-robots.com/raul
  The administrator has disabled public write access. Please, register to participate in the forum.
Raúl
Moderator

Moderator
Posts: 388
graph
Karma: 6  
Re:Simulated WebCam exception in QueryFrame - 2007/09/18 13:25 The code after making the mentioned changes is as follows (this code seems to work ok. I am unsure whether or not some frames are loss when the method is called when the simulator engine has blocked the image).

Code:

         // Raul - Service handler changed from Concurrent to Exclusive.         [ServiceHandler(ServiceHandlerBehavior.Exclusive)]         public IEnumerator<ITaskQueryFrameHandler(webcam.QueryFrame query)         {             if (_state.Image == null)             {                 query.ResponsePort.Post(new webcam.QueryFrameResponse());                 yield break;             }             Size size = new Size((int)query.Body.Size.X, (int)query.Body.Size.Y);             if (query.Body.Format == Guid.Empty)             {                 // raw image requested;                 BitmapData raw null;                 try                 {                     // Raul - This check moved into the try-catch block.                     // size not specified                     if (size.Width == 0)                         size _state.Image.Size;                     raw _state.Image.LockBits(new Rectangle(Point.Empty, size),                         ImageLockMode.ReadOnlyPixelFormat.Format24bppRgb);                     int byteSize raw.Height raw.Stride;                     webcam.QueryFrameResponse response = new webcam.QueryFrameResponse();                     response.TimeStamp _state.LastFrameUpdate;                     response.Frame = new byte[byteSize];                     response.Size = new Size(raw.Widthraw.Height);                     response.Format Guid.Empty;                     System.Runtime.InteropServices.Marshal.Copy(raw.Scan0response.Frame0byteSize);                     query.ResponsePort.Post(response);                 }                 catch (Exception ex)                 {                     query.ResponsePort.Post(Fault.FromException(ex));                 }                 finally                 {                     if (raw != null)                     {                         _state.Image.UnlockBits(raw);                     }                 }             }             else             {                 ImageFormat format = new ImageFormat(query.Body.Format);                 using (MemoryStream stream = new MemoryStream())                 {                                         if (size == _state.Image.Size ||                         size.Width == ||                         size.Height == ||                         size.Width >= _state.Image.Width ||                         size.Height >= _state.Image.Height)                     {                         size _state.Image.Size;                         _state.Image.Save(streamformat);                     }                     else                     {                         using (Bitmap temp = new Bitmap(                             _state.Imagesize))                         {                             temp.Save(streamformat);                         }                     }                     webcam.QueryFrameResponse response = new webcam.QueryFrameResponse();                     response.TimeStamp _state.LastFrameUpdate;                     response.Frame = new byte[(int)stream.Length];                     response.Size size;                     response.Format format.Guid;                     stream.Position 0;                     stream.Read(response.Frame0response.Frame.Length);                     query.ResponsePort.Post(response);                 }             }             yield break;         }

Raúl Arrabales Moreno. conscious-robots.com/raul
  The administrator has disabled public write access. Please, register to participate in the forum.





Lost Password?
No account yet? Register
Last Posts in Forum
Last comments
My review of Conscious Machines
Review of the book ‘The Co...
23/09/08 12:32 More...
By Raúl

Re: How do i use this in a simulated rob
Hi, let me answer...
12/08/08 14:46 More...
By Raúl

How do i use this in a simulated robot?
I\'m entirely new ...
10/08/08 16:30 More...
By carlcs

How do i use this in a simulated robot?
I\'m entirely new ...
10/08/08 16:29 More...
By carlcs

Foro de Robotics Studio en Español
Hola sutaro, he copiado...
17/07/08 14:32 More...
By Raúl

fallo de ejecución:Unhandled within caus
Hola a todos, es ...
17/07/08 13:03 More...
By sutaro

Amazed Challenge 2
Good news for those of you who missed t...
03/07/08 11:21 More...
By Raúl

Poster Session Pictures
Some pictures of the poster sessio...
08/04/08 13:17 More...
By Raúl

Final Announcement and Conference Overvi
Final Announcemen...
07/02/08 12:45 More...
By Raúl

Discussion about test for consciousness
More information a...
29/01/08 15:01 More...
By Raúl

 
Top! Top!