File size: 3,732 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# file to crop object from image and enhance it.
import cv2
import numpy as np
import logging

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

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

def croped_images(image,merged_boundary_boxes):
    """ Crop object from original image
    Args:
        image (numpy array): get numpy array of image which has 3 channels
        merged_boundary_boxes (json array): get json array 

    Returns:
        croped_images_list(list of numpy array): returns list which has croped images
        single_object_images(list of numpy array): returns list which has single object images
    """
    logger.info('croped images function is called...')
    try:
        croped_images_list = []
        single_object_images = []

        for data in merged_boundary_boxes:
            crop_image = image[data['merged_boundries']['top_left'][1]:data['merged_boundries']['bottom_right'][1],data['merged_boundries']['top_left'][0]:data['merged_boundries']['bottom_right'][0]]
            croped_images_list.append(crop_image)

            for object in data['actual_boundries']:
                if object['class']=='person':
                    crop_object= image[object['top_left'][1]:object['bottom_right'][1],object['top_left'][0]:object['bottom_right'][0]]
                    single_object_images.append(crop_object)


        return croped_images_list,single_object_images

    except Exception as e:
        logger.error("Something went wrong in croped image function...")
        logger.error(e)
        

def image_enhancements(croped_images_list,single_object_images):
    """ Enhance cropped image using openCV techniques
    Args:
        croped_images_list (list numpy array): croped images list 
        single_object_images (list numpy array): single object images list

    Returns:
        enhanced croped images: returns enhanced images
        enhanced single_object_images: returns enhanced images
    """
    logger.info("image enhance function is called...")
    try:
        enhanced_images = []
        enhanced_single_object_images = []

        for image in croped_images_list:

            # resize the image
            res = cv2.resize(image,(500*image.shape[1]//image.shape[0],500), interpolation = cv2.INTER_CUBIC)
        
            # brightness and contrast
            brightness = 16
            contrast = 0.95
            res2 = cv2.addWeighted(res, contrast, np.zeros(res.shape, res.dtype), 0, brightness) 

            # Sharpen the image 
            kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) 
            sharpened_image = cv2.filter2D(res2, -1, kernel) 

            #append in the list
            enhanced_images.append(sharpened_image)

        
        for image in single_object_images:

            # resize the image
            res = cv2.resize(image,(500*image.shape[1]//image.shape[0],500), interpolation = cv2.INTER_CUBIC)   

            # brightness and contrast
            brightness = 16
            contrast = 0.95 
            res2 = cv2.addWeighted(res, contrast, np.zeros(res.shape, res.dtype), 0, brightness) 
            
            # Sharpen the image 
            kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) 
            sharpened_image = cv2.filter2D(res2, -1, kernel) 

            #append enhnaced single object image
            enhanced_single_object_images.append(sharpened_image)
        
        return enhanced_images,enhanced_single_object_images

    except Exception as e:
        logger.error("Something went wrong in image enhancements function...")
        logger.error(e)