Esempio n. 1
0
def preprocess(image_location):
    images = []
    for location in image_location:
        img = cv2.imread(location)
        print("location ", location)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        ret, thresh = cv2.threshold(gray, 127, 255,
                                    cv2.THRESH_TRUNC)  #applying truncation
        rgb = np.repeat(thresh[..., np.newaxis], 3, -1)
        print("type ", type(rgb))
        images.append((Image(pil2tensor(rgb, dtype=np.float32).div_(255))))
        print("tensor ",
              type(Image(pil2tensor(rgb, dtype=np.float32).div_(255))))
    return images
Esempio n. 2
0
def load_image(image_file):
    pil_img = PIL.Image.open(image_file)

    img = pil_img.convert('RGB')
    img = image.pil2tensor(img, np.float32).div_(255)
    img = image.Image(img)
    return img, np.asarray(pil_img)
Esempio n. 3
0
 def run(self):
     with verrou:
         H, W, C = self.image.shape
         X, Y, w, h = self.coord
         X_1, X_2 = (max(0, X - int(w)), min(X + int(w), W))
         Y_1, Y_2 = (max(0, Y - int(h)), min(Y + int(h), H))
         img_cp = self.image[Y_1:Y_2, X_1:X_2].copy()
         img_cp1 = cv2.cvtColor(img_cp, cv2.COLOR_BGR2RGB)
         prediction = str(
             learn.predict(Image(pil2tensor(img_cp1, np.float32).div_(255)))[0]
         ).split(";")
         label = (
             " ".join(prediction)
             # if "No_Beard" in prediction
             # else "Beard " + " ".join(prediction)
         )
         label_list = label.split(" ")
         self.image = cv2.rectangle(self.image, (X, Y), (X + w, Y + h), (150, 0, 100), 2)
         for idx in range(1, len(label_list) + 1):
             cv2.putText(
                 self.image,
                 LABEL_MAP[label_list[idx - 1]],
                 (X, Y - 14 * idx),
                 cv2.FONT_HERSHEY_SIMPLEX,
                 0.45,
                 (80, 100, 50),
                 2,
             )
         print("Label :", label)
Esempio n. 4
0
def open_4_channel(fname):
    fname = str(fname)
    # strip extension before adding color
    if fname.endswith('.png'):
        fname = fname[:-4]
    colors = ['red', 'green', 'blue', 'yellow']
    flags = cv2.IMREAD_GRAYSCALE
    img = [
        cv2.imread(fname + '_' + color + '.png', flags).astype(np.float32) /
        255 for color in colors
    ]
    x = np.stack(img, axis=-1)
    return Image(pil2tensor(x, np.float32).float())


## DEBUGGING
# def open_4_channel(fname):
#     fname = str(fname)
#     # strip extension before adding color
#     if fname.endswith('.png'):
#         fname = fname[:-4]
#     colors = ['red','green','blue','yellow']
#     flags = cv2.IMREAD_GRAYSCALE
#     img = []
#     for color in colors:
#         try:
#             im = cv2.imread(fname+'_'+color+'.png', flags).astype(np.float32)/255
#             img.append(im)
#         except AttributeError:
#             print(f"color: {color}")
#             print(f"fname: {fname}")
#             print(f"flags: {flags}")
#             print(f"Error: OpenCV was unable to open: {fname+'_'+color+'.png'}")
#     x = np.stack(img, axis=-1)
#     return Image(pil2tensor(x, np.float32).float())
Esempio n. 5
0
def image_to_vec(url_img, hook, learner):
    '''
    Function to convert image to vector
    '''
    print("Convert image to vec")
    _ = learner.predict(Image(pil2tensor(url_img, np.float32).div_(255)))
    vect = hook.features[-1]
    return vect
Esempio n. 6
0
    def open(self, fn):
        """
        fn: path to the image file

        return: Image containing FloatTensor with values in [0;1]
        """
        x = open_image(fn)
        x = pil2tensor(x, np.float32)
        x = torch.cat((x, x, x))
        return Image(x/255)
    def open(self, fn):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore",
                                  UserWarning)  # EXIF warning from TiffPlugin
            x = PIL.Image.open(fn)
            if x.palette is not None:
                x = x.convert('P')
            else:
                x = x.convert('L')
            x = pil2tensor(x, np.float32)

        return ArcGISImageSegment(x, cmap=self.cmap, norm=self.mplnorm)
    def feed(self):
        def extractFaceCoords(img, cascade, tolerance):
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            face_coords = cascade.detectMultiScale(gray,
                                                   1.2,
                                                   tolerance,
                                                   minSize=(60, 60))
            return face_coords

        cap = cv2.VideoCapture(0)

        while True:
            ret, frame = cap.read()
            face_coords = extractFaceCoords(frame, self.cascade,
                                            self.tolerance)
            if face_coords is not None:
                for coords in face_coords:
                    x, y, w, h = coords
                    face_rgb = cv2.cvtColor(frame[y:y + h, x:x + h],
                                            cv2.COLOR_BGR2RGB)
                    img_fastai = Image(
                        pil2tensor(face_rgb, np.float32).div_(255))
                    prediction = self.learn.predict(img_fastai)
                    if int(prediction[0]) == 1:
                        result = self.Feature + ': True'
                    else:
                        result = self.Feature + ': False'
                    p = prediction[2].tolist()
                    prob = 'probability: ' + str(round(p[1], 3))
                    cv2.rectangle(img=frame,
                                  pt1=(x, y),
                                  pt2=(x + w, y + h),
                                  color=(255, 255, 0),
                                  thickness=1)  # color in BGR
                    cv2.putText(img=frame,
                                text=prob,
                                org=(x, y - 13),
                                fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                                fontScale=.5,
                                color=(0, 255, 0),
                                thickness=1)
                    cv2.putText(img=frame,
                                text=result,
                                org=(x, y - 26),
                                fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                                fontScale=.5,
                                color=(0, 255, 0),
                                thickness=1)

            ret, jpeg = cv2.imencode('.jpg', frame)
            output = jpeg.tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + output + b'\r\n\r\n')
Esempio n. 9
0
    def open(self, fn):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", UserWarning) # EXIF warning from TiffPlugin
            x = PIL.Image.open(fn)
            if x.palette is not None:
                x = x.convert('P')
            else:
                x = x.convert('L')
            x = pil2tensor(x, np.float32)

        if not self.is_contiguous:
            x = map_to_contiguous(x, self.pixel_mapping)
        return ArcGISImageSegment(x, color_mapping=self.color_mapping)
Esempio n. 10
0
    def open(self, fn):
        """
        fn: tuple containing path to original image and run-length encoded
            string for the corresponding mask

        return: ImageSegmentFloat containing FloatTensor with values in {0;1}
        """
        assert self.train_path, "a path for train set must be specified"
        img_path = fn[0]
        rle = fn[1]
        h, w = open_image(self.train_path/img_path).shape
        y = rle2mask(rle, w, h)
        y = pil2tensor(y, np.float32)
        return ImageSegmentFloat(y/255)
Esempio n. 11
0
    def open(self, img_path, rle):
        """
        Creates the mask corresponding to an image from run-length encoded
        string

        img_path: path to original image
        rle: run-length encoded string for the mask

        return: ImageSegment object that contains FloatTensor for the mask
                with values in {0;1}
        """
        assert self.train_path, "a path for train set must be specified"
        h, w = open_image(self.train_path/img_path).shape
        y = rle2mask(rle, w, h)
        y = pil2tensor(y, np.float32)
        return ImageSegment(y/255)
 def open_image_custom(fn:typing.Union[pathlib.Path, str], 
                   div:bool=True, 
                   convert_mode:str='RGB', 
                   cls:type=fastai.vision.Image, 
                   after_open:Callable=None)->fastai.vision.Image:
     "Open image in `fn`."
     fn = Path(fn)
     tile_name = fn.name
     t = tile_name_to_tile_object[tile_name]
     tile = tiles.ExtractTileFromWSI(path=t.wsi_path, 
                                     x=t.get_x(), 
                                     y=t.get_y(), 
                                     width=t.get_width(), 
                                     height=t.get_height(), 
                                     level=t.level)
     tile = tile.convert(convert_mode)
     if after_open: 
         tile = after_open(tile)
     tile = pil2tensor(tile,np.float32)
     if div: 
         tile.div_(255)
     return cls(tile)
Esempio n. 13
0
def convert_to_fastai_image(upload_file: UploadFile):
    img = Image.open(upload_file.file).convert('RGB')
    img_tensor = pil2tensor(img, np.float32)
    img_tensor.div_(255)
    return fastai.vision.image.Image(img_tensor)
Esempio n. 14
0
    def predict(self,
                image_path,
                threshold=0.1,
                nms_overlap=0.1,
                return_scores=True,
                visualize=False,
                resize=False):
        """
        Predicts and displays the results of a trained model on a single image.

        =====================   ===========================================
        **Argument**            **Description**
        ---------------------   -------------------------------------------
        image_path              Required. Path to the image file to make the
                                predictions on.
        ---------------------   -------------------------------------------
        thresh                  Optional float. The probabilty above which
                                a detection will be considered valid.
        ---------------------   -------------------------------------------
        nms_overlap             Optional float. The intersection over union
                                threshold with other predicted bounding 
                                boxes, above which the box with the highest
                                score will be considered a true positive.
        ---------------------   -------------------------------------------
        return_scores           Optional boolean.
                                Will return the probability scores of the 
                                bounding box predictions if True.
        ---------------------   -------------------------------------------
        visualize               Optional boolean. Displays the image with 
                                predicted bounding boxes if True.
        ---------------------   -------------------------------------------
        resize                  Optional boolean. Resizes the image to the same size
                                (chip_size parameter in prepare_data) that the model was trained on,
                                before detecting objects.
                                Note that if resize_to parameter was used in prepare_data,
                                the image is resized to that size instead.

                                By default, this parameter is false and the detections are run
                                in a sliding window fashion by applying the model on cropped sections
                                of the image (of the same size as the model was trained on).
        =====================   ===========================================
        
        :returns: 'List' of xmin, ymin, width, height of predicted bounding boxes on the given image
        """

        if not HAS_OPENCV:
            raise Exception(
                "This function requires opencv 4.0.1.24. Install it using pip install opencv-python==4.0.1.24"
            )

        if not HAS_PIL:
            raise Exception(
                "This function requires PIL. Please install it via pip or conda"
            )

        if isinstance(image_path, str):
            image = cv2.imread(image_path)
        else:
            image = image_path

        orig_height, orig_width, _ = image.shape
        orig_frame = image.copy()

        if resize and self._data.resize_to is None\
                and self._data.chip_size is not None:
            image = cv2.resize(image,
                               (self._data.chip_size, self._data.chip_size))

        if self._data.resize_to is not None:
            if isinstance(self._data.resize_to, tuple):
                image = cv2.resize(image, self._data.resize_to)
            else:
                image = cv2.resize(
                    image, (self._data.resize_to, self._data.resize_to))

        height, width, _ = image.shape

        if self._data.chip_size is not None:
            chips = _get_image_chips(image, self._data.chip_size)
        else:
            chips = [{
                'width': width,
                'height': height,
                'xmin': 0,
                'ymin': 0,
                'chip': image,
                'predictions': []
            }]

        valid_tfms = self._data.valid_ds.tfms
        self._data.valid_ds.tfms = []

        include_pad_detections = False
        if len(chips) == 1:
            include_pad_detections = True

        for chip in chips:
            frame = Image(
                pil2tensor(PIL.Image.fromarray(
                    cv2.cvtColor(chip['chip'], cv2.COLOR_BGR2RGB)),
                           dtype=np.float32).div_(255))
            self.learn.predicting = True
            bbox = self.learn.predict(frame,
                                      thresh=threshold,
                                      nms_overlap=nms_overlap,
                                      ret_scores=True,
                                      model=self)[0]
            if bbox:
                scores = bbox.scores
                bboxes, lbls = bbox._compute_boxes()
                bboxes.add_(1).mul_(
                    torch.tensor([
                        chip['height'] / 2, chip['width'] / 2,
                        chip['height'] / 2, chip['width'] / 2
                    ])).long()
                for index, bbox in enumerate(bboxes):
                    if lbls is not None:
                        label = lbls[index]
                    else:
                        label = 'Default'

                    data = bb2hw(bbox)
                    if include_pad_detections or not _exclude_detection(
                        (data[0], data[1], data[2], data[3]), chip['width'],
                            chip['height']):
                        chip['predictions'].append({
                            'xmin':
                            data[0],
                            'ymin':
                            data[1],
                            'width':
                            data[2],
                            'height':
                            data[3],
                            'score':
                            float(scores[index]),
                            'label':
                            label
                        })

        self._data.valid_ds.tfms = valid_tfms

        predictions, labels, scores = _get_transformed_predictions(chips)

        # Scale the predictions to original image and clip the predictions to image dims
        y_ratio = orig_height / height
        x_ratio = orig_width / width
        for index, prediction in enumerate(predictions):
            prediction[0] = prediction[0] * x_ratio
            prediction[1] = prediction[1] * y_ratio
            prediction[2] = prediction[2] * x_ratio
            prediction[3] = prediction[3] * y_ratio

            # Clip xmin
            if prediction[0] < 0:
                prediction[2] = prediction[2] + prediction[0]
                prediction[0] = 1

            # Clip width when xmax greater than original width
            if prediction[0] + prediction[2] > orig_width:
                prediction[2] = (prediction[0] + prediction[2]) - orig_width

            # Clip ymin
            if prediction[1] < 0:
                prediction[3] = prediction[3] + prediction[1]
                prediction[1] = 1

            # Clip height when ymax greater than original height
            if prediction[1] + prediction[3] > orig_height:
                prediction[3] = (prediction[1] + prediction[3]) - orig_height

            predictions[index] = [
                prediction[0], prediction[1], prediction[2], prediction[3]
            ]

        if visualize:
            image = _draw_predictions(orig_frame,
                                      predictions,
                                      labels,
                                      color=(255, 0, 0),
                                      fontface=2,
                                      thickness=1)
            import matplotlib.pyplot as plt
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            if getattr(self._data, "_is_coco", "") == True:
                figsize = (20, 20)
            else:
                figsize = (4, 4)
            fig, ax = plt.subplots(1, 1, figsize=figsize)
            ax.imshow(image)

        if return_scores:
            return predictions, labels, scores
        else:
            return predictions, labels
Esempio n. 15
0
 ret = count % 5
 if ret == 0:
     H, W, C = img.shape
     gray = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY)
     Traffic_sign = Traffic.detectMultiScale(gray, scaleFactor=2, minNeighbors=5, minSize=(90, 90), maxSize=(120, 120))  # 1.05
     if len(Traffic_sign) < 1:
         print("NOTHING FOUND")
     elif len(Traffic_sign) < 2:
         print("Found 1")
         X, Y, w, h = Traffic_sign[0]
         X_1, X_2 = (max(0, X - int(w)), min(X + int(w), W))
         Y_1, Y_2 = (max(0, Y - int(h)), min(Y + int(h), H))
         img_cp = img[Y_1:Y_2, X_1:X_2].copy()
         img_cp1 = cv2.cvtColor(img_cp, cv2.COLOR_BGR2RGB)
         prediction = str(
             learn.predict(Image(pil2tensor(img_cp1, np.float32).div_(255)))[0]
         ).split(";")
         img = cv2.rectangle(img, (X, Y), (X + w, Y + h), (150, 0, 100), 2)
         label = (
             " ".join(prediction)
             # if "No_Beard" in prediction
             # else "Beard " + " ".join(prediction)
             )
         label_list = label.split(" ")
         for idx in range(1, len(label_list) + 1):
             cv2.putText(
                 img,
                 LABEL_MAP[label_list[idx - 1]],
                 (X, Y - 14 * idx),
                 cv2.FONT_HERSHEY_SIMPLEX,
                 0.45,
                index = i
        x, y, w, h = face_coords[index]

    return x, y, w, h


cap = cv2.VideoCapture(0)
while (True):
    ret, frame = cap.read()
    # H, W, D = frame.shape
    face_coords = extractFaceCoords(frame, fc, 1)

    if face_coords is not None:
        x, y, w, h = face_coords
        face_rgb = cv2.cvtColor(frame[y:y + h, x:x + h], cv2.COLOR_BGR2RGB)
        img_fastai = Image(pil2tensor(face_rgb, np.float32).div_(255))
        prediction = str(learn.predict(img_fastai))
        cv2.rectangle(img=frame,
                      pt1=(x, y),
                      pt2=(x + w, y + h),
                      color=(255, 255, 255),
                      thickness=1)
        cv2.putText(img=frame,
                    text=prediction,
                    org=(x, y - 13),
                    fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                    fontScale=.5,
                    color=(255, 255, 255))

    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
Esempio n. 17
0
    for c in os.listdir('flowers/'+i):
        flower_files.append('flowers/'+i+'/'+c)

    first_half = flower_files[0:(int(len(flower_files)/2))]
    p = len(flower_files)
    second_half = flower_files[int(p/2):p]
    for c in range(int(p/2)):
        img1 = cv2.imread(first_half[c])
        img2 = cv2.imread(second_half[c])
        size = (400,400)
        img1 = cv2.resize(img1,size)
        img2 = cv2.resize(img2,size)
        img= np.concatenate((img1,img2),axis=1)
        img1_pred = cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)
        img2_pred = cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)
        pred_1 = str(learn.predict(Image(pil2tensor(img1_pred,np.float32).div_(255)))[0])
        pred_2 = str(learn.predict(Image(pil2tensor(img2_pred,np.float32).div_(255)))[0])
        print(pred_1,i)
        if str(pred_1) == str(i):
            src = cv2.imread('true.png', -1)
            img = overlay_transparent(img, src, 150, 100, (150,150))
        else:
            src = cv2.imread('wrong.png', -1)
            img = overlay_transparent(img, src, 150, 100, (150,150))

        if str(pred_2) == str(i):
            src = cv2.imread('true.png',-1)
            img = overlay_transparent(img, src, 530, 100, (150,150))
        else:
            src = cv2.imread('wrong.png', -1)
            img = overlay_transparent(img, src, 530, 100, (150,150))   
Esempio n. 18
0
    def predict(
        self,
        image_path,
        threshold=0.5,
        nms_overlap=0.1,
        return_scores=False,
        visualize=False
    ):

        """
        Runs prediction on a video and appends the output VMTI predictions in the metadata file.

        =====================   ===========================================
        **Argument**            **Description**
        ---------------------   -------------------------------------------
        image_path              Required. Path to the image file to make the
                                predictions on.
        ---------------------   -------------------------------------------
        threshold               Optional float. The probability above which
                                a detection will be considered valid.
        ---------------------   -------------------------------------------
        nms_overlap             Optional float. The intersection over union
                                threshold with other predicted bounding
                                boxes, above which the box with the highest
                                score will be considered a true positive.
        ---------------------   -------------------------------------------
        return_scores           Optional boolean. Will return the probability
                                scores of the bounding box predictions if True.
        ---------------------   -------------------------------------------
        visualize               Optional boolean. Displays the image with
                                predicted bounding boxes if True.
        =====================   ===========================================
        
        :returns: 'List' of xmin, ymin, width, height of predicted bounding boxes on the given image
        """
        if not HAS_OPENCV:
            raise Exception("This function requires opencv 4.0.1.24. Install it using pip install opencv-python==4.0.1.24")

        if isinstance(image_path, str):
            image = cv2.imread(image_path)
        else:
            image = image_path

        orig_height, orig_width, _ = image.shape
        orig_frame = image.copy()

        if self._data.resize_to is not None:
            if isinstance(self._data.resize_to, tuple):
                image = cv2.resize(image, self._data.resize_to)
            else:
                image = cv2.resize(image, (self._data.resize_to, self._data.resize_to))

        height, width, _ = image.shape

        if self._data.chip_size is not None:
            chips = _get_image_chips(image, self._data.chip_size)
        else:
            chips = [{'width': width, 'height': height, 'xmin': 0, 'ymin': 0, 'chip': image, 'predictions': []}]

        valid_tfms = self._data.valid_ds.tfms
        self._data.valid_ds.tfms = []

        for chip in chips:
            frame = Image(pil2tensor(PIL.Image.fromarray(cv2.cvtColor(chip['chip'], cv2.COLOR_BGR2RGB)), dtype=np.float32).div_(255))
            bbox = self.learn.predict(frame, thresh=threshold, nms_overlap=nms_overlap, ret_scores=True, ssd=self)[0]
            if bbox:
                scores = bbox.scores
                bboxes, lbls = bbox._compute_boxes()
                bboxes.add_(1).mul_(torch.tensor([chip['height'] / 2, chip['width'] / 2, chip['height'] / 2, chip['width'] / 2])).long()
                for index, bbox in enumerate(bboxes):
                    if lbls is not None:
                        label = lbls[index]
                    else:
                        label = 'Default'

                    data = bb2hw(bbox)
                    if not _exclude_detection((data[0], data[1], data[2], data[3]), chip['width'], chip['height']):
                        chip['predictions'].append({
                            'xmin': data[0],
                            'ymin': data[1],
                            'width': data[2],
                            'height': data[3],
                            'score': float(scores[index]),
                            'label': label
                        })

        self._data.valid_ds.tfms = valid_tfms

        predictions, labels, scores = _get_transformed_predictions(chips)

        y_ratio = orig_height/height
        x_ratio = orig_width/width

        for index, prediction in enumerate(predictions):
            prediction[0] = prediction[0]*x_ratio
            prediction[1] = prediction[1]*y_ratio
            prediction[2] = prediction[2]*x_ratio
            prediction[3] = prediction[3]*y_ratio

            # Clip xmin
            if prediction[0] < 0: 
                prediction[2] = prediction[2] + prediction[0]
                prediction[0] = 1

            # Clip width when xmax greater than original width
            if prediction[0] + prediction[2] > orig_width:
                prediction[2] = (prediction[0] + prediction[2]) - orig_width

            # Clip ymin
            if prediction[1] < 0:
                prediction[3] = prediction[3] + prediction[1]
                prediction[1] = 1

            # Clip height when ymax greater than original height
            if prediction[1] + prediction[3] > orig_height:
                prediction[3] = (prediction[1] + prediction[3]) - orig_height

            predictions[index] = [
                prediction[0],
                prediction[1],
                prediction[2],
                prediction[3]
            ]      

        if visualize:
            image = _draw_predictions(orig_frame, predictions, labels)
            import matplotlib.pyplot as plt
            plt.xticks([])
            plt.yticks([])
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            plt.imshow(PIL.Image.fromarray(image))

        if return_scores:
            return predictions, labels, scores
        else:
            return predictions, labels
Esempio n. 19
0
	pred_prob = round(torch.max(model.predict(img)[2]).item()*100)
	st.write('## This is ',str(pred_class).capitalize(),' with probablity of ',pred_prob,'%')



options = st.radio('',('Select Image','Enter Image URL','Upload an image'))
#Select image
if options =='Select Image':
	static = os.listdir('static')
	test_image = st.selectbox('Please select a image:',static)
	file_path = 'static/' + test_image
	pil_img =Image.open(file_path)

	#read the image
	img = pil_img.convert('RGB')
	img = image.pil2tensor(img,np.float32).div_(224)
	img = image.Image(img)

    #display image
	display_img = mpimg.imread(file_path)
	st.image(pil_img,use_column_width=True)

	predict(img)




#Image URL
if options == 'Enter Image URL':
	url = st.text_input('Enter Image URL')
Esempio n. 20
0
def detect_facial_attributes(input_path, output_path, save_video):
    path = Path(input_path)

    # Creating a databunch
    imagenet_stats = ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    data = (ImageItemList.from_csv(
        path, csv_name="labels.csv").no_split().label_from_df(
            label_delim=" ").transform(
                None,
                size=128).databunch(no_check=True).normalize(imagenet_stats))

    # Loading our model
    learn = create_cnn(data, models.resnet50, pretrained=False)
    learn.load("ff_stage-2-rn50")

    # Loading HAAR cascade
    face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

    cap = cv2.VideoCapture(0)
    if save_video:
        out = cv2.VideoWriter(output_path + "output.avi", -1, 20.0, (640, 480))

    while True:
        # Capture frame-by-frame
        _, frame = cap.read()

        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Find faces using Haar cascade
        face_coord = face_cascade.detectMultiScale(gray,
                                                   1.1,
                                                   5,
                                                   minSize=(30, 30))

        ## Looping through each face
        for coords in face_coord:

            ## Finding co-ordinates of face
            X, Y, w, h = coords

            ## Finding frame size
            H, W, _ = frame.shape

            ## Computing larger face co-ordinates
            X_1, X_2 = (max(0, X - int(w * 0.35)), min(X + int(1.35 * w), W))
            Y_1, Y_2 = (max(0, Y - int(0.35 * h)), min(Y + int(1.35 * h), H))

            ## Cropping face and changing BGR To RGB
            img_cp = frame[Y_1:Y_2, X_1:X_2].copy()
            img_cp1 = cv2.cvtColor(img_cp, cv2.COLOR_BGR2RGB)

            ## Prediction of facial featues
            prediction = str(
                learn.predict(Image(pil2tensor(
                    img_cp1, np.float32).div_(255)))[0]).split(";")
            label = (" ".join(prediction) if "Male" in prediction else
                     "Female " + " ".join(prediction))
            label = (" ".join(prediction) if "No_Beard" in prediction else
                     "Beard " + " ".join(prediction))

            ## Drawing facial boundaries
            cv2.rectangle(
                img=frame,
                pt1=(X, Y),
                pt2=(X + w, Y + h),
                color=(128, 128, 0),
                thickness=2,
            )

            ## Drawing facial attributes identified
            label_list = label.split(" ")
            for idx in range(1, len(label_list) + 1):
                cv2.putText(
                    frame,
                    label_list[idx - 1],
                    (X, Y - 14 * idx),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.45,
                    (0, 128, 0),
                    2,
                )

        # Display the resulting frame
        cv2.imshow("frame", frame)

        ## Save the resulting frame
        if save_video:
            out.write(frame)

        ## Escape keys
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

    # When everything done, release the capture
    cap.release()
    if save_video:
        out.release()
    cv2.destroyAllWindows()