# 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)