File size: 1,480 Bytes
3c87883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# file to get distances of object from image

# Configure the logger
import logging
logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s - %(levelname)s - %(message)s",
    filename="logs.log",
)

# Create a logger
logger = logging.getLogger("pipline")

#constants
PERSON_HEIGHT = 1.5
VEHICAL_HEIGHT = 1.35
ANIMAL_HEIGHT = 0.6
FOCAL_LENGTH = 6400

def get_distances(merged_boundary_boxes):
    """get distance of object fro image

    Args:
        merged_boundary_boxes (json array): takes json array of detected image's data

    Returns:
        distance_list: list of distances of each object
    """
    logger.info("get_distances function is called...")
    
    try:
        distance_list = []
        for box in merged_boundary_boxes:
            for actual_box in box['actual_boundries']:
                height = actual_box['bottom_right'][1] - actual_box['top_left'][1]

                if actual_box['class'] == "person":
                    distance = FOCAL_LENGTH*PERSON_HEIGHT/height
                    
                elif actual_box['class'] == "vehical":
                    distance = FOCAL_LENGTH*PERSON_HEIGHT/height

                else:
                    distance = FOCAL_LENGTH*PERSON_HEIGHT/height

                distance_list.append(str(round(distance)) + "m")
        return distance_list
    except Exception as e:
        logger.error("Something went wrong in distance estimation function...")
        logger.error(e)