xna-4.0


XNA, EmguCV and perfomance issue with webcam


I started working with EmguCV, but I stumble upon a performance issue trying to convert from the grabber's bitmap to a Texture2D
This is my code
private void FrameGrabber()
{
NamePersons.Add("");
currentFrame = grabber.QueryFrame().Resize(320, 240, INTER.CV_INTER_CUBIC);
gray = currentFrame.Convert<Gray, Byte>();
MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(face, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
foreach (MCvAvgComp f in facesDetected[0])
{
t = t + 1;
result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
currentFrame.Draw(f.rect, new Bgr(System.Drawing.Color.Red), 2);
if (trainingImages.ToArray().Length != 0)
{
MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
EigenObjectRecognizer recognizer = new EigenObjectRecognizer(trainingImages.ToArray(), labels.ToArray(), 2500, ref termCrit);
name = recognizer.Recognize(result);
currentFrame.Draw(name, ref font, new System.Drawing.Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(System.Drawing.Color.LightGreen));
}
NamePersons[t - 1] = name;
NamePersons.Add("");
}
t = 0;
for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
names = names + NamePersons[nnn] + ", ";
names = "";
NamePersons.Clear();
Bitmap b = currentFrame.ToBitmap();
//slow
using (MemoryStream s = new MemoryStream())
{
b.Save(s, System.Drawing.Imaging.ImageFormat.Png);
s.Seek(0, SeekOrigin.Begin); //must do this, or error is thrown in next line
frame = Texture2D.FromStream(GraphicsDevice, s);
}
////second option but image is bluish and still slow
//GraphicsDevice.Textures[0] = null;
//if (frame == null || b.Width != frame.Width || b.Height != frame.Height)
// frame = new Texture2D(GraphicsDevice, b.Width, b.Height);
//BitmapData bData = b.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(), b.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
//int byteCount = bData.Stride * b.Height;
//byte[] bmpBytes = new byte[byteCount];
//Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount);
//b.UnlockBits(bData);
//frame.SetData(bmpBytes);
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
grabber = new Capture();
}
protected override void UnloadContent()
{
grabber.Dispose();
}
protected override void Update(GameTime gameTime)
{
FrameGrabber();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.LightGray);
spriteBatch.Begin();
if (frame !=null)
spriteBatch.Draw(frame, new Microsoft.Xna.Framework.Rectangle(0, 0, ScreenWidth, ScreenHeight), Microsoft.Xna.Framework.Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
I searched the web for documentation but with no luck.
Well I solved my problem creating a class that start a parallel thread and handle the frame conversion coming from the webcam.
If anyone is interested, to convert to texture and also handle the face recognition I used this:
private Image<Gray, byte> gray = null;
private HaarCascade haarCascade = new HaarCascade("haarcascade_frontalface_default.xml");
private void QueryFrame()
{
while (is_running)
{
nextFrame = capture.QueryFrame().Flip(FLIP.HORIZONTAL);
if (nextFrame != null)
{
gray = nextFrame.Convert<Gray, Byte>();
MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(haarCascade, 1.2, 10, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new System.Drawing.Size(20, 20));
foreach (MCvAvgComp face in facesDetected[0])
nextFrame.Draw(face.rect, new Bgr(System.Drawing.Color.Red), 2);
byte[] bgrData = nextFrame.Bytes;
for (int i = 0; i < colorData.Length; i++)
colorData[i] = new Color(bgrData[3 * i + 2], bgrData[3 * i + 1], bgrData[3 * i]);
}
}
}

Related Links

How to add a start (splash) screen in XNA 4.0?
My program is being reported as a high-level security threat by AVG?
How to add an end screen in XNA 4.0?
Texture2D is way oversized in XNA 4.0?
How to set xna's TextureFilter to Point
Why doesnt my xna pixel shader turn this texture blue?
How to calibrate kinect skeleton data to video feed?
XNA game studio4.0 using c#
How to add collision detection in a 2D car game in XNA?
Texturing a terrain causes Windows Phone to crash (without an exception) or Models are not drawn afterwards
compiling and deploy window mobile phone platform games into xbox360 game platform with XNA 4.0
'Expected class, delegate, enum, interface,or struct' in XNA 4.0?
Having errors running JigLibX 0.3.1 on XNA 4.0
How to remove splash screen?
Layouting a row of bricks on the wall in XNA 4
Xbox 360 XNA HLS Streaming

Categories

HOME
openlayers
hook
pycharm
google-api-php-client
grep
cplex
rsyslog
amortized-analysis
jsrender
webpack-2
ezpublish
wamp
in-app-purchase
node-notifier
slurm
midi
kentor-authservices
etl
jplayer
remote-access
dynamics-crm-online
clearcase-ucm
telephony
after-effects
http-status-code-504
visual-composer
conemu
reverse-proxy
autocad-plugin
paging
java-3d
chromium-embedded
crystal-reports-2010
dynamic-featured-image
phpfox
web-api-testing
spring-mybatis
msys2
jaxb2
libssl
c++-amp
procdump
instant-messaging
phonegap
gtrendsr
column-family
sequential
webix-treetable
jna
streamreader
opshub
lumberjack
galen
http-redirect
import-from-excel
gpx
convertapi
sas-jmp
logfiles
clean-architecture
directory-structure
pg-dump
knpmenubundle
qtwebview
iso8601
lync-client-sdk
cubes
url-pattern
radtreelist
dstu2-fhir
phishing
mikroc
contact-list
lttng
holder.js
nsviewcontroller
pickadate
tform
pervasive-sql
teamcity-8.0
ionic
dundas
bridge.net
oxwall
bitcoinj
elliptic-curve
reactfx
system.net.webexception
mbr
monomac
terminal-services
yui-compressor
type-equivalence
angularjs-controller
anonymous-methods
browser-detection
sublist
gil
propertyeditor
wse3.0
transactionscope
stage
recent-documents
datareader
telerik-scheduler
infrastructure
mirah
microsoft-virtualization
exchange-server-2003
ctp4
virtual-functions
boost-smart-ptr
project-hosting
port-number
lzh
uiq3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App