Anda di halaman 1dari 40

POLYTECHNIC UNIVERSITY OF THE PHILIPPINES CITY OF SANTA ROSA LAGUNA COLLEGE OF ENGINEEERING DEPARTMENT OF ELECTRONICS ENGINEERING

March 14, 2011 To Whom It May Concern: Greetings! This project entitled Computer Interfaced Electronic Gun with Motion Detector prepared and submitted by Group 1, in partial fulfillment of the requirements for the degree of Bachelor of Science in Electronic and Communications Engineering has been recommended for acceptance and approval for oral examination.

ENGR. Aris Carpena Adviser

Approves and accepted by the Committee on Oral Examination with a grade of ____.

1|Page

ACKNOWLEDGEMENT

We would like to acknowledge all the people behind us who gave us inspiration and helped us a lot to make this project possible. And we would like to extend our deepest gratitude to Engr. Aris Carpena who made us believe that our idea about this project can be made into reality. We are thanking you for helping us at times of trouble, and for all the efforts and patience you have given us in order to finish this project; to our parents who are very supportive and understanding without them this wouldnt be possible. And most especially we thank our Almighty God for giving us knowledge, patience and courage to plan and make it real. Without Him we cannot do anything, the project was done all by His grace.

2|Page

TABLE OF CONTENTS

Letter of transmittal.1 Acknowledgement...2 Abstract...4 Objective......5 Introduction.6 Related Studies7 System Layout . . . ... 11 Program Flowchart.....14 Circuit Diagram.18 Conclusion.20 Recommendation...21 Appendix A22 Appendix B....25

3|Page

ABSTRACT

Ever imagine bringing into life the most popular computer game Counter Strike? Yes! It is possible, controlling the gun through computer interfacing; observing and examining every part of a specific area, targeting and shooting or executing the enemy. The project will meet the requirement of providing protection and security of the user and not to harm others. The system is design to reduce the chance of encountering harm or loss of the people responsible for the security of a certain place. And it is by means of incorporating these advance gadgets and devices; the computer having the keyboard and mouse that will direct and control every movement of the gun ( moving up and down, left to right) and the two cameras (as surveillance and aim target).

4|Page

OBJECTIVES

General Objective To be able to develop a security system that will use an Electronic Gun through Computer Interfacing.

Specific Objective To be able to reduce the risk encountered by the security personnel in the field. To be able to create a VB program that can control the movements of the electronic gun.

To be able to develop a motion detecting software that will automatically sense an intruder.

5|Page

INTRODUCTION

It is the year 2011 and various technologies have conquered our home planet Earth. From entertainment to communication and even security system, innovation has always been their main focus; to develop gadgets and devices that will satisfy the growing needs of every individual. Security is the state of being secure; the freedom from danger, an undeniable necessity of each and everyone. It is inevitable or our essential to live. Protection is being implemented in every home, some establishments like banks and also on those safe houses, because they are in the state of wanting to avoid the risk of danger. The study aims to provide an advanced security system that can possibly reduce and avoid threats. It is by integrating an electronic gun and computer technology, a system that will combine the gun which will serve as the defense mechanism and computer as the medium from the user on the way to the device. Through computer interfacing, every movement and direction of the electronic gun can be executed. This will surely offer a different kind of protection and assurance of taking the risk of danger.

6|Page

RELATED STUDIES

The Real Purpose of Security There is a lot of misunderstanding in the veterinary profession about security and violence prevention. Some folks believe the veterinary practice is not a high risk target for violence and therefore security measures are not warranted. Some believe that the addition of security measures will give the practice a fort-like atmosphere. And still others believe that security precautions create too much of an inconvenience for the staff and are not worth the effort.

Using Multiple Cameras to Monitor Effectively Different environments have different surveillance requirements. A large facility like a parking lot, store, residence, or hall cannot be monitored efficiently by a single camera. Traditionally, CCTV (Closed Circuit TV) based Surveillance Systems were used for multi camera monitoring. This solution was expensive due to the huge hardware costs Protecting a countrys borders is vital to its Homeland Security. It is, however, very challenging to detect potential intruders or smugglers in total darkness or in diverse weather conditions. Thermal imaging cameras can help border control professionals to meet the demands they face at night and in other low-light situations. Thermal imaging cameras can be integrated in a border security project with radars and other sensors in a so called slew-to-cue mode. But not only land borders need to be protected. Thernal imaging cameras are the perfect tools for coastal surveillance as well. Small vessels can

7|Page

be detected at extremely long ranges. FLIR Systems has a broad experience with Homeland Security projects and markets a wide variety of thermal imaging cameras for this application. Multi-Sensor Systems are also available. In order to help system integrators that want to include FLIR thermal imaging cameras and third party sensors in a security network we have founded FLIR Networked Systems.

About Artillery Originally applied to any group of infantry primarily armed with projectile weapons, artillery has over time become limited in meaning to refer only to those engines of war that operate by projection of munitions far beyond the range of effect of personal weapons. These engines comprise specialized devices which use some form of stored energy to operate, whether mechanical, chemical, or electromagnetic. Originally designed to breach fortifications they have evolved from nearly static installations intended to reduce a single obstacle to highly mobile weapons of great flexibility in which now reposes the greater portion of a modern army's offensive capabilities. In common speech the word artillery is often used to refer to individual devices, together with their accessories and fittings, although these assemblages are more properly referred to as equipments. By association, artillery may also refer to the arm of service that customarily operates such engines. Artillery may also refer to a system of applied scientific research relating to the design, manufacture and employment of artillery weapon systems although, in general, the terms ballistics and ordnance are more commonly employed in this sense.

8|Page

High tech surveillance gadget At the same time the simple fact that you have CCTV on your building at all means that you will deter a large number of crimes from happening in the first place people will see that you have cameras installed and then will hopefully deign not to commit the crime for fear of being caught on film. However at the same time it can also bee a quite fun and certainly satisfying project that can make you feel almost like some kind of Big Brother style omnipresent overlord. It can also feel a bit like being Dr No or some other villainous character in a lair full of video equipment to capture the every movement of your trespassers (if only you could activate trap doors and unleash hounds it would be a complete effect). Thus it is a fun pass time to imagine all of the possible surveillance equipment and how you could rig them up around your home to be foolproof. Some of these, such as an IP CCTV, you might also choose to purchase and thereby enjoy having a high tech gadget that impresses guests and that protects your home from unwanted visitors. Analogue solutions are now quite dated, and far more high tech is something like IP CCTV. These are digital cameras that transmit their footage wirelessly and are often used for large business. As digital cameras this also means that your IP CCTV can be set up to use other features such as motion sensors. This way your camera won't record hours of footage, but instead will only record when there is movement. Using image analysis you can further get it to recognise humanoid figures to ensure that spiders and things don't set it off. Include VOIP in this set up and you can then get it to call you when it sees something, or to allow you to speak through it to ward off trespassers. IP CCTV then

9|Page

allows you to wirelessly transmit the footage to a device, such as your netbook. This way you can watch someone in another room. For a more low-tech but creative solution you can set up baby alarms around the home. Using these it is possible to hear what is going on in every room from your one location and this would mean you'd be alerted to trespassers or movement in any of those rooms. It's also possible to purchase bugging devices as might be found in a spy novel and then to locate these in secretive locations around a room in order to listen to a target and these could also of course be used as surveillance devices. Another great gadget that can be used for surveillance is a remote controlled CCTV camera. These you control via any wireless network, and this allows you to drive the CCTV which comes in many forms around your home so that you can check for trespassers while abroad while also checking on your pets and other aspects of your home.

10 | P a g e

SYSTEM LAYOUT

USER INTERFACE

Serial Data from the Cameras

PERSONAL COMPUTER

SURVEILANC E CAMERA Guns View

MCU Microcontroller Unit


PWM Signals ServoMotor Controller

RELAY DRIVERS

Controls the guns triggering mechanism, flashlight, and laser pointer

CIRCUIT

11 | P a g e

SYSTEM DESCRIPTION

The system was composed of an electronic gun, a microcontroller circuit and a computer unit (e.g., laptops, desktop PC). The electronic gun is mounted in a mechanical frame, which allows the gun to perform rotational movements. The frame is made from aluminum and acrylic materials. Acrylic is a kind of plastic, a material that possess both hardness and flexibility, but it is prone to breakage, so it is reinforced with aluminum, a malleable, rigid but lightweight material, to sustain the needed rigidity of the frame. Then it is mounted in a wood base that will support the moving frame, together with the systems circuitry. The guns rotational movement is controlled by the servomotors placed in the joints of the frame. The servomotors movement is directed by a microcontroller unit (MCU) by sending signals to it. Servomotors interpret the signal as an angle in of rotation of the motor. The control unit which composes the MCU and the relay drivers are the decoder part of the electronic guns system. It interprets the serial information, that the computer unit is sending, as a command that the MCU is going to perform, such as instructing the servo motors , and controlling the relay drivers, which controls the triggering mechanism of the gun and switching mechanism of the guns flashlight and laser pointers. The serial data that the computer unit is sending is instructed and controlled by a program installed in the unit. The program is written and compiled in a VisualBasic Compiler, a software that is capable of making Windows based application. The compiler uses a 4th generation programming language which is object oriented.

12 | P a g e

The system also has a network of cameras that enables the user o have the means of aiming the gun, and enables the user to see the state and the position of the system. One of its interesting features is the surveillance camera which is capable of rotation to have a better view of the subject. It has a motion detecting mechanism which only activates if the camera is stationary, and has an alarm that would inform the user about the detected movements in the field, thereby still offering its top security even if the operator is momentarily not around.

13 | P a g e

PROGRAM FLOWCHART

STARTUP FORM
Password Activated: Activates all VB forms Mouse Click Interface Keyboard User Interface

Controller Form

Keyboard and Mouse Click Events and Procedures

MSComm Object

MCU

WEBCAM1

Video Processor

GUNs VIEW VB Form Output

SURVEILANC E

WEBCAM2

Video Processor

Picture Cell Analyzer

ALARM/BUZ

Signal Analyzer

MSComm Object

MCU

14 | P a g e

PROGRAM DESCRIPTION

The VisualBasic program that is develop by the Group for this system has four VB forms, namely the startup, controller, guns view and the surveillance. Here is the discussion of their uses:

The Startup Form

The startup form is compose of textbox object and command buttons that ask the user to enter the activation code of the system.

This provides added protection so that only the real operating personnel could activate the system and loads the other forms into the computer once activated.

The Controller Form

This controller, which is a little bit colorful, is the heart of the system.

It provides the means of interpreting the keyboard pressing of the user to send the appropriate information to the MCU.

It has also provision for mouse click interface.


15 | P a g e

The Guns View Form

Its purpose is to serve as a pointer for the gunner to the target. Uses Window API declaration to call the camera and extract its video to be used in the system.

The Surveillance Form

Double click video/ picture box to show all the statistical data

This form uses a an algorithm that detects pixel change in the output video on a PictureBox object.

16 | P a g e

At runtime the green dots will appear in the area of the video where the pixel change or motion happens. Then the white would serve as a measure of the average movement in the planes coordinate and interpret this data to instruct the MCU to move the gun, with the pointer camera, to the place where the motion is detected, thereby giving ease to the personnel, who will pinpoint and shoot the intruder

17 | P a g e

CIRCUIT DIAGRAM

18 | P a g e

CIRCUIT DESCRIPTION

The circuit is made to analyze and process the data that is send by the computer through its USB ports. The information is transmitted to the MCU asynchronously at 9600 bauds, byte per byte. The MCU, PIC16F877A a 40-pin, dual-in-line package IC used in this project has serial interrupt module that enables it to receive incoming bytes of data even it is processing looping task. The data received by the device is divided into two categories; the first is used to control the sending of pulse width modulation signals to the three servomotors; the second is used to signal the relay drivers that controls the switching operation of the guns trigger, flashlight, and laser pointer. Servomotors is controlled by means of Pulse Width Modulation (PWM). PWM, called Duration Modulation (PDM) or Pulse Length Modulation (PLM), is a type of digital pulse modulation that uses the width (active portion of the duty cycle) of a also Pulse

19 | P a g e

constant

amplitude

pulse

and

varies

it

proportionally to the information. The servomotor used in this project is a 6V, 50 Hz, 1.5 ms-center signal. The 1.5 ms-center parameter specifies that at the pulse duration of 1.5 ms the servos lever arm is at its center.

CONCLUSION

Upon interpreting and analyzing the research done, the proponents obtained the following conclusions with respect to the objective: It is possible to have both a security and defense system using an integrated electronic gun through computer interfacing. It is possible to control and direct every movement of the electronic gun and even the camera using visual basic program. It can possibly reduce the risk or danger of the security personnel from the intruder. It is very useful to integrate a motion detecting mechanism in a surveillance system.

20 | P a g e

RECOMMENDATION

The following are the recommendations for subsequent developments: The interfaced electronic gun can be wireless to avoid trouble and mess also in the system and to have an improved exterior. The interfaced electronic gun can be enhanced by reducing the size of the platform (base) to have a more compact appearance. The system can also use an auto aim program for it to automatically detect, point and fire to an intruder.

21 | P a g e

APPENDIX A

Sourcecode for the microcontroller: 'leftright RD0 , up/down RD1 , camera RD2 , fire RD3 , light RC4 , laser RC5 @ DEVICE HS_OSC DEFINE OSC 20 dir var WORD dir =750 dir2 var WORD dir2 =750 dir3 var WORD dir3 =750 TRISD=0 TRISD.3=0 fire var PORTD.3 fire=0 TRISC.4=0 light var PORTC.4 light=0 TRISC.5=0 laser var PORTC.5 laser=0 TRISC.7 = 1 TXSTA=$24 RCSTA=$90 SPBRG=129 ' RC.7 => USART RX pin set to input ' enable transmit and SPBRGH=1 ' Enable USART and continuous receive ' BAUD RATE = 9600 BAUDS
22 | P a g e

' clocked at 20 MHZ

PIE1.5 =1 INTCON.6 =1 RCIF

' enable USART receive interrupt ' enable peripheral interrupt

var PIR1.5 ' receiver interrupt ' use to store RCREG content ' use to erase RCREG register

DataIn var byte Discard var byte

on interrupt goto USARTInterrupt PORTD=0 PORTC=0 main: PULSout PORTD.0 , dir 'left/right PULSout PORTD.4 , dir2 'up/down PULSout PORTD.2 , dir3 'camera pause 11 goto main disable interrupt USARTInterrupt: PORTD=0 RCSTA=0 datain=RCREG while RCif discard=RCREG wend select case datain CASE "a" if dir > 1050 then dir = 1050 endif dir = dir + 3 case "d" if dir < 450 then dir = 450 endif dir = dir - 3
23 | P a g e

' Disable serial port AND ' clear possible error (FERR,OERR) ' Get data ' wait untill the RCREG is empty ' by reading it and store result in a ' don't care variable ' Analyze and Interpret the received Data

case "s" if dir2 > 940 then dir2 = 940 endif dir2 = dir2 + 3 case "w" if dir2 < 560 then dir2 = 560 endif dir2 = dir2 - 3 CASE "q" if dir3 > 1050 then dir3 = 1050 endif dir3 = dir3 + 3 case "e" if dir3 < 450 then dir3 = 450 endif dir3 = dir3 - 3 case " " 'all servo back to center dir = 750 dir2 = 750 dir3 = 750 case "f" 'control trigger fire = 1 case "F" fire = 0 case "l" 'control light toggle light case "x" 'control laser toggle laser end select

RCSTA=$90 resume enable interrupt end

' Re-enable the serial PORT AND ' get out of here

24 | P a g e

APPENDIX B Sourcecode for the VisualBasic Forms


Startup Form: Private Sub cmdexit_Click() reply = MsgBox("Are you sure you want to exit?", vbOKCancel) If reply = vbOK Then End End If End Sub Private Sub cmdOk_Click() Dim reply Dim prompt Dim prompt1 prompt = "Please enter activation code to continue." & Chr(13) & Chr(10) & "Do you still want to continue?" prompt1 = "Incorrect activation code!" & Chr(13) & Chr(10) & "Do you still want to continue?" If Txtcode.Text = "" Then reply = MsgBox(prompt, vbOKCancel, "ERROR") If reply = vbCancel Then End End If GoTo here End If If Txtcode.Text = "PIECES" Then Load frmmain startup.Hide frmmain.Show Else reply = MsgBox(prompt1, vbOKCancel, "ERROR")
25 | P a g e

If reply = vbCancel Then End Else Txtcode.Text = "" End If End If here: End Sub Controller Form: (general) Dim a As Boolean Dim X As Boolean Dim s As Boolean Dim w As Boolean Dim d As Boolean Dim z As Boolean Dim Y As Boolean Dim j As Boolean Dim k As Boolean Dim m As Boolean Sub ctrl() If w And d Then ms1.Output = "w" ms1.Output = "d" p1.Left = p1.Left + 70 p1.Top = p1.Top - 70 ElseIf w And a Then ms1.Output = "w" ms1.Output = "a" p1.Top = p1.Top - 70 p1.Left = p1.Left - 70 ElseIf s And a Then ms1.Output = "s" ms1.Output = "a" p1.Top = p1.Top + 70 p1.Left = p1.Left - 70 ElseIf s And d Then ms1.Output = "s"
26 | P a g e

ms1.Output = "d" p1.Top = p1.Top + 70 p1.Left = p1.Left + 70 ElseIf d = True Then ms1.Output = "d" p1.Left = p1.Left + 70 ElseIf a = True Then ms1.Output = "a" p1.Left = p1.Left - 70 ElseIf w = True Then ms1.Output = "w" p1.Top = p1.Top - 70 ElseIf s = True Then ms1.Output = "s" p1.Top = p1.Top + 70 End If End Sub Sub ctrl2() If w And d Then ms1.Output = "w" ms1.Output = "d" ms1.Output = "w" ms1.Output = "d" p1.Left = p1.Left + 70 p1.Top = p1.Top - 70 ElseIf w And a Then ms1.Output = "w" ms1.Output = "a" ms1.Output = "w" ms1.Output = "a" p1.Top = p1.Top - 70 p1.Left = p1.Left - 70 ElseIf s And a Then ms1.Output = "s" ms1.Output = "a" ms1.Output = "s" ms1.Output = "a"
27 | P a g e

p1.Top = p1.Top + 70 p1.Left = p1.Left - 70 ElseIf s And d Then ms1.Output = "s" ms1.Output = "d" ms1.Output = "s" ms1.Output = "d" p1.Top = p1.Top + 70 p1.Left = p1.Left + 70 ElseIf d = True Then ms1.Output = "d" ms1.Output = "d" p1.Left = p1.Left + 70 ElseIf a = True Then ms1.Output = "a" ms1.Output = "a" p1.Left = p1.Left - 70 ElseIf w = True Then ms1.Output = "w" ms1.Output = "w" p1.Top = p1.Top - 70 ElseIf s = True Then ms1.Output = "s" ms1.Output = "s" p1.Top = p1.Top + 70 End If End Sub Private Sub Command1_Click() Label2.Caption = Label2.Caption + "wa" End Sub Optional: Private Sub cmdxcam_Click() If m = True Then m = False frmwidecam.Timer1.Enabled = False frmwidecam.Hide cmdxcam.Caption = "surveilance on" GoTo here
28 | P a g e

End If If m = False Then m = True frmwidecam.Timer1.Enabled = True frmwidecam.Show cmdxcam.Caption = "surveilance off" here: End If End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyW w = True ctrl Case vbKeyD d = True ctrl Case vbKeyS s = True ctrl Case vbKeyA a = True ctrl Case vbKeyShift ctrl2 ctrl2 Y = True Case vbKeyL If j = True Then ms1.Output = "l" Label2.Caption = Label2.Caption + "wa" j = False End If Case vbKeySpace If k = True Then ms1.Output = "f" Label2.Caption = Label2.Caption + "wa" k = False End If Case vbKeyP If X = True Then ms1.Output = "x" Label2.Caption = Label2.Caption + "wa" X = False End If
29 | P a g e

Case vbKeyLeft ms1.Output = "q" ms1.Output = "q" Case vbKeyRight ms1.Output = "e" ms1.Output = "e" Case vbKeyC ms1.Output = " " End Select End Sub Private Sub Form_KeyPress(KeyAscii As Integer) If Y Then ctrl2 End If End Sub Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer) Select Case KeyCode Case vbKeyW w = False Case vbKeyD d = False Case vbKeyS s = False Case vbKeyA a = False Case vbKeyShift Y = False Case vbKeyL j = True Case vbKeyP X = True Case vbKeySpace ms1.Output = "F" Label2.Caption = Label2.Caption + "Wa" k = True End Select End Sub Private Sub Form_Load() ms1.PortOpen = True X = True j = True k = True ms1.Output = "F"
30 | P a g e

ms1.Output = " " m = True End Sub Private Sub Label1_DblClick() ms1.Output = "f" Label1.BackColor = vbRed Label2.Caption = Label2.Caption + "bang!""" End Sub Private Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ms1.Output = "f" Label1.BackColor = vbRed Label2.Caption = Label2.Caption + "bang!" End Sub Private Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) ms1.Output = "F" Label1.BackColor = vbGreen Label2.Caption = Label2.Caption + "bang!" End Sub Private Sub Label10_Click() ms1.Output = "s" ms1.Output = "a" End Sub Private Sub Label11_Click() ms1.Output = "s" ms1.Output = "d" End Sub Private Sub Label2_Click() Label2.Caption = "" End Sub Private Sub Label3_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) ms1.Output = " " Label3.BackColor = vbRed End Sub Private Sub Label3_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Label3.BackColor = vbGreen
31 | P a g e

End Sub Private Sub Label4_Click() ms1.Output = "a" End Sub Private Sub Label5_Click() ms1.Output = "d" End Sub Private Sub Label6_Click() ms1.Output = "w" End Sub Private Sub Label7_Click() ms1.Output = "s" End Sub Private Sub Label8_Click() ms1.Output = "w" ms1.Output = "d" End Sub Private Sub Label9_Click() ms1.Output = "w" ms1.Output = "a" End Sub

32 | P a g e

Guns View Form: 'FOR WEBCAM DECLARATIONS Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" Alias "capCreateCaptureWindowA" (ByVal lpszWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hwndParent As Long, ByVal nID As Long) As Long Private mCapHwnd As Long Private mCapHwnd2 As Long Private mCapHwnd3 As Long Private Const CONNECT As Long = 1034 Private Const DISCONNECT As Long = 1035 Private Const GET_FRAME As Long = 1084 Private Const COPY As Long = 1054 Dim Tppx As Single, Tppy As Single 'declarations Dim l As Integer Dim r As Integer Option Explicit Private Sub Form_Load() Load frmctrl frmctrl.Show STARTCAM End Sub Private Sub Timer1_Timer() Dim reply 'Get the picture from camera.. the main part SendMessage mCapHwnd, GET_FRAME, 0, 0 SendMessage mCapHwnd, COPY, 0, 0 mCapHwnd2 = SendMessage(mCapHwnd, GET_FRAME, 0, 0) mCapHwnd3 = SendMessage(mCapHwnd, COPY, 0, 0) Picture1.Picture = Clipboard.GetData Clipboard.Clear
33 | P a g e

End Sub Sub STARTCAM() mCapHwnd = _ capCreateCaptureWindow("WebcamCapture", 0, 0, 0, 640, 480, Me.hwnd, 0) DoEvents SendMessage mCapHwnd, CONNECT, 0, 0 Timer1.Enabled = True End Sub Surveilance Form: 'WEBCAM DECLARATIONS Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As Long Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _ Alias "capCreateCaptureWindowA" (ByVal lpszWindowName As String, _ ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, _ ByVal nWidth As Long, ByVal nHeight As Long, ByVal hwndParent As Long, _ ByVal nID As Long) As Long Private mCapHwnd As Long Private Const CONNECT As Long = 1034 Private Const DISCONNECT As Long = 1035 Private Const GET_FRAME As Long = 1084 Private Const COPY As Long = 1054 'declarations Dim i1 As Integer, j1 As Integer, i2 As Integer, j2 As Integer, a As Integer Dim p1 As Boolean Dim t As Integer Dim act As Boolean Dim h As Integer Dim w As Integer Dim P() As Long Dim POn() As Boolean Dim Cor() As Boolean Dim rl() As Boolean Dim cnt1() As Integer Dim cnt2() As Integer Dim inten As Integer Dim i As Integer, j As Integer Dim Ri As Long, Wo As Long Dim RealRi As Long Dim c As Long, c2 As Long
34 | P a g e

Dim R As Integer, G As Integer, B As Integer Dim R2 As Integer, G2 As Integer, B2 As Integer Dim Tppx As Single, Tppy As Single Dim Tolerance As Integer Dim RealMov As Integer Dim Counter As Integer Option Explicit Sub movegun() a=2 If i1 > (w / (a * inten)) Then For i = 0 To (w / (a * inten)) Label3.Caption = Label3.Caption & "d" MsComm1.Output = d Next i Else For i = 0 To (w / (a * inten)) - i1 Label3.Caption = Label3.Caption & "a" MsComm1.Output = a Next i End If If j1 > (h / (a * inten)) Then For j = 0 To (h / (a * inten)) Label3.Caption = Label3.Caption & "s" MsComm1.Output = s Next j Else For j = 0 To (h / (a * inten)) - j1 Label3.Caption = Label3.Caption & "w" MsComm1.Output = w Next j End If End Sub Sub analyse() For i = 1 To w / inten - 2 t=0 For j = 1 To h / inten - 2 If rl(i, j) = True Then t=t+1 End If Next j cnt1(i) = t Next i

35 | P a g e

For j = 1 To h / inten - 2 t=0 For i = 1 To w / inten - 2 If rl(i, j) = True Then t=t+1 End If Next i cnt2(j) = t Next j i1 = w / (2 * inten) j1 = h / (2 * inten) For i = 1 To w / inten - 2 If cnt1(i - 1) < cnt1(i) Then i1 = i End If Next i For j = 1 To h / inten - 2 If cnt2(j - 1) < cnt2(j) Then j1 = j End If Next j End Sub Private Sub Command1_Click() If act = True Then Command1.Caption = "real movements" act = False Else Command1.Caption = "all movements" act = True End If End Sub Private Sub Command2_Click() a = Val(Text3.Text) End Sub Private Sub Form_Load() p1 = True 'set up the visual stuff Form1.Height = 4455 Form1.Width = 5200 w = 325 h = 250
36 | P a g e

Picture1.Width = w * Screen.TwipsPerPixelX Picture1.Height = h * Screen.TwipsPerPixelY 'Inten is the measure of how many pixels are going to be recognized inten = 5 'The tolerance of recognizing the pixel change Tolerance = 20 Tppx = Screen.TwipsPerPixelX Tppy = Screen.TwipsPerPixelY ReDim POn(w / inten, h / inten) alloting aray spaces ReDim P(w / inten, h / inten) ReDim Cor(w / inten, h / inten) ReDim rl(w / inten, h / inten) ReDim cnt1(w / inten) ReDim cnt2(h / inten) act = False STARTCAM End Sub Private Sub Picture1_DblClick() If p1 = True Then Form1.Width = 7065 p1 = False Else Form1.Width = 5200 p1 = True End If End Sub Private Sub Timer1_Timer() Tolerance = Val(Text2.Text) j1 = 0 i1 = 0 For i = 0 To w / inten For j = 0 To h / inten rl(i, j) = False Next j Next i For j = 0 To w / inten cnt1(j) = 0 Next j For j = 0 To h / inten cnt2(j) = 0 Next j 'Get the picture from camera.. the main part
37 | P a g e

SendMessage mCapHwnd, GET_FRAME, 0, 0 SendMessage mCapHwnd, COPY, 0, 0 Picture1.Picture = Clipboard.GetData Clipboard.Clear Ri = 0 'right Wo = 0 'wrong LastTime = GetTickCount For i = 0 To w / inten - 1 For j = 0 To h / inten 1 'get a point c = Picture1.Point(i * inten * Tppx, j * inten * Tppy) 'analyze it, Red, Green, Blue R = c Mod 256 G = (c \ 256) Mod 256 B = (c \ 256 \ 256) Mod 256 'recall what the point was one step before this c2 = P(i, j) 'analyze it R2 = c2 Mod 256 G2 = (c2 \ 256) Mod 256 B2 = (c2 \ 256 \ 256) Mod 256 'main comparison part... if each R, G and B are somewhat same, then it pixel is same still If Abs(R - R2) < Tolerance And Abs(G - G2) < Tolerance And _ Abs(B - B2) < Tolerance Then 'pixel remained same Ri = Ri + 1 'Pon stores a boolean if the pixel changed or didnt, to be used to detect REAL movement POn(i, j) = True Else 'Pixel changed Wo = Wo + 1 'make a red dor P(i, j) = Picture1.Point(i * inten * Tppx, j * inten * Tppy) If act = True Then Picture1.PSet (i * inten * Tppx, j * inten * Tppy), vbRed End If
38 | P a g e

POn(i, j) = False End If Next j Next i RealRi = 0

For i = 1 To w / inten - 2 For j = 1 To h / inten - 2 'Real movement is simply occuring when all 4 pixels around one pixel changed 'Simply put, If this pixel is changed and all around it changed too, then this is a real 'movement If POn(i, j) = False Then If POn(i, j + 1) = False Then If POn(i, j - 1) = False Then If POn(i + 1, j) = False Then If POn(i - 1, j) = False Then RealRi = RealRi + 1 Picture1.PSet _ (i * inten * Tppx, j * inten * Tppy), vbGreen rl(i, j) = True End If End If End If End If End If Next j Next i analyse 'state all statistics Label1.Caption = Int(Wo / (Ri + Wo) * 100) & " % movement" & vbCrLf & "Real Movement: " & RealRi If RealRi > Val(Text1.Text) Then DoEvents Beep End If i2 = i1 * (3360 / (h / inten)) j2 = j1 * (4440 / (w / inten)) Label3.Caption = "Ri=" & Ri & vbCrLf & "Wo=" & Wo & vbCrLf & "R=" & R & vbCrLf & "G=" & G & vbCrLf & "B=" & B & vbCrLf & "R2=" & R2 & vbCrLf & "G2=" & G2 & vbCrLf & "B2=" & B2 & vbCrLf & "RealRi=" & RealRi & vbCrLf & "i1=" & i1 & ";j1=" & j1 & vbCrLf & "X=" & i2 & vbCrLf & ";Y=" & j2 & vbCrLf Shape1.Top = j2
39 | P a g e

Shape1.Left = i2 movegun End Sub Sub STARTCAM() mCapHwnd = capCreateCaptureWindow("WebcamCapture", 0, 0, 0, w, h, Me.hwnd, 0) DoEvents SendMessage mCapHwnd, CONNECT, 0, 0 Timer1.Enabled = True End Sub

40 | P a g e

Anda mungkin juga menyukai