go to http://oracle.in.th

Wednesday, September 30, 2009

Part2 Motion Detection: Capture Image

ส่วนนี้ผมจะอธิบายคร่าวๆ ถึงการจับภาพที่ได้จากกล้อง โดยมีคลาสสำคัญที่เกี่ยวข้องได้แก่ Camera, Video, BitmapData
ลำดับการทำงาน (วาดภาพน่าจะเข้าใจง่ายขึ้น...             หรอ)

Part2 Motion Detection: Capture Image
อธิบายคร่าวๆ ก่อนเขียน (สงสัยตรงไหนถามเอาละกันครับ)
  • เริ่มแรกกำหนดตัวแปรที่จะใช้ให้เรียบร้อย
  • สร้างปุ่มเมื่อกดกล้องก็เริ่มทำงาน
  • เมื่อโปรแกรมเริ่มทำงานจะทำการเก็บภาพ ทุกๆ 5 นาทีและทุกครั้งที่มีการเคลื่อนไหว
  • เมื่อเก็บภาพได้นำภาพไปแสดงที่หน้าจอจัดระยะห่างกับวางตำแหน่งให้เรียบร้อย
  • ส่งภาพขึ้นเซิร์ฟเวอร์ (sendMsg function)

จากนั้นลงมือเขียนตามแรงศรัทธา

var cam:Camera = Camera.getCamera();
var vid:Video;
var now:BitmapData;
var listCapture:Array = new Array();

var stageWidth = 440;
var stateHeight = 240;
var vidWidth = 160;
var vidHeight = 120;
var fps = 30;

var activeInterval;
var captureInterval;
var camStatusInterval;
var countImage:int = 0;

var txtStatus:TextField = new TextField();
var txtFormat:TextFormat = new TextFormat();
txtFormat.color = 0xC8C8C8;
txtFormat.size = 10;
txtFormat.font = "Arial";
txtStatus.defaultTextFormat = txtFormat;
txtStatus.x = 0;
txtStatus.y = 0;
txtStatus.width = stageWidth;
txtStatus.height = stateHeight;
txtStatus.type = TextFieldType.DYNAMIC;
txtStatus.selectable=false;
txtStatus.text = "status: ";
addChildAt(txtStatus, 0);

var btnStart:Button = new Button();
btnStart.label = "Start Capture";
btnStart.setSize(120, 30);
btnStart.move(250, 40);
btnStart.toggle = true;
btnStart.addEventListener(MouseEvent.CLICK, startCapture);
addChild(btnStart);

if (cam != null) {
setStatus("camera : " + cam.name);
} else {
setStatus("camera not detect");
}

function startCapture(event:MouseEvent):void {
if (btnStart.selected) {
if (cam != null) {
vid = new Video(vidWidth, vidHeight);
cam.setMode(vidWidth, vidHeight, fps, true);
vid.attachCamera(cam);
cam.addEventListener(StatusEvent.STATUS, camStatusHandler);
cam.addEventListener(ActivityEvent.ACTIVITY, camActivityHandler);
} else {
setStatus("camera not detect");
}
btnStart.label = "capture image: ";
btnStart.enabled = false;
}
}

function camStatusHandler(event:StatusEvent):void {
if (!cam.muted) {
addChildAt(vid, 0);
vid.x = 20;
vid.y = 20;
captureInterval = setInterval(capture, 5 * 60 * 1000);
camStatusInterval = setInterval(camStatus, 1000);
} else {
setStatus("camera has't permission");
Security.showSettings(SecurityPanel.CAMERA)
}
}

function camActivityHandler(event:ActivityEvent):void {
if(cam.muted){
setStatus("camera has't permission");
Security.showSettings(SecurityPanel.CAMERA)
}
if (event.activating) {
activeInterval = setInterval(capture, 500);
}
}

function capture():void {
var captureData:BitmapData = new BitmapData(vidWidth, vidHeight);
captureData.draw(vid);
listCapture.push(addChild(new Bitmap(captureData)));
listCapture[listCapture.length-1].x = 20 + (listCapture.length-1)%5 * vidWidth/2;
listCapture[listCapture.length-1].y = vidHeight + 40;
listCapture[listCapture.length-1].width = vidWidth/2;
listCapture[listCapture.length-1].height = vidHeight/2;
++countImage;
sendMsg(captureData);
clearInterval(activeInterval);
}

function camStatus():void {
setStatus("currentFPS: " + (cam.currentFPS) + "\n");
}

function setStatus(status:String):void {
txtStatus.text = status;
}
จากนั้นรันเลย ช่วงนี้ใครเดินผ่านไปผ่านมาแถวโต๊ะผมเก็บภาพไว้หมดหละ ตัดภาพมาบางส่วนเพื่อไม่ให้กระทบต่อความมั่นคงต่อแหล่งที่มาของภาพ!

Capture
ถ้ารู้สึกวิธีนี้ไม่ยืดหยุ่นพอลองใช้แบบของนายคนนี้ แล้ววัดระดับ sensitive เอาก็ได้ครับ ตอนหน้ามาส่งภาพขึ้นเซิร์ฟเวอร์
ข้อเขียนนี้ช่วยฉัน:  

5 comments:

Tom Condo said...

โหวว พี่ยุทธ สุดยอดเลยอ๊า~~~~

ขอยืมมาใช้กะเครื่องมั่งสิ จะได้ดูว่าแม่แอบมา Chat MSN แทนเตยรึป่าว


PS. เตยเคยเขียน Face Detection เหมือนกัน เอามาทำ Game ส่ง Project ตอนเรียน ^_^

Unknown said...

Face Detection น่าสนใจดีนะ ว่างๆ มาแชร์ความรู้ให้ฟังบ้างดิ :D

Kim said...

@phamonyut, project นี้ coding เสร็จแล้วใช่มั้ย?

Rice Cooker said...

@phamonyut ภาพอธิบายข้างบน ดูแล้วเข้าใจ

เข้ามาฮา @Sanivipa แล้วผมจะรู้ไหมว่าตอนคุยกับ @Sanivipa ผมไม่ได้คุยกับแม่ของ @Sanivipa

เอางี้ทำ Motion Detection: Capture Image เพื่อเปลี่ยน display ตามคนที่เล่นอยู่ โดยจะเปลี่ยนตามคนที่เปลี่ยนไปโดยใช้ Face Detection ของ @Sanivipa ส่วน Motion Detection ของ @phamonyut มารวมกัน

ปล. แต่ว่าใครเขียน.....(ผมไม่เขียนนะแต่จะ test ให้ 555+)

Unknown said...

@sirichai feature หลักเสร็จแล้วครับ เหลือเพิ่มอะไรบางอย่างอีกเล็กน้อย

Post a Comment