|
|
|
import cv2 |
|
import numpy as np |
|
import logging |
|
|
|
|
|
logging.basicConfig( |
|
level=logging.DEBUG, |
|
format="%(asctime)s - %(levelname)s - %(message)s", |
|
filename="logs.log", |
|
) |
|
|
|
|
|
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: |
|
|
|
|
|
res = cv2.resize(image,(500*image.shape[1]//image.shape[0],500), interpolation = cv2.INTER_CUBIC) |
|
|
|
|
|
brightness = 16 |
|
contrast = 0.95 |
|
res2 = cv2.addWeighted(res, contrast, np.zeros(res.shape, res.dtype), 0, brightness) |
|
|
|
|
|
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) |
|
sharpened_image = cv2.filter2D(res2, -1, kernel) |
|
|
|
|
|
enhanced_images.append(sharpened_image) |
|
|
|
|
|
for image in single_object_images: |
|
|
|
|
|
res = cv2.resize(image,(500*image.shape[1]//image.shape[0],500), interpolation = cv2.INTER_CUBIC) |
|
|
|
|
|
brightness = 16 |
|
contrast = 0.95 |
|
res2 = cv2.addWeighted(res, contrast, np.zeros(res.shape, res.dtype), 0, brightness) |
|
|
|
|
|
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) |
|
sharpened_image = cv2.filter2D(res2, -1, kernel) |
|
|
|
|
|
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) |
|
|