## 3 Subsystems
### Intelligent Persons Counter System  
The identification of occupancy and the number of persons inside a building is important for energy management algorithms and building optimizations because the presence of people drastically changes the optimization results. However, the simple counting of persons is a very complex and difficult task. The intelligent persons counter system uses a webcam to detect the movement of persons in the main hallway of our building. Previous solutions were tried with infrared line sensors. However, they resulted in higher errors and were not efficient enough to provide a precise counter system. Some types of clothes and the line infrared sensor became problems that seemed impossible to overpass. The sensors also had some false positives that mislead the system. The solution adopted uses a webcam and computer vision algorithms to process the webcam image, this enables a more precise and stable system. Figure 9: Desk light control flowchart. The system was developed in Python and runs on a desk computer where the webcam is connected. The webcam is located in the hallway's ceiling and reaches all the hallway in order to detect any movement. Figure 10 shows the connections between the system modules. Beside the webcam, the system uses another external connection to energy analyzers in order to execute the system reset routine. The Python software is able to detect a person and trace their route. This tracing can be seen on the left screen of Figure 11 where a person is detected and marked within a green rectangle and where the person's route is represented by a green line. The blue and red lines are specified limits that trigger events every time a person collides with it. Using the open source computer vision library (OpenCV) the system is able to read and process the video feed of the USB webcam. Then the software separates and identifies the video background from the moving objects, displaying a black and white frame where the background is represented  
Figure 11: Software graphical interface: (**a**) person leaving the building; (**b**) person entering the building. Figure 10: Intelligent persons counter system. in black and moving objects are represented in white, this black and white frame can be seen in the right side of Figure 11. To detect the background objects, the software compares previous frames, if an object is still in a set number of previous frames, then it is identified as being part of the background. The entering of a person in the frame generates a white spot. The software uses frame filters to avoid and decrease the amount of interference generated by reflections and other signal distortions and noise. If the white spot is bigger than the defined target, the system assumes it is a person and starts the identification of the spot, to delimitate its borders. These borders are defined by the green line rectangle of Figure 11. With the limits delimitated, then the system starts the object tracking. The tracing of an object is drawn in the graphical interface, as seen in Figure 11. Every time a person passes a blue or red line, a trigger is activated. Using the blue and red line triggers sequence, the software is able to detect the direction of a person--to detect if the person in entering or leaving. During the implementation, new blue and red lines were added and experimented in order to achieve a satisfactory result. The low ceiling of the hallway and the lack of natural light were a problem that was solved using more blue and red lines and with artificial light that must be turned on--otherwise, the webcam will not be able to see any person. One error that was not possible to solve, was the error produced by two persons passing at the same time. Because the hallway is narrow and the camera angle is too short, when two persons pass at the same time the software will see only one person--as is the combination of both. This happens because the distance from both persons will be too short for effective individual identification.