priya-zha / Software-Engineering-Project

0 stars 2 forks source link

Enhance Flask API with Image and video File Validation for Improved Security and Reliability #12

Closed Sekhar0799 closed 11 months ago

Sekhar0799 commented 11 months ago

Issue Description:

The Flask API code lacks image file validation, which is essential for security and accuracy. I propose adding an is_valid_image_file function to validate image uploads. The function uses the Python Imaging Library (PIL) and opencv (cv2) to check the integrity and validity of uploaded image files, ensuring they are in supported formats. This step is crucial for preventing the processing of potentially harmful or incompatible files. By implementing this image validation function, the Flask API will become more robust and secure, allowing it to handle only valid image uploads while rejecting any invalid or malicious files.

Expected Impact:

The addition of image/video file validation will enhance the security and accuracy of the Flask API. It will ensure that only valid and safe image/video uploads are processed while rejecting invalid or potentially malicious files. This is essential for maintaining the integrity of the YOLO inference process and protecting the API from potential security risks.

image image
Sekhar0799 commented 11 months ago

Image and Video File Resolution Validation

When working with image and video files in your application, it's essential to ensure that these files meet specific resolution requirements. Validating the resolution of uploaded files is crucial for maintaining consistency, optimizing storage, and ensuring compatibility with downstream processes.

Image Resolution Validation

You can use Python's PIL (Python Imaging Library) to validate the resolution of image files. Here's a code snippet that checks whether an image file meets certain resolution criteria:

from PIL import Image

def is_valid_image_file(file_path):
    try:
        img = Image.open(file_path)
        img.verify()  # Check the image file's integrity
        return True
    except Exception as e:
        print(f"Error while checking image validity: {str(e)}")
        return False

To use this function, simply pass the file path of the image and the minimum required width and height. If the image's resolution is greater than or equal to the specified values, the function returns True.

Video File Resolution Validation

When dealing with video files, you can use the OpenCV library to validate the resolution. Here's an example code snippet for video resolution validation:

import cv2

def is_valid_video_file(file_path):
    try:
        cap = cv2.VideoCapture(file_path)

        if not cap.isOpened():
            return False  

        cap.release()  
        return True

    except Exception as e:
        print(f"Error while checking video validity: {str(e)}")
        return False

This function checks the resolution of a video file by opening it with OpenCV and retrieving the frame width and height. If the resolution meets the specified criteria, the function returns True.

Usage

You can use these functions within Flask application to validate the resolution of uploaded image and video files before processing or storing them. This ensures that only files with the desired resolution are accepted, maintaining consistency and quality in application.