1. A method for calculating a total number of objects in a space by an infrared depth sensor, wherein the infrared depth sensor is configured above the space and shoots downward, and at least one depth range of the space is defined, the method comprising steps of: (a) using the infrared depth sensor to shoot N frames in a time period and averaging the N frames to obtain a background depth map, where the N is a natural number; (b) shooting a (N+1) th frame where at least one of the objects appear; (c) using a loop method to scan every pixel of the background depth map and every pixel of the (N+1) th frame according to the at least one depth range, and extracting the pixels of the (N+1) th frame different from the corresponding pixels of the background depth map to construct a first foreground depth map; (d) using at least one first erosion, at least one dilation and at least one second erosion to eliminate a noise of the first foreground depth map, and to obtain a second foreground depth map; (e) classifying and marking a plurality of pixels of the second foreground depth map, wherein adjacent ones of the plurality of pixels are classified into the same set; and (f) reclassifying the marked plurality of pixels of the second foreground depth map to obtain the total number of the objects.